为什么输出中没有显示ggplot的输出图?以下代码有什么问题?它不会产生任何错误,但确实显示了ui中的ggplot图。由于我是初学者,因此数据流非常困难
ui.R
library(shiny)
library(DT)
library(ggplot2)
shinyUI(fluidPage(
titlePanel("Stock prediction"),
sidebarLayout(
sidebarPanel(
textInput(inputId = "stock_name",label = "Enter the stock name",value = "MSFT"),
textInput(inputId = "stock_history",label = "How many days back would you like your data to go back to help make the prediction?",value = "compact")
),
mainPanel(
h2("Stock data"),
DT::dataTableOutput("data_table"),
renderPlot("plot_high")
)
)
))
server.R
library(DT)
library(shiny)
library(alphavantager)
library(ggplot2)
data <- function(stock_name="",days_history){
av_api_key("YOUR_API_KEY")
sri <- data.frame(av_get(symbol = stock_name, av_fun = "TIME_SERIES_DAILY", interval = "15min", outputsize = "compact"))
sri
}
visualization <- function(sri){
high_vis <- ggplot(aes(x = timestamp, y = high),data = sri) + geom_freqpoly(stat = "identity") +
labs(title = "High price vs Time",x = "Timeline in months",y = "Share price - High") +
theme_classic(base_size = 20)
high_vis
}
shinyServer(function(input, output) {
output$data_table <- renderDataTable({data(input$stock_name,input$stock_history)})
output$plot_high <- renderPlot({visualization(data_table)})
})
答案 0 :(得分:2)
这应该有效。
替换
renderPlot("plot_high")
由plotOutput("plot_high")
在ui.R visualization(data_table)
by
server.R中的visualization(data(stock_name=input$stock_name, input$stock_history))
您可能还想删除DT::
中的DT::dataTableOutput("data_table")
。出于某种原因,来自dataTableOutput
包的DT
函数对我不起作用(因此我使用相同的函数,但直接来自shiny
)。
修改强>
您可能更清楚地将shinyServer
重写为:
shinyServer(function(input, output) {
data_table <- reactive({data(stock_name=input$stock_name, input$stock_history)})
output$data_table <- renderDataTable({data_table()})
output$plot_high <- renderPlot({visualization(data_table())})
})
请注意,在input$stock_history
函数提供的代码中未使用days_history
(data()
)(并且可以删除)。