[R] Works outside but not inside!

Sarah Goslee sarah.goslee at gmail.com
Wed Feb 15 14:10:29 CET 2017


Well, what happens? "Doesn't work" covers a lot of ground, and you don't
provide a reproducible example.

Also, why all the > at the beginning PDF lines? Your R prompts shouldn't
look like that when entering a function, and they make it impossible to
just cut and paste the code. Not that we have any data to try it on,
either.

However, I'm going to take a wild guess that you meant to call the argument
to the first function am rather than ams because you use an object named
the former in the function, ands  not the latter. I'm further going to
guess that you have an object named am in your workspace, so the function
is running from the console because it is silently using that object. But
from another function it can't find it.

So, your first function is not actually correct, and starting from a clean
session would have shown you that, as would the process of making a
reproducible example.

Sarah

On Wed, Feb 15, 2017 at 7:51 AM Keith S Weintraub <kw1958 at gmail.com> wrote:

> Folks,
> The following function works like a charm!
>
> > #Amortization for multiple rows
> > createAmorts<-function(ams, numPer, term) {
> >   fctrs<-rep(1:term, each = numPer)
> >
> >   oneRow<-function(am,  fac){
> >     tdf<-data.frame(ams = c(am), yrs=fac)
> >     agg<-aggregate(ams ~ yrs, data = tdf, sum)
> >     agg$ams<-1-cumsum(agg$ams)
> >     agg
> >   }
> >   data.frame(id = rep(1:nrow(ams), each = term),
> >              do.call(rbind, apply(ams, 1, oneRow, fctrs)))
> > }
>
> But when I run the function inside some other code:
> > retrieveSSdata<-function(inputPath) {
> >   iList<-list()
> >   theWb<-loadWorkbook(inputPath)
> >
> >   # Set up the amorts using inputs and term and frequency
> >   theTerm<-readNamedRegion(theWb, "Term", useCachedValues = TRUE, header
> = FALSE)
> >   theFreq<-readNamedRegion(theWb, "freq", useCachedValues = TRUE, header
> = FALSE)
> >   allAmorts<-readNamedRegion(theWb, "allAmorts", useCachedValues = TRUE,
> header = FALSE)
>
> >   theAmorts<-createAmorts(allAmorts, 12/theFreq, theTerm*theFreq)
>
> >   iList[["amort"]]<-theAmorts
> >   iList[["PremAttach"]]<-readNamedRegion(theWb, "amPremAttach",
> useCachedValues = TRUE)
> >   #
> >
> >   iList
> > }
>
>
> > Note that in the above code everything seems to work fine except for the
> “createAmorts" code.
>
>
>
> I have the following packages loaded:
> require(XLConnect)
> require(plyr)
> require(sm)
> require(fOptions)
> require(fCopulae)
>
> I have spent a lot of time on this to no avail.
>
> Any help would be appreciated.
>
> Best,
> KW
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Sarah Goslee
http://www.stringpage.com
http://www.sarahgoslee.com
http://www.functionaldiversity.org

	[[alternative HTML version deleted]]



More information about the R-help mailing list