I'm trying to design a Shiny app such that a user can click on buttons to add or remove a tabPanel. I've been able to do this successfully with the code below, but there is a problem. In the code below every time the user clicks on Add L2 or Remove L2, the entire panel is recreated, thus resetting all inputs in each tabPanel. This is problematic because the goal is for the user to dynamically add tabPanels and save their inputs. Is there any way to do this?
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(width = 3, fixed=T,
h3("L2 Machine"),
actionButton('moreL2', tags$b('Add L2')),
actionButton('lessL2', tags$b('Remove L2')),
uiOutput('panelset')
),
mainPanel(
)
)
)
server <- function(input, output) {
more <- reactiveValues(i=1)
less <- reactiveValues(i=1)
observe({
input$moreL2
isolate({
more$i <- more$i + 1
})
})
observe({
input$lessL2
isolate({
less$i <- less$i + 1
})
})
output$panelset <- renderUI({
if(less$i > more$i) less$i <- more$i
n <- seq(max(more$i - less$i + 1, 1))
lapply(n, function(i) {
tabPanel(
title = paste0('Pan', i),
numericInput(paste0('L2amount', i), 'Select L2 Amount', value = 0),
selectInput(paste0('L2type', i), 'Select L2 Type', c('Percent', 'Absolute')),
dateRangeInput(paste0('L2daterange',i), 'Select Adjustment Period',
start='01-01-2010', end='01-12-2015'))
})
})
}
shinyApp(ui, server)