'Append Radio Button inputs on UI upon User textInput in R shiny

I'm trying to get the textInput from the user and upon submitting it through an action button,I need to update the choices (Append the user input to already existing radio buttons) ,I tried couple of ways but no luck, Thanks in advance for your Help:)

library(shiny)


  ui = navbarPage("Sample Trial",
                  tabPanel("Input Tab",
                           mainPanel(
                             textInput("textinp","Create New Label", placeholder = NULL),
                             actionButton("labbutton","Create")
                           )
                           
                  ),
                  tabPanel("Radio Button Panel",
                       radioButtons("labradio", label = "New Label",choices=values)
                  )
  )
  values <- c("Label1","label2","label3")
  server = function(input, output) {
    observeEvent(input$labbutton,{
  req(input$textinp)
  value01 <-input$textinp
  updatedValues <- c(values, value01)
  updateRadioButtons(session,inputId ="labradio",choices=updatedValues)
  
})

    
  }#Server End

Currently the App will append user input at first time to the list of already present inputs but upon the second input from the user ,It rewrites the previous one.



Solution 1:[1]

Try this

library(shiny)

ui = navbarPage("Sample Trial",
                tabPanel("Input Tab",
                         mainPanel(
                           textInput("textinp","Create New Label", placeholder = NULL),
                           actionButton("labbutton","Create")
                         )
                         
                ),
                tabPanel("Radio Button Panel",
                         radioButtons("labradio", label = "New Label",choices=values)
                )
)

server = function(input, output,session) {
  value <- c("Label1","label2","label3")
  rv <- reactiveValues(values=value)
  observeEvent(input$labbutton,{
    req(input$textinp)
    rv$values <- c(rv$values, input$textinp)
    updateRadioButtons(session,inputId ="labradio",choices=rv$values)
  })
  
}

shinyApp(ui, server)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 YBS