【问题标题】:Shiny app: Can't plot stock chart based on user input闪亮的应用程序:无法根据用户输入绘制股票图表
【发布时间】:2019-02-08 21:57:58
【问题描述】:

我正在尝试在闪亮的应用程序中使用 quantmod 绘制股票图表,但出现以下错误:两次尝试后 input$stockInput 下载失败。错误消息:HTTP 错误 404。感谢任何帮助。

服务器:

library(quantmod)

shinyServer(function(input, output) {

  output$distPlot <- renderPlot({

    price <- getSymbols('input$stockInput',from='2017-01-01')
    plot(price)

})})

用户界面:

library(shiny)

shinyUI(fluidPage(


  titlePanel("Stock Chart"),


  sidebarLayout(
    sidebarPanel(

       #This is a dropdown to select the stock
       selectInput("stockInput", 
                   "Pick your stock:", 
                   c("AMZN","FB","GOOG","NVDA","AAPL"),
                   "AMZN"),selected = "GOOG"),

    # Show a plot of the generated distribution
    mainPanel(
       plotOutput("distPlot")
))))

谢谢。

【问题讨论】:

    标签: r plot shiny quantmod


    【解决方案1】:

    您的代码需要进行一些更改。首先,当您在server.R 中访问闪亮的 UI 对象时,您应该将其用作对象而不是引用字符

    price &lt;- getSymbols(input$stockInput,from='2017-01-01')

    并且函数 getSymbols 没有为参数设置值 (auto.assign = F) 在股票名称中创建一个新的 xts 对象,该对象的数据被请求,因此在下面的代码中我使用它来设置 auto.assign = F因此更容易访问对象price 进行绘图。否则,您可能必须使用get() 获取price 中的值,然后按照我评论的方式绘制它们。

    服务器.R

    library(quantmod)
    
    shinyServer(function(input, output) {
    
      output$distPlot <- renderPlot({
    
        price <- getSymbols(input$stockInput,from='2017-01-01', auto.assign = F)
        #plot(get(price), main = price) #this is used when auto.assign is not set by default which is TRUE
        plot(price, main = input$stockInput) # this is when the xts object is stored in the name price itself
    
      })})
    

    ui.R

    library(shiny)
    
    shinyUI(fluidPage(
    
    
      titlePanel("Stock Chart"),
    
    
      sidebarLayout(
        sidebarPanel(
    
          #This is a dropdown to select the stock
          selectInput("stockInput", 
                      "Pick your stock:", 
                      c("AMZN","FB","GOOG","NVDA","AAPL"),
                      "AMZN"),selected = "GOOG"),
    
        # Show a plot of the generated distribution
        mainPanel(
          plotOutput("distPlot")
        ))))
    

    希望它澄清!

    【讨论】:

      猜你喜欢
      • 2018-04-30
      • 2021-08-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      • 2014-04-11
      • 2020-10-06
      • 2023-03-07
      • 1970-01-01
      相关资源
      最近更新 更多