除非必要,否则不要更改 R Shiny 的 selectInput 的值
Don't change R Shiny's selectInput's value unless necessary
将选项 A 设置为 E
将选项 B 设置为 2
选项 A 发生了变化,尽管我希望它保持原样,除非用户更改 或 设置为一个选项不再可用(即如果它设置为 C 并且选项 B 设置为 3)。
这是怎么做到的?
library("shiny")
library("bslib")
library("shinyWidgets")
ui <- bootstrapPage(
# https://bootswatch.com/journal/
theme = bs_theme(version = 5, "font_scale" = 1.0),
div(class = "container-fluid",
div(class = "row",
div(class="col-4",
selectInput(
inputId = "opt_a",
label = "Option A:",
choices = LETTERS[1:5],
selected = "A",
multiple = FALSE,
selectize = TRUE,
width = "120px",
size = NULL
),
),
div(class="col-4",
selectInput(
inputId = "opt_b",
label = "Option B:",
choices = 1:5,
selected = 1,
multiple = FALSE,
selectize = TRUE,
width = "120px",
size = NULL
),
),
)
)
)
# If Option B == 3, then Option A has no C
server <- function(input, output, session) {
observeEvent(input$opt_b, {
# DO THIS
if(input$opt_b == 3){
freezeReactiveValue(input, "opt_a")
updateSelectInput(
session = session,
inputId = "opt_a",
choices = c("A", "B", "D", "E"),
selected = "A"
)
} else {
freezeReactiveValue(input, "opt_a")
updateSelectInput(
session = session,
inputId = "opt_a",
choices = LETTERS[1:5]
)
}
})
}
shinyApp(ui, server)
也许您正在寻找这个
server <- function(input, output, session) {
observeEvent(input$opt_b, {
# DO THIS
if(input$opt_b == 3){
#freezeReactiveValue(input, "opt_a")
choices <- c("A", "B", "D", "E")
if (sum(choices %in% input$opt_a)>0) sel <- input$opt_a else sel <- choices[1]
updateSelectInput(
session = session,
inputId = "opt_a",
choices = choices,
selected = sel
)
} else {
#freezeReactiveValue(input, "opt_a")
sel <- input$opt_a
updateSelectInput(
session = session,
inputId = "opt_a",
choices = LETTERS[1:5],
selected = sel
)
}
})
}
将选项 A 设置为 E
将选项 B 设置为 2
选项 A 发生了变化,尽管我希望它保持原样,除非用户更改 或 设置为一个选项不再可用(即如果它设置为 C 并且选项 B 设置为 3)。
这是怎么做到的?
library("shiny")
library("bslib")
library("shinyWidgets")
ui <- bootstrapPage(
# https://bootswatch.com/journal/
theme = bs_theme(version = 5, "font_scale" = 1.0),
div(class = "container-fluid",
div(class = "row",
div(class="col-4",
selectInput(
inputId = "opt_a",
label = "Option A:",
choices = LETTERS[1:5],
selected = "A",
multiple = FALSE,
selectize = TRUE,
width = "120px",
size = NULL
),
),
div(class="col-4",
selectInput(
inputId = "opt_b",
label = "Option B:",
choices = 1:5,
selected = 1,
multiple = FALSE,
selectize = TRUE,
width = "120px",
size = NULL
),
),
)
)
)
# If Option B == 3, then Option A has no C
server <- function(input, output, session) {
observeEvent(input$opt_b, {
# DO THIS
if(input$opt_b == 3){
freezeReactiveValue(input, "opt_a")
updateSelectInput(
session = session,
inputId = "opt_a",
choices = c("A", "B", "D", "E"),
selected = "A"
)
} else {
freezeReactiveValue(input, "opt_a")
updateSelectInput(
session = session,
inputId = "opt_a",
choices = LETTERS[1:5]
)
}
})
}
shinyApp(ui, server)
也许您正在寻找这个
server <- function(input, output, session) {
observeEvent(input$opt_b, {
# DO THIS
if(input$opt_b == 3){
#freezeReactiveValue(input, "opt_a")
choices <- c("A", "B", "D", "E")
if (sum(choices %in% input$opt_a)>0) sel <- input$opt_a else sel <- choices[1]
updateSelectInput(
session = session,
inputId = "opt_a",
choices = choices,
selected = sel
)
} else {
#freezeReactiveValue(input, "opt_a")
sel <- input$opt_a
updateSelectInput(
session = session,
inputId = "opt_a",
choices = LETTERS[1:5],
selected = sel
)
}
})
}