简体   繁体   中英

Reactive plots in shiny with multiple datasets

I'm trying to produce a reactive UI in shiny.

I have a dataset consisting in supermarket products daily sales over a period of 2 years. For instance:

    product                   Total_Sales               Date(YYYY-MM-DD)

1. 'Coke 0.5 L'              23                      2014-01-02  
2. 'Sprite 0.5 L'            18                      2014-01-02  
3. 'CornFlakes 1.kg'         21                      2014-01-02  
4. 'Coke 0.5 L'              25                      2014-01-03   
5. 'BurgersX6 1.kg'           8                      2014-01-03  
6. 'CornFlakes 1.kg'         17                      2014-01-03

I want my user to select a product as input and, given the input, plot the corresponding time_series.

selectInput("product", "Choose a product:", 
              choices = as.factor(levels(df$product)))

And this works fine.

In server:

datasetInput <- df
output$tsplot <- reactive({renderPlot(
    {dataset <- datasetInput[product==input$product,]
    plot(as.ts(dataset$Total_Sales,dataset$Date),start=input$start,end=input$end")
    }
  )
  })  

Unfortunately I don't get any plot out. If instead I put a reactive input datasetInput <- df[product==input$product,] and change the output$tsplot accordingly I get as an output the first available input, but when I change the input$product the plot remains the same. I'm pretty new to shiny, any help is much welcomed, thank you

Try to filter your input dataframe in a reactive expression and then call the result in renderPlot :

datasetInput <- reactive({
  df[df$product == input$product, ]
}) 

output$tsplot <- renderPlot({
  dataset <- datasetInput()
  plot(as.ts(dataset$Total_Sales, dataset$Date, start = input$start, end = input$end))
})

EDIT :

With your data :

df <- read.table(
  text = "    Name                   Total_Sales               Date
  1. 'Coke 0.5 L'              23                      2014-01-02  
  2. 'Sprite 0.5 L'            18                      2014-01-02  
  3. 'CornFlakes 1.kg'         21                      2014-01-02  
  4. 'Coke 0.5 L'              25                      2014-01-03   
  5. 'BurgersX6 1.kg'           8                      2014-01-03  
  6. 'CornFlakes 1.kg'         17                      2014-01-03"
)
df$Date <- as.Date(as.character(df$Date, format = "%Y-%m-%d"))


library("shiny")
ui <- fluidPage(
  selectInput("product", "Choose a product:", 
              choices = levels(df$Name)),
  plotOutput(outputId = "tsplot")
)
server <- function(input, output){
  datasetInput <- reactive({
    df[df$Name == input$product, ]
  }) 

  output$tsplot <- renderPlot({
    dataset <- datasetInput()
    plot(as.ts(dataset$Total_Sales, dataset$Date))
  })
}
shinyApp(ui = ui, server = server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM