Recently I have been trying to use the addLines() function of quantmod along with Shiny package to overlay technical analysis vertical lines over the chartSeries. However, when I define global variables (followThroughDays and distributionDays in this case) and use them as arguments of the "v" parameter of addLines, an error message as follows appears:
object 'followThroughDays' not found
object 'distributionDays ' not found
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')"
)))})
What should I do so that the addLines argument can access the global variables for plotting the lines? The code works when I explicitly state the value (eg 2 or 3) stored in the variables but will show error messages when I used the variables as arguments directly.
Reproducible error download: https://drive.google.com/open?id=1ix81cd9gdJG6nXMM1v1WYwBE2nV0loPy
You've to overcome this error by using paste0
to create the TA
argument value.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.