繁体   English   中英

R shiny - 根据 select 输入禁用/启用单个单选按钮

[英]R shiny - Disable/enable single radio button based upon select input

我想在选择“B”时禁用“3”并将单选按钮选择移动到“1”。 当再次选择“A”时,我希望再次启用“3”。 我用shinyjs::runjs("")尝试了几件事,但效果不佳。 任何帮助将不胜感激。

library(shiny)
library(shinyjs)
library(shinyWidgets)
if (interactive()) {
  ui <- fluidPage(
    useShinyjs(),
    selectInput(inputId="input", 
                label="choose ",
                c("A" = "a",
                  "B" = "b")),
    radioButtons(inputId="select", 
                 label="number",
                 c("1"="one",
                   "2"="two",
                   "3"="three")),
    mainPanel(verbatimTextOutput("output")
  )
)
  server <- function(input, output) {
    observeEvent(input$input, {
      if(input$input=="b"){
        # disable 3
        shinyjs::runjs("")
      }else{
        # enable 3 again if input$input=="a"
      }
    })
    output$output <- renderText({ input$select })
  }
  shinyApp(ui, server)
}


一种方法是使用conditionPanel 尝试这个。

library(shiny)
library(shinyjs)
library(shinyWidgets)

  ui <- fluidPage(
    useShinyjs(),
    selectInput(inputId="input", 
                label="choose ",
                c("A" = "a",
                  "B" = "b")),
    conditionalPanel(condition = "input.input == 'a'",
                     radioButtons(inputId="select", 
                                  label="number",
                                  c("1"="one",
                                    "2"="two",
                                    "3"="three"),
                                  selected="three")
    ),
    conditionalPanel(condition = "input.input == 'b'",
                     radioButtons(inputId="select2", 
                                  label="number",
                                  c("1"="one",
                                    "2"="two"),
                                  selected="one")
    ),

    mainPanel(verbatimTextOutput("output")
    )
  )
  server <- function(input, output) {
   
    mysel <- reactive({
      if (input$input=="a") {
        sel <- input$select
      } else{
        sel <- input$select2
      }
      sel 
    })
    
    output$output <- renderText({ mysel() })
  }
  shinyApp(ui, server)

另一种方法是使用updateRadioButtons

  ui <- fluidPage(
    useShinyjs(),
    selectInput(inputId="input",
                label="choose ",
                c("A" = "a",
                  "B" = "b")),
    radioButtons(inputId="select",
                 label="number",
                 c("1"="one",
                   "2"="two",
                   "3"="three"),
                 selected="one"),
    

    mainPanel(verbatimTextOutput("output")
    )
  )
  server <- function(input, output, session) {

    observeEvent(input$input, {
      if(input$input=="b"){
        mychoices <- c("1"="one", "2"="two")
        
      }else{
        mychoices <- c("1"="one", "2"="two", "3"="three")
      }
      updateRadioButtons(session, "select", choices = mychoices)
    })
    output$output <- renderText({ input$select })

    
  }
  shinyApp(ui, server)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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