[英]R Shiny - Using a custom message handler to disable a single radio button in a radio button group
[英]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.