I have a shiny app that uses a reactive expression to respond to a user selecting a value from a drop-down list.
The value chosen is then used as a filter condition on a data frame.
If I want to create two different plots of the same filtered data frame, do I have to call the reactive expression twice, once for each renderPlot
function (as in the example code below), or is there a way to store the results of a filtered data frame for use by different renderPlot
calls?
server.R
library(shiny)
library(dplyr)
library(ggplot2)
shinyServer(function(input, output) {
## create dummy data
set.seed(1)
location <- c("locA", "locB", "locC")
location <- sample(location, 20, replace=TRUE)
service <- c("serviceA", "serviceB")
service <- sample(service, 20, replace=TRUE)
person <- c("A", "B")
person <- sample(person, 20, replace=TRUE)
value <- runif(20, 0, 5)
df <- data.frame(location, service, person, value)
## reactive to user input
select <- reactive({
input$select
})
## plot 1
output$plot1 <- renderPlot({
## filter data frame for use in first plot
df <- df %>%
filter(person==select()) ## select() - calls reactive expression
ggplot(data=df, aes(x=location, y=value, fill=person)) +
geom_bar(stat="identity")
})
## plot 2
output$plot2 <- renderPlot({
## filter data frame for use in second plot
## this is the same data as in plot1
df <- df %>%
filter(person==select()) ## select() - calls reactive expression
ggplot(data=df, aes(x=location, y=value, fill=person)) +
geom_bar(stat="identity") +
facet_wrap(~service)
})
})
ui.R
library(shiny)
shinyUI(navbarPage("Test",
tabPanel("panel",
sidebarLayout(
sidebarPanel(
selectInput("select", label="select", choices=c("A", "B"))
),
mainPanel(
plotOutput("plot1"),
hr(),
plotOutput("plot2")
)
)
)
)
)
This is just a basic example; my actual application will use the same calculated and filtered data frame in many different plots, so I'd like it not to have to re-run the filter condition for each plot.
So the flow of information would be:
Select_Input > filter_data > (plot1, plot2, plot3, ... plotn)