I am working on a project that will allow the user to specify certain inputs and then generate some tables and plots. The user selects some options and various outputs are generated. I want to allow the user to modify the inputs and then easily compare the outputs from the various ‘runs’.
The output results are easily grouped into three categories, so I have three tabset panels, and each time the user hits a run button, a new tab with the new tables/plots will be generated.
When the user selects run, the new tables and tabs are created and show up. The problem is that all the plots in the previous tabs are overwritten, and so the tabs all look identical.
Please see the working example below that has the same issue:
library(shiny)
ui <- navbarPage("Demo", theme = shinytheme("lumen"),
tabPanel( "Input Info",
numericInput("plotNum", "Enter number of plots",
min = 1, max = 10, value = 2),
actionButton("run_button", "Create Plots", icon = icon("play")),
),
tabPanel("Plots",
tabsetPanel(id = "plots", type = "tabs"),
),
)
server <- function(input, output) {
## Reactive value to keep track of the set
## Reactive to keep track of which set (and tab)
plotKeep <- reactiveValues(plotSetNum = 0)
observeEvent(input$run_button, {
## Update number each time plots are requested
plotKeep$plotSetNum = plotKeep$plotSetNum + 1
### Create plots with individual names
for(k in 1:input$plotNum){
local({
plotN <- k
tempPlotname <- paste("toPlot", plotN, "_", plotKeep$plotSetNum, sep="")
output[[tempPlotname]] <- renderPlot({
hist(rnorm(1000, mean = (plotN-1)*10, sd = plotN),
main = paste0("Set ", plotKeep$plotSetNum, ", number ", plotN))
})
})
} ## End creation of plot
### New tab each time you request plots, show UI
appendTab(inputId = "plots",
tabPanel(title = paste("Plot Set =", plotKeep$plotSetNum),
value = toString(plotKeep$plotSetNum),
uiOutput(paste0("myPlot_", plotKeep$plotSetNum))
)
) ## End append tab
## Generate UI object with individual plots
output[[paste0("myPlot_", plotKeep$plotSetNum)]] =
renderUI({plot_output_list <- lapply(1:input$plotNum, function(j) {
outPlotName <- paste0('toPlot', j, '_', plotKeep$plotSetNum)
plotOutput(outPlotName)})
do.call(tagList, plot_output_list)}) ## End UI generation
}) ## End observe
}
shinyApp(ui = ui, server = server)
Please let me know if I am out of luck, or if I can get to my goal.
Aggie Kidd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.