Dynamic widgets in shiny

I have a sample application as below. The idea is only 2 widgets are seen by default. But when you click on "Add", the widgets keep on adding. But when we click "Add" for the first time, the widgets pops up (that is fine) and now when enter some value in "First Company" and then click on "Add", another widgets pops up(this is also fine). But now, the values in "First Company" disappears. Can we make is appear when we click on "Add"

library(shiny)

ui <- fluidPage(
  uiOutput("fcompany"),
  uiOutput("add_exp"),
  actionButton("add_button", "Add")
)

server <- function(input, output, session) {
  
  get_data <- reactiveValues()
  
  get_data$all_experiences <- c(0) 
  
  output$fcompany <- renderUI({
    column(width = 2, textInput("id_zero", label = "Gra", placeholder = "Col"), 
           numericInput("idexp_zero", label = "", value = 4, min = 0, max = 10))
  })
  
  
  observeEvent(input$add_button,{
    
    added_varaible <- max(get_data$all_experiences)+1
    
    excel_Input <- structure(list(number = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), words = c("one", 
                                                                                       "two", "three", "four", "five", "six", "seven", "eight", "nine", 
                                                                                       "ten"), company = c("First Company", "Second Company", "Third Company", 
                                                                                                           "Fourth Company", "Fifth Company", "Sixth Company", "Seventh Company", 
                                                                                                           "Eighth Company", "Ninth Company", "Tenth Company")), row.names = c(NA, 
                                                                                                                                                                               10L), class = "data.frame")
    output$add_exp <- renderUI({
      lapply(1:added_varaible, function(i){
        column(width = 2, textInput(paste0("id_",excel_Input$words[excel_Input$number == i]), label = excel_Input$company[excel_Input$number == i], 
                                    placeholder = paste0("Enter the ", excel_Input$company[excel_Input$number == i])), 
               numericInput(paste0("idexp_",excel_Input$words[excel_Input$number == i]), label = "", value = 4, min = 0, max = 10))
      })
    })
    
    get_data$all_experiences <- c(get_data$all_experiences, added_varaible)
    
  })
  
}

shinyApp(ui, server)

Hello @vinayprakash808. Below is a slightly reworked example app that saves company inputs when new widgets are added. Two reactiveValues() were created to track 1) company inputs, and 2) the "collection" of widgets. When the "Add" button is clicked, these two objects are updated, as well as a click counter.

library(shiny)

excel_Input <- structure(list(number = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 
                              words = c("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"),
                              company = c("First Company", "Second Company", "Third Company", "Fourth Company", 
                                          "Fifth Company", "Sixth Company", "Seventh Company", "Eighth Company", 
                                          "Ninth Company", "Tenth Company")
                              ),
                         row.names = c(NA,10L), 
                         class = "data.frame")

ui <- fluidPage(
  uiOutput("fcompany"),
  uiOutput("add_exp"),
  actionButton("add_button", "Add")
)

server <- function(input, output, session) {
  
  output$fcompany <- renderUI({
    column(width = 2, textInput("id_zero", label = "Gra", placeholder = "Col"), 
           numericInput("idexp_zero", label = "", value = 4, min = 0, max = 10))
  })
  
  # reactive value to "collect" all widgets
  widget <- reactiveValues(collection = NULL)
  
  # reactive value to "collect" company inputs
  company <<- reactiveValues(inputs = list())
  
  # reactive val to count clicks
  click_count <- reactiveVal(1)
  
  # click "Add" button to, 
  # 1) track companies entered 
  # 2) update the widget collection
  # 3) update click counts
  observeEvent(input$add_button,{
    
    # update company inputs
    lapply(1:(click_count()), function(i){
      out = ''
      value = paste0('input$id_', excel_Input$words[excel_Input$number == i])
      if(!is.null(eval(parse(text = value)))) { out = eval(parse(text = value)) }
      company$inputs[i] <<- out
    })
    
    # function to create widget
    create_widget = function(i){
      column(width = 2,
             textInput(paste0("id_",excel_Input$words[excel_Input$number == i]),
                       label = excel_Input$company[excel_Input$number == i], 
                       value = company$inputs[i],
                       placeholder = paste0("Enter the ", excel_Input$company[excel_Input$number == i])),
             
             numericInput(paste0("idexp_",excel_Input$words[excel_Input$number == i]),
                          label = "",
                          value = 4, min = 0, max = 10)
             )
      }
    
    # update widget collection
    widget$collection <<- lapply(1:click_count(), create_widget)
    
    # update click count
    click_count(click_count() + 1)
    
  })
  
  # render the widget collection
  output$add_exp <- renderUI({
    widget$collection
  })

}

shinyApp(ui, server)

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.