[英]Append Radio Button inputs on UI upon User textInput in R shiny
[英]How to delete a textInput upon a radioButtons click in R shiny
我面临的任务是创建一个反应式文本输入表单。 根据单选按钮选择的值,将显示一个或两个textInput字段。 到现在为止还挺好。 但是,当从2个文本输入更改为1个文本输入时,将保留第二个输入的值。 我试图用observe()
删除textInput值,但是没有按预期工作。 这是我使用的代码:
#library(shiny)
if (interactive()) {
ui <- fluidPage(
radioButtons("controller", "No. inputs", choices = c(1, 2), selected = 2),
uiOutput("text_fields"),
verbatimTextOutput("test")
)
server <- function(input, output, session) {
output$text_fields <- renderUI({
if(input$controller == 2){
fluidRow(
column(6, textInput("inText_1", label = "Input text 1", value = "")),
column(6, textInput("inText_2", label = "Input text 2", value = ""))
)
}else{
fluidRow(textInput("inText_1", label = "Input text 1", value = ""))
}
})
output$test <- renderText(c(input$inText_1, input$inText_2))
observe({
# The input$controller variable needs to be used inside the observe, correct?
x <- input$controller
# This will delete the values of text input, based on change of x
updateTextInput(session, "inText_1", value = "")
updateTextInput(session, "inText_2", value = "")
})
}
shinyApp(ui, server)
}
这是使用conditionalPanel
一个好例子:
library(shiny)
if (interactive()) {
ui <- fluidPage(
radioButtons("controller", "No. inputs", choices = c(1, 2), selected = 2),
fluidRow(
column(6, textInput("inText_1", "Input text 1", value = "")),
column(6, conditionalPanel('input.controller == 2',
textInput("inText_2", "Input text 2", value = "")))
),
verbatimTextOutput("test")
)
server <- function(input, output, session) {
output$test <- renderText(c(input$inText_1, input$inText_2))
observe({
# The input$controller variable needs to be used inside the observe, correct?
x <- input$controller
# This will delete the values of text input, based on change of x
updateTextInput(session, "inText_1", value = "")
updateTextInput(session, "inText_2", value = "")
})
}
shinyApp(ui, server)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.