简体   繁体   中英

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM