Reputation: 182
Why does the output plot for the ggplot is not showing in the output?What is the problem with the following code? It doesn't produce any error but it does shows the ggplot graph in ui.In r shiny the data flow is very difficult, since i'm a beginner
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)})
})
Upvotes: 1
Views: 191
Reputation: 4940
This should work.
Replace
renderPlot("plot_high")
by plotOutput("plot_high")
in ui.Rvisualization(data_table)
by
visualization(data(stock_name=input$stock_name, input$stock_history))
in server.RYou may also want to remove DT::
in DT::dataTableOutput("data_table")
. For some reason, dataTableOutput
function from DT
package is not working for me (so I used the same function but directly from shiny
).
EDIT
It might be clearer to you to rewrite shinyServer
as:
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())})
})
Note that input$stock_history
(days_history
) is not used (and could be removed) in the provided code for data()
function.
Upvotes: 2