I want to disable "3" when "B" is selected and move radio button selection to "1". When "A" is selected again, I want "3" to be enable again. I tried a couple of things with shinyjs::runjs("")
but it didn't work out in desirable way. 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
. 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
.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.