R 完全复制函数,不管怎样都会出错

R 完全复制函数,不管怎样都会出错,r,quantmod,R,Quantmod,我正试图用quantmod在图表上绘制指标。我想对bbands函数进行一些调整,但即使我完全复制了该函数,仍然会得到一个错误。我的步骤如下: 键入“addBBands” 控制+c代码 键入addBBands2我不知道quantmod,但可能这是因为get.current.chob不是从包名称空间导出的。您可以尝试使用quantmod:::get.current.chob引用它。对于其他未导出的函数,可能会有许多这样的错误。您需要确保函数具有正确的环境设置,以便它可以查看未导出的函数 复制函数(将

我正试图用quantmod在图表上绘制指标。我想对bbands函数进行一些调整,但即使我完全复制了该函数,仍然会得到一个错误。我的步骤如下:

  • 键入“addBBands”
  • 控制+c代码

  • 键入addBBands2我不知道quantmod,但可能这是因为
    get.current.chob
    不是从包名称空间导出的。您可以尝试使用
    quantmod:::get.current.chob
    引用它。对于其他未导出的函数,可能会有许多这样的错误。

    您需要确保函数具有正确的
    环境设置,以便它可以查看未导出的函数

    复制函数(将保留环境):


    不要那样做。改用
    newTA
    Error in addBBands2() : could not find function "get.current.chob"
    
    addBBands2<- function (n = 20, sd = 2, maType = "SMA", draw = "bands", on = -1) 
    function (n = 20, sd = 2, maType = "SMA", draw = "bands", on = -1) 
    {
        stopifnot("package:TTR" %in% search() || require("TTR", quietly = TRUE))
        draw.options <- c("bands", "percent", "width")
        draw <- draw.options[pmatch(draw, draw.options)]
        lchob <- get.current.chob()
        x <- as.matrix(lchob@xdata)
        chobTA <- new("chobTA")
        if (draw == "bands") {
            chobTA@new <- FALSE
        }
        else {
            chobTA@new <- TRUE
            on <- NULL
        }
        xx <- if (is.OHLC(x)) {
            cbind(Hi(x), Lo(x), Cl(x))
        }
        else x
        bb <- BBands(xx, n = n, maType = maType, sd = sd)
        chobTA@TA.values <- bb[lchob@xsubset, ]
        chobTA@name <- "chartBBands"
        chobTA@call <- match.call()
        chobTA@on <- on
        chobTA@params <- list(xrange = lchob@xrange, colors = lchob@colors, 
            color.vol = lchob@color.vol, multi.col = lchob@multi.col, 
            spacing = lchob@spacing, width = lchob@width, bp = lchob@bp, 
            x.labels = lchob@x.labels, time.scale = lchob@time.scale, 
            n = n, ma = maType, sd = sd, draw = draw)
        return(chobTA)
    }
    
    > str(addBBands)
    function (n = 20, sd = 2, maType = "SMA", draw = "bands", on = -1)  
    > str(addBBands2)
    function (n = 20, sd = 2, maType = "SMA", draw = "bands", on = -1)  
     - attr(*, "srcref")=Class 'srcref'  atomic [1:8] 1 14 31 1 14 1 1 31
      .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x25eb9bf4> 
    
    addBBands2 <- addBBands
    
    f <- function() get.current.chob()
    f()
    Error in f() : could not find function "get.current.chob"
    environment(f) <- environment(addBBands)
    f()
    An object of class "chob"
    Slot "device":
    ...