【问题标题】:How to correctly use ellipsis in nested function如何在嵌套函数中正确使用省略号
【发布时间】:2013-11-03 10:18:36
【问题描述】:

editfirst

我有一个父函数和一个子函数。我想在我的子函数中使用... 功能。具体来说,我想知道如何在子函数的这些特定代码行中正确使用...

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]]))

... 应该是一个列表列表,即... 列表的每个元素都是一个列表。 在下面我的主要(主)函数中,... 将包含SharpListSortinoList

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)}}

现在我收到了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'

最好的问候!

EDIT

找到解决办法:

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]]))

但是有人有更好的解决方案吗?

【问题讨论】:

  • @fotNelton Well...找到了解决方案 CombinedList=lapply(seq_along(theList), function (x,...) { .x
  • 请将其发布为答案而不是评论。
  • 您似乎只想引起已经使用该组合包的人的注意,因为您没有提供特定的测试用例。
  • 问题出在base,但感谢您的关注

标签: r parent-child ellipsis


【解决方案1】:
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]]))

【讨论】:

    猜你喜欢
    • 2019-01-28
    • 1970-01-01
    • 2015-02-11
    • 2017-03-02
    • 2013-03-06
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    相关资源
    最近更新 更多