在 R shiny 中的用户文本输入上附加 UI 上的单选按钮输入

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

我正在尝试从用户那里获取 textInput 并通过操作按钮提交它,我需要更新选项(将用户输入附加到现有的单选按钮),我尝试了几种方法但没有运气好,在此先感谢您的帮助:)

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

目前,应用程序会在第一次将用户输入附加到已经存在的输入列表中,但在用户第二次输入时,它会重写前一个。

试试这个

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)