Quantmod AddLines()错误:找不到对象

时间:2019-01-21 07:47:07

标签: r charts shiny global-variables quantmod

最近,我一直在尝试将Quantmod的addLines()函数与Shiny软件包一起使用,以将技术分析垂直线覆盖在chartSeries上。但是,当我定义全局变量(在这种情况下为followThroughDays和distributionDays)并将它们用作addLines的“ v”参数的参数时,会出现如下错误消息:

找不到对象'followThroughDays'

找不到对象“ distributionDays”

 followThroughDays <- 2
 distributionDays <- 3

    output$plot <- 
        renderPlot({
          filtered_data <- window(stock_data, start = graph_start, end = state$progress)
          #flags <- getFollowThroughDaysRowNumber(filtered_data)
            switch(
                input$chartType, 
                "candle_stick" = chartSeries(filtered_data, TA=list(
                  "addLines(v=followThroughDays, on=-1, col='grey')",
                  "addLines(v=distributionDays, on=-1, col='orange')"
                )))})

我应该怎么做才能使addLines参数可以访问用于绘制线条的全局变量?当我明确声明存储在变量中的值(例如2或3)时,代码会起作用,但是当我直接将变量用作参数时,则会显示错误消息。

可再现的错误下载:https://drive.google.com/open?id=1ix81cd9gdJG6nXMM1v1WYwBE2nV0loPy

1 个答案:

答案 0 :(得分:1)

您必须通过使用paste0创建TA参数值来克服此错误。

library("shiny")
library("ggplot2")
library("shinythemes")
library("lubridate")
library("quantmod")
library("data.table")

#source("dtstore.R")

fetchedStockData <- getSymbols("IBM", auto.assign = FALSE)

ui <- 
    fluidPage(
      plotOutput("plot")
        )

followThroughDays <- 300

server <- function(input, output, session) {
  TADays <- 3
  output$plot <- renderPlot({    
    chartSeries(fetchedStockData, theme = chartTheme("white"),
                type = "line", TA = paste0("addLines(v=",followThroughDays,", on=-1, col='orange')"))
  })
}

shinyApp(ui, server)

enter image description here