【问题标题】:plotting stocks with shiny绘制有光泽的股票
【发布时间】:2020-03-22 15:35:34
【问题描述】:

您好,我是 r shiny 的新手,我正在尝试创建一个应用程序来绘制 r 中 quantmod 中的股票 像这样

这是我的代码

library(shiny)

server = function(input, output, session) {
  output$plot <- renderPlot({
    data <- getSymbols(input$stock, 
                       from = input$date[1],
                       to = input$date[2]
                      )

    chartSeries(data, theme = chartTheme("white"),
                type = "line", log.scale = input$log, TA = NULL)
  })
} # the server

ui = basicPage(
  h1("stock app"),
  textInput("stocks", "pick stock"),
  dateRangeInput("date", "date range ", start = "2013-01-01", end = "2020-03-15",min = "2007-01-01", max = "2020-03-15",format = "yyyy-mm-dd" ),
  plotOutput("plot")
) # the user interface

shinyApp(ui = ui, server = server) # perform app launch

但是,我的应用程序没有绘制股票系列,而是返回了这样的错误

错误:chartSeries 需要一个 xtsible 对象。

我想知道为什么我的应用程序没有在我的输入中绘制股票

【问题讨论】:

标签: r shiny


【解决方案1】:

这是一个工作版本,需要进行一些小改动(参见下面的 cmets)。

您的input$stocks 需要与ui 中的inputId 匹配。 server 中缺少一个“s”。

getSymbols 中需要 auto.assign = FALSE,因为默认情况下数据会转到 parent.frame。

我向textInput 添加了默认库存,这样您在启动时就不会收到错误消息。

chartSeries 引用 input$log 但 ui 中没有匹配的输入。为此添加了一个复选框。

library(shiny)
library(quantmod)

server = function(input, output, session) {
  output$plot <- renderPlot({
    data <- getSymbols(input$stocks,  # needs to match textInput, missing s
                       from = input$date[1],
                       to = input$date[2],
                       auto.assign = FALSE   # getSymbols returns data to parent.frame by default
    )
    chartSeries(data, theme = chartTheme("white"),
                type = "line", log.scale = input$log, TA = NULL) # no input for "log", needs to be added to ui
  })
} # the server

ui = basicPage(
  h1("stock app"),
  textInput("stocks", "pick stock", "AAPL"),   # added default stock
  dateRangeInput("date", "date range ", start = "2013-01-01", end = "2020-03-15",min = "2007-01-01", max = "2020-03-15",format = "yyyy-mm-dd" ),
  checkboxInput(inputId = "log", label = "log y axis", value = FALSE),  # added "log" input
  plotOutput("plot")
) # the user interface

shinyApp(ui = ui, server = server) # perform app launch

【讨论】:

    猜你喜欢
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多