Reputation: 143
I would like to build a shiny app, that runs a simulation and then shows the 3 result plots (I have set them up with tabsetPanel()
). The code needs a few seconds to run, so I made a submitButton()
. Now as the code takes longer, I want it to run only once and then display the 3 plots.
How do I run the code with the input values, extract the 5 result vectors and only then plot them?
Currently shiny gives an error, because when the code is not within the output$plot <- renderPlot({ ...})
part, it is not reactive.
What I have so far looks like this:
ui <- fluidPage(
titlePanel("My Simulation"),
submitButton(text="RUN SIMULATION")
mainPanel(
tabsetPanel(
tabPanel("Plot 1", plotOutput("p1")),
tabPanel("Plot 2", plotOutput("p2")),
tabPanel("Plot 3", plotOutput("p3"))
)
)
)
server <- function(input, output) {
v<-reactiveValues(v1=0,v2=0,v3,v4=0,v5=0)
observeEvent(input,{ myFunction()})
output$p1<-renderPlot{
plot(v1,v2)
}
output$p2<-renderPlot{
plot(v1,v3)
}
output$p3<-renderPlot{
plot(v1,v4)
lines(v1,v4)
}
shinyApp(ui, server)
Upvotes: 0
Views: 181
Reputation: 17699
Actually it is seperated. To make the plot reactive you have to reference the reactiveValues()
as v$v1
, v$v2
,...
It was assigned to v
so you would also have to reference it that way. You can actually compare the reactiveValues()
to a data.frame()
or more like a list that is reactive.
A list v <- list(v1 = 1)
you would also call with v$v1
,...
Reproducible example:
library(shiny)
ui <- fluidPage(
titlePanel("My Simulation"),
actionButton(inputId = "simulate", label = "simulate"),
mainPanel(
tabsetPanel(
tabPanel("Plot 1", plotOutput("p1")),
tabPanel("Plot 2", plotOutput("p2")),
tabPanel("Plot 3", plotOutput("p3"))
)
)
)
server <- function(input, output) {
v <- reactiveValues(v1 = 0, v2 = 0, v3 = 0, v4 = 0, v5 = 0)
observeEvent(input$simulate,{
v$v1 <- 1
v$v2 <- 2
v$v3 <- 3
v$v4 <- 4
})
output$p1<-renderPlot({
plot(v$v1, v$v2)
})
output$p2<-renderPlot({
plot(v$v1, v$v3)
})
output$p3<-renderPlot({
plot(v$v1, v$v4)
lines(v$v1, v$v4)
})
}
shinyApp(ui, server)
Upvotes: 1