Interactive shiny app // Error:Discrete value supplied to continuous scale
Interactive shiny app // Error:Discrete value supplied to continuous scale
我使用以下代码从 quantmod 下载了一些财务数据(2016 年每一天的股票收盘价):
library(quantmod)
DJ30_smbl_list = c("AAPL", "AXP", "BA", "CAT", "CSCO", "CVX","DWDP", "DIS", "GE",
"GS", "HD", "IBM", "INTC", "JNJ", "JPM", "KO", "MCD", "MMM",
"MRK", "MSFT", "NKE", "PFE", "PG", "TRV", "UNH", "UTX", "V",
"VZ", "WMT", "XOM")
# use a for loop to import closing price to DJIA
DJIA <- c()
for(j in DJ30_smbl_list){
DJIA<- cbind(DJIA, getSymbols(j, auto.assign = F, from = "2016-01-01", to = "2017-01-01")[,4])
}
colnames(DJIA) = DJ30_smbl_list
我正在尝试创建一个交互式闪亮应用程序,我在其中从 DJIA (.xts) 中挑选了 2 只股票,并使用以下代码获取了它们的相关图:
ui=fluidPage(
titlePanel("Correlation of Stocks"),
selectInput(inputId="stock",label="Choose stock 1:",c("AAPL", "AXP", "BA", "CAT", "CSCO", "CVX", "DWDP", "DIS", "GE",
"GS", "HD", "IBM", "INTC", "JNJ", "JPM", "KO", "MCD", "MMM",
"MRK", "MSFT", "NKE", "PFE", "PG", "TRV", "UNH", "UTX", "V",
"VZ", "WMT", "XOM")),
selectInput(inputId="stock2",label="Choose stock 2:",c("AAPL", "AXP", "BA", "CAT", "CSCO", "CVX", "DWDP", "DIS", "GE",
"GS", "HD", "IBM", "INTC", "JNJ", "JPM", "KO", "MCD", "MMM",
"MRK", "MSFT", "NKE", "PFE", "PG", "TRV", "UNH", "UTX", "V",
"VZ", "WMT", "XOM")),
plotOutput(outputId="scatter"))
server=function(input,output){
output$scatter<-renderPlot({ggplot(DJIA, aes(input$stock, input$stock2)) + geom_point() + scale_x_continuous(input$stock, breaks = seq(0, 150,10))+ scale_y_continuous(input$stock2, breaks = seq(0, 150,by = 10))+ theme_bw()+labs(title="STOCKS")})
}
shinyApp(ui=ui,server=server)
```
在那之后,我得到了标题中写的错误。你能帮帮我吗?提前致谢
你忘了ggplot
美学中的get
。
你的渲染函数应该如下:
server=function(input,output){
output$scatter<-renderPlot({
ggplot(DJIA, aes(get(input$stock), get(input$stock2))) +
geom_point() +
scale_x_continuous(input$stock, breaks = seq(0, 150,10))+
scale_y_continuous(input$stock2, breaks = seq(0, 150,by = 10))+
theme_bw()+
labs(title="STOCKS")})
我使用以下代码从 quantmod 下载了一些财务数据(2016 年每一天的股票收盘价):
library(quantmod)
DJ30_smbl_list = c("AAPL", "AXP", "BA", "CAT", "CSCO", "CVX","DWDP", "DIS", "GE",
"GS", "HD", "IBM", "INTC", "JNJ", "JPM", "KO", "MCD", "MMM",
"MRK", "MSFT", "NKE", "PFE", "PG", "TRV", "UNH", "UTX", "V",
"VZ", "WMT", "XOM")
# use a for loop to import closing price to DJIA
DJIA <- c()
for(j in DJ30_smbl_list){
DJIA<- cbind(DJIA, getSymbols(j, auto.assign = F, from = "2016-01-01", to = "2017-01-01")[,4])
}
colnames(DJIA) = DJ30_smbl_list
我正在尝试创建一个交互式闪亮应用程序,我在其中从 DJIA (.xts) 中挑选了 2 只股票,并使用以下代码获取了它们的相关图:
ui=fluidPage(
titlePanel("Correlation of Stocks"),
selectInput(inputId="stock",label="Choose stock 1:",c("AAPL", "AXP", "BA", "CAT", "CSCO", "CVX", "DWDP", "DIS", "GE",
"GS", "HD", "IBM", "INTC", "JNJ", "JPM", "KO", "MCD", "MMM",
"MRK", "MSFT", "NKE", "PFE", "PG", "TRV", "UNH", "UTX", "V",
"VZ", "WMT", "XOM")),
selectInput(inputId="stock2",label="Choose stock 2:",c("AAPL", "AXP", "BA", "CAT", "CSCO", "CVX", "DWDP", "DIS", "GE",
"GS", "HD", "IBM", "INTC", "JNJ", "JPM", "KO", "MCD", "MMM",
"MRK", "MSFT", "NKE", "PFE", "PG", "TRV", "UNH", "UTX", "V",
"VZ", "WMT", "XOM")),
plotOutput(outputId="scatter"))
server=function(input,output){
output$scatter<-renderPlot({ggplot(DJIA, aes(input$stock, input$stock2)) + geom_point() + scale_x_continuous(input$stock, breaks = seq(0, 150,10))+ scale_y_continuous(input$stock2, breaks = seq(0, 150,by = 10))+ theme_bw()+labs(title="STOCKS")})
}
shinyApp(ui=ui,server=server)
```
在那之后,我得到了标题中写的错误。你能帮帮我吗?提前致谢
你忘了ggplot
美学中的get
。
你的渲染函数应该如下:
server=function(input,output){
output$scatter<-renderPlot({
ggplot(DJIA, aes(get(input$stock), get(input$stock2))) +
geom_point() +
scale_x_continuous(input$stock, breaks = seq(0, 150,10))+
scale_y_continuous(input$stock2, breaks = seq(0, 150,by = 10))+
theme_bw()+
labs(title="STOCKS")})