结果为ggplot在r闪亮

时间:2018-04-07 06:46:49

标签: r plot ggplot2 shiny visualization

为什么输出中没有显示ggplot的输出图?以下代码有什么问题?它不会产生任何错误,但确实显示了ui中的ggplot图。由于我是初学者,因此数据流非常困难

enter image description here

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

1 个答案:

答案 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_historydata())(并且可以删除)。