Add/remove input fields dynamically by a button in shiny AND keep values

2.6k views Asked by At

my question is a followup question on the following discussion:

How to add/remove input fields dynamically by a button in shiny

I want a be able to add/remove dynamically input with action button on shiny app, but also when I add a new input I want the values of the input fields to remain instead of change as it is now. Can you help me with this?

For example if I change the first box and add another text input via the button, the value of the first box has been reset with the default one.

library(shiny)

ui <- shinyUI(fluidPage(

  sidebarPanel(

      actionButton("add_btn", "Add Textbox"),
      actionButton("rm_btn", "Remove Textbox"),
      textOutput("counter")

    ),

  mainPanel(uiOutput("textbox_ui"))

))

server <- shinyServer(function(input, output, session) {

  # Track the number of input boxes to render
  counter <- reactiveValues(n = 0)

  observeEvent(input$add_btn, {counter$n <- counter$n + 1})
  observeEvent(input$rm_btn, {
    if (counter$n > 0) counter$n <- counter$n - 1
  })

  output$counter <- renderPrint(print(counter$n))

  textboxes <- reactive({

    n <- counter$n

    if (n > 0) {
      lapply(seq_len(n), function(i) {
        textInput(inputId = paste0("textin", i),
                  label = paste0("Textbox", i), value = "Hello World!")
      })
    }

  })

  output$textbox_ui <- renderUI({ textboxes() })

})

shinyApp(ui, server)
1

There are 1 answers

1
SBista On

There might be better solution but this also does the work:

library(shiny)

ui <- shinyUI(fluidPage(
  
  sidebarPanel(
    actionButton("add_btn", "Add Textbox"),
    actionButton("rm_btn", "Remove Textbox"),
    textOutput("counter")
    
  ),
  
  mainPanel(uiOutput("textbox_ui"))
  
))

server <- shinyServer(function(input, output, session) {
  
  # Track the number of input boxes to render
  counter <- reactiveValues(n = 0)
  
  #Track the number of input boxes previously
  prevcount <-reactiveValues(n = 0)
  
  observeEvent(input$add_btn, {
        counter$n <- counter$n + 1
        prevcount$n <- counter$n - 1})
  
  observeEvent(input$rm_btn, {
    if (counter$n > 0) {
      counter$n <- counter$n - 1 
      prevcount$n <- counter$n + 1
    }
     
  })
  
  output$counter <- renderPrint(print(counter$n))
  
  textboxes <- reactive({
    
    n <- counter$n
    
    if (n > 0) {
      # If the no. of textboxes previously where more than zero, then 
      #save the text inputs in those text boxes 
      if(prevcount$n > 0){
        
         vals = c()
        if(prevcount$n > n){
          lesscnt <- n
          isInc <- FALSE
        }else{
          lesscnt <- prevcount$n
          isInc <- TRUE
        }
        for(i in 1:lesscnt){
          inpid = paste0("textin",i)
         vals[i] = input[[inpid]] 
        }
        if(isInc){
          vals <- c(vals, "New text box")
        }
        
        lapply(seq_len(n), function(i) {
          textInput(inputId = paste0("textin", i),
                    label = paste0("Textbox", i), value = vals[i])
        })
        
      }else{
        lapply(seq_len(n), function(i) {
          textInput(inputId = paste0("textin", i),
                    label = paste0("Textbox", i), value = "New text box")
        }) 
      }
      
    }
    
  })
  
  output$textbox_ui <- renderUI({ textboxes() })
  
})

shinyApp(ui, server)