function(theList,...){
CombinedList=lapply(seq_along(theList), function (x,...) {
.x <- as.list(substitute(list(...)))[-1]
elementz=data.frame(rbind(eval(.x[[1]])[[x]],eval(.x[[2]])[[x]],eval(.x[[3]])[[x]]))
How to correctly use ellipsis in nested function
-
03-07-2022 - |
Domanda
See edit
first
I have a parent function and a child function. I would like to use ...
feature in my child function. Specifically I am wondering how I can correctly use ...
in these specific rows of code in my child function:
function(theList,...){
####
CombinedList=lapply(seq_along(theList), function (x,...) {
.x <- as.list(substitute(list(...)))[-1]
elementz=data.frame(rbind(.x[1][[x]],.x[2][[x]],.x[3][[x]]))
The ...
is supposed to be a list of lists, i.e. each element of ...
list is a list.
In my main (master) function, below, the ...
would contain SharpList
and SortinoList
.
windowSize=36
PerformanceDF <- function(data,windowSize,AvgForvalt=(0.02/12)){
require(quantmod,quietly = TRUE)
require(reshape2,quietly = TRUE)
require(PerformanceAnalytics,quietly = TRUE)
require(xts,quietly = TRUE)
#36 mån rolling window list
windows <- embed(1:nrow(data), windowSize)
lapplyApproach <- function(data, windows) {
windowsList <- split(t(windows), rep(1:nrow(windows), each=ncol(windows)))
names(windowsList) <- unlist(lapply(windowsList,
function(x) data[x[1],1]))
return(lapply(windowsList,function(x) data[rev(x),]))
}
DropBench=grep("Benchmark",names(hedgesample),ignore.case=TRUE)
data=data[,-c(DropBench)]
theList=lapplyApproach(data,windows)
##xts
theListXts=lapply(theList,function(x){
blab=xts(x[,-1],as.Date(x[,1]))
return(blab)
})
##Sharpe Ratio
SharpList=lapply(theListXts,function(x) {
hej=SharpeRatio(x,FUN="StdDev")
rownames(hej)=NULL
hej[is.infinite(hej)] <- 0
return(hej)
})
##Sortino ratio
SortinoList=lapply(theListXts,function(x) {
mmm=SortinoRatio(x)
rownames(mmm)=NULL
mmm[is.infinite(mmm)]<-0
return(mmm)
})
function(theList,...){
####
CombinedList=lapply(seq_along(theList), function (x,...) {
.x <- as.list(substitute(list(...)))[-1]
elementz=data.frame(rbind(.x[1][[x]],.x[2][[x]],.x[3][[x]]))
rownames(elementz)=NULL
elementz$Statistic=.x
return(elementz)
},...)
names(CombinedList)=names(theList)
return(CombinedList)}}
Right now I get the error
Error in .x[1][[x]] : subscript out of bounds
In addition: Warning messages:
1: In rbind(.x[1][[x]], .x[2][[x]]) :
(symbol) object cannot be coerced to type 'list'
2: In rbind(.x[1][[x]], .x[2][[x]]) :
(symbol) object cannot be coerced to type 'list'
3: In rbind(.x[1][[x]], .x[2][[x]]) :
(symbol) object cannot be coerced to type 'list'
Best Regards!
EDIT
Found a solution:
function(theList,...){
CombinedList=lapply(seq_along(theList), function (x,...) {
.x <- as.list(substitute(list(...)))[-1]
elementz=data.frame(rbind(eval(.x[[1]])[[x]],eval(.x[[2]])[[x]],eval(.x[[3]])[[x]]))
But does anyone have a nicer solution?
Soluzione
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow