I want to collect individual information (name and age) as user input with the attached demo code. The initial number of individuals is 1, and when the user increases/decreases the value of the number of individuals, it will reactively add/reduce corresponding rows to collect individual information. My question is how to avoid wiping out the current input information when increasing/decreasing the number of individuals. For example, screenshot 1 shows the information of individual 1 entered by the user (me). When I changed the values of the number of individuals to 2, the information I entered before was wiped out (screenshot 2), and I had to re-enter the information for individual 1. What I expect will be like screenshot 3 (information for individual 1 is not wiped out) when I increase the number of individuals to 2. Can anyone help me with this? Thanks!
Update on 05/15/2022
The update answer using isolate is what I am looking for. Thanks again for the help from @jpdugo17.
Example code
library(shiny)
ui <- fluidPage(
tabsetPanel(
tabPanel(
h4("Individual Information"),
fluidRow(column(4,numericInput("ninds",
label = "Number of individuals",
value = 1, min = 1, step = 0.5, width = "300px"))),
br(),
fluidRow(column(2,align = "center",strong("Individual #")),
column(5,align = "center",strong("Individual Name")),
column(5,align = "center",strong("Age"))),
fluidRow(
column(2,wellPanel(uiOutput("indNum"))),
column(5,wellPanel(uiOutput("Name"))),
column(5,wellPanel(uiOutput("Age"))))
)
)
)
server <- function(input, output) {
# create reactive variable paste0("individualNum", i) for further using
output$indNum <- renderUI({
num <- as.integer(input$ninds)
req(num)
lapply(1:num, function(i) {
numericInput(paste0("individualNum", i), value = i, label = "", max = i, min = i)
})
})
# create reactive variable paste0("name", i) for further using
output$Name <- renderUI({
num <- as.integer(input$ninds)
req(num)
lapply(1:num, function(i) {
textInput(paste0("name", i), label = "")
})
})
# create reactie variable paste0("age", i) for further using
output$Age <- renderUI({
num <- as.integer(input$ninds)
req(num)
lapply(1:num, function(i) {
numericInput(paste0("age", i), label = "", value = 0)
})
})
}
# Run the app ----
shinyApp(ui, server)


