我试图在一个光亮的应用程序中使用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")
))))
谢谢。
答案 0 :(得分:2)
您的代码需要进行一些更改。首先,当您在server.R
中访问闪亮的UI对象时,应将其用作对象而不是带引号的字符
price <- getSymbols(input$stockInput,from='2017-01-01')
没有设置参数(getSymbols
的值)的函数auto.assign = F
在需要其数据的股票名称中创建一个新的xts对象,因此在下面的代码中,我将其与设置auto.assign = F
,以便更容易访问对象price
进行绘图。否则,您可能必须使用price
在get()
中获取值,然后按照我的评论进行绘制。
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
})})
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")
))))
希望它能澄清!