[R] Works outside but not inside!

Keith S Weintraub kw1958 at gmail.com
Wed Feb 15 13:49:50 CET 2017


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



More information about the R-help mailing list