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

时间:2018-09-03 22:03:24

标签: r plot shiny quantmod

我试图在一个光亮的应用程序中使用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")
))))

谢谢。

1 个答案:

答案 0 :(得分:2)

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

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

没有设置参数(getSymbols的值)的函数auto.assign = F在需要其数据的股票名称中创建一个新的xts对象,因此在下面的代码中,我将其与设置auto.assign = F,以便更容易访问对象price进行绘图。否则,您可能必须使用priceget()中获取值,然后按照我的评论进行绘制。

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

希望它能澄清!