I have a set of dynamic UI forms being generated in tabs. I have used dynamic observeEvents to check which form is being saved. Please see the code below
library(shiny)
ui <- fluidPage(
actionButton("generate_tab","Generate Tabs"),
tabsetPanel(id="tabs",
uiOutput('tabsN')),
dataTableOutput("saved_tabs_output")
)
server <- function(input, output, session) {
rv<- reactiveValues(no_of_tabs =0)
#generating the UI dynamically
observeEvent(input$generate_tab,{
rv$no_of_tabs <- rv$no_of_tabs + 1
appendTab(inputId = "tabs",
tabPanel(title = paste0("Tab_",rv$no_of_tabs),
selectInput(paste0("Input",rv$no_of_tabs),paste0("Input",rv$no_of_tabs), choices = c('',LETTERS), selected = NULL),
actionButton(paste0("submit_input",rv$no_of_tabs),"submit input")
)
)})
# Reading the inputs upon clicking of Submit Input in each tab
#dynamic Observe Event needs to be set up dependent on number of Tabs (rv$no_of_tabs)
observeEvent(lapply(paste0("submit_input",1:rv$no_of_tabs), function(x){input[[x]]
}
),{
rv$inputs <-sapply(paste0("Input",1:rv$no_of_tabs),
function(x)input[[x]])
},ignoreInit = TRUE )
output$saved_tabs_output <- renderDataTable({
as.data.frame(rv$inputs)
})
}
shinyApp(ui, server)
because multiple submit-button clicks are being checked in one observeEvent lapply formulation, it accepts inputs from non-submitted forms as well. Here is an example
Step 1: Hit Generate tabs 4 times to generate Tab1,Tab2,Tab3,Tab 4. Click Tab 1
Step 2: Select B in Tab 1. Do NOT press Submit
Step 3: Select D in Tab 4 and Hit Submit
Desired output is that only Input4 is updated and not input1, however in this case, both B and D are saved. Any pointers as to how one can fix this?