我面临的任务是创建一个反应性文本输入表单。根据单选按钮选择的值,将显示一个或两个textInput字段。到现在为止还挺好。但是当从2个文本输入更改为1时,将预先设置第二个输入的值。我尝试使用#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)
}
删除textInput值,但它没有按预期工作。这是我使用的代码:
create-react-app
答案 0 :(得分:2)
这是使用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)
}