[英]R shiny - Disable/enable single radio button based upon select input
I want to disable "3" when "B" is selected and move radio button selection to "1".我想在选择“B”时禁用“3”并将单选按钮选择移动到“1”。 When "A" is selected again, I want "3" to be enable again.
当再次选择“A”时,我希望再次启用“3”。 I tried a couple of things with
shinyjs::runjs("")
but it didn't work out in desirable way.我用
shinyjs::runjs("")
尝试了几件事,但效果不佳。 Any help will be appreciated.任何帮助将不胜感激。
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)
}
One way to do it is use conditionPanel
.一种方法是使用
conditionPanel
。 Try this.尝试这个。
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)
Another way is to use updateRadioButtons
.另一种方法是使用
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.