假设我有以下Shiny
应用程序-
library(shiny)
library(shinyjs)
shinyApp(
ui = fluidPage(
useShinyjs(),
checkboxInput("checkbox", label = "Choice a value", value = TRUE),
selectInput("variable", "Variable:",
c("Cylinders" = "cyl",
"Transmission" = "am",
"Gears" = "gear"))
),
server = function(input, output) {
observeEvent(input$checkbox, {
toggleState("variable")
})
}
)
现在我希望如果checkbox
单击则始终Cylinders
从variable
.
使用上述方法,这不会发生。Gears
目前,当用户在启用后选择其他值 ig variable
,然后再次用户继续checkbox
禁用variable
时,选择Gears
保留在 UI 中,我不想并且Cylinders
每次都想立即返回到该选项variable
被禁用。
任何如何实现这一点的指针都将受到高度赞赏。
bretauv's
回复后更新——
他的回答适用于shiny's
native ,但如果我使用如下包selectInput()
似乎不起作用-shinyWidgets
pickerInput()
library(shiny)
library(shinyjs)
library(shinyWidgets)
shinyApp(
ui = fluidPage(
useShinyjs(),
checkboxInput("checkbox", label = "Choice a value", value = FALSE),
pickerInput("variable", "Variable:",
c("Cylinders" = "cyl",
"Transmission" = "am",
"Gears" = "gear"),
width = "50%",
selected = "gear")
),
server = function(input, output, session) {
observe({
if (input$checkbox) {
toggleState(id = "variable")
} else {
toggleState(id = "variable")
updatePickerInput(session = session,
inputId = "variable", label = NULL,
choices = c("Cylinders" = "cyl",
"Transmission" = "am",
"Gears" = "gear"),
selected = "Cylinders")
}
})
}
)
任何解决此问题的指针将不胜感激。