我有许多相关的输入字段,并希望在用户点击按钮时更新所有这些字段。用例是加载存储的输入设置。以下是可重复的示例。 “petal”selectInput的选择取决于“species”selectInput中的选择。
当我点击按钮时,只有第一个selectInput会更新。
library(shiny)
data(iris)
# ui function
ui <- fluidPage(
uiOutput("species.out"),
uiOutput("petal.out"),
actionButton("update", label = "Update")
)
# server function
server <- function(input, output, session) {
output$species.out = renderUI({
selectInput("species", label = "", choices = sort(unique(iris$Species)))
})
output$petal.out = renderUI({
req(input$species)
selectInput("petal", label = "", choices = sort(unique(iris[iris$Species == input$species,]$Petal.Width)))
})
observeEvent(input$update,
{
updateSelectInput(session, inputId = "species", selected = "virginica")
updateSelectInput(session, inputId = "petal", choices=sort(unique(iris[iris$Species == input$species,]$Petal.Width)), selected = "2.3")
})
}
shinyApp(ui = ui, server = server)
有没有办法一次更新两个字段?
答案 0 :(得分:0)
执行此操作的一种方法是为observeEvent
添加另一个input$species
,以便更新selectInput
花瓣。以下是带有更改的修改后的代码:
library(shiny)
data(iris)
# ui function
ui <- fluidPage(
uiOutput("species.out"),
uiOutput("petal.out"),
actionButton("update", label = "Update")
)
# server function
server <- function(input, output, session) {
output$species.out = renderUI({
selectInput("species", label = "", choices = sort(unique(iris$Species)))
})
output$petal.out = renderUI({
req(input$species)
selectInput("petal", label = "", choices = sort(unique(iris[iris$Species == input$species,]$Petal.Width)))
})
observeEvent(input$update,
{
updateSelectInput(session, inputId = "species", selected = "virginica")
# updateSelectInput(session, inputId = "petal", choices=sort(unique(iris[iris$Species == input$species,]$Petal.Width)), selected = "2.3")
})
observeEvent(input$species,{
req(input$petal)
updateSelectInput(session, inputId = "petal", choices=sort(unique(iris[iris$Species == input$species,]$Petal.Width)), selected = "2.3")
} )
}
shinyApp(ui = ui, server = server)
希望它有所帮助!
修改强>
要更新input$petal
,即使input$species
不符合,以下代码也会有效:
library(shiny)
data(iris)
# ui function
ui <- fluidPage(
uiOutput("species.out"),
uiOutput("petal.out"),
actionButton("update", label = "Update")
)
# server function
server <- function(input, output, session) {
output$species.out = renderUI({
selectInput("species", label = "", choices = sort(unique(iris$Species)))
})
output$petal.out = renderUI({
req(input$species)
selectInput("petal", label = "", choices = sort(unique(iris[iris$Species == input$species,]$Petal.Width)))
})
observeEvent(input$update,
{
updateSelectInput(session, inputId = "species", selected = "virginica")
updateSelectInput(session, inputId = "petal", choices=sort(unique(iris[iris$Species == input$species,]$Petal.Width)), selected = "2.3")
})
observeEvent(input$species,{
if(input$petal == "" || req(input$petal))
updateSelectInput(session, inputId = "petal", choices=sort(unique(iris[iris$Species == input$species,]$Petal.Width)), selected = "2.3")
} )
}
shinyApp(ui = ui, server = server)
此处我已将条件req(input$petal)
更改为if(input$petal == "" || req(input$petal))
,并在updateSelectInput(session, inputId = "petal", choices=sort(unique(iris[iris$Species == input$species,]$Petal.Width)), selected = "2.3")
的{{1}}中添加了observeEvent
。