闪亮的应用程序:无法根据用户输入绘制股票图表

问题描述 投票:0回答:1

我试图在一个闪亮的应用程序中使用quantmod绘制股票图表,但我得到以下错误:两次尝试后输入$ 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个回答
2
投票

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

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

并且没有设置参数值(getSymbols)的函数auto.assign = F在库存名称中创建了一个新的xts对象,其数据被请求,因此在下面的代码中我使用它设置auto.assign = F,因此更容易访问对象price用于绘图。否则,您可能需要使用price获取get()中的值,然后按照我的评论绘制它们。

server.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")
    ))))

希望它澄清!

© www.soinside.com 2019 - 2024. All rights reserved.