R Shiny - 如何在更新依赖反应图之前更新依赖反应 selectInput
R Shiny - How to update a dependent reactive selectInput before updating dependent reactive plot
应用结构
我有一个带有典型侧边栏面板 + 主面板结构的 Shiny 应用程序。
- 侧边栏面板:侧边栏面板中有多个 selectInput 小部件,每个 selectInput 中的选择取决于
前一个 selectInput 的选择值。 (即,用户从 selectInput 1 中选择一个数据集并从 selectInput 2 中选择一个变量,其中 selectInput #2 中可用作“选择”的变量取决于输入 1 的选择)
- 主面板:有一个基本的 ggplot2 可视化,它取决于侧边栏面板中的 2 个输入选择(数据集和变量)。
问题
当用户在 selectInput #1 中选择新数据集时,selectInput #2(可用变量)和绘图都需要更新。我希望 selectInput #2 首先更新,然后是情节。但是,似乎情节总是在第二个 selectInput 有机会更新之前继续更新。这会导致绘图尝试渲染无效绘图——即尝试使用 iris 数据集渲染 mtcars 变量的绘图,反之亦然。
有没有一种方法可以优先考虑 selectInput #2 的反应性更新 在 renderPlot 的反应性更新之前发生?
备注
- 作为用户体验要求,我避免使用按钮来渲染绘图。
我需要情节实时动态更新基于
选择。
- 在我的 reprex 中,我包含了打印语句来描述情节
尝试使用无效的选择组合进行更新。
library(shiny)
library(ggplot2)
library(dplyr)
# Define UI for application that draws a histogram
ui <- fluidPage(
titlePanel("Reactivity Test"),
# Sidebar with two input widgets
sidebarLayout(
sidebarPanel(
selectInput(inputId = "dataset",
label = "Input #1 - Dataset",
choices = c("mtcars", "iris")),
selectInput(inputId = "variable",
label = "Input #2 - Variable",
choices = NULL)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
input_dataset <- reactive({
if (input$dataset == "mtcars") {
return(mtcars)
} else {
return(iris)
}
})
mtcars_vars <- c("mpg", "cyl", "disp")
iris_vars <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
available_vars <- reactive({
if (input$dataset == "mtcars") {
return(mtcars_vars)
} else {
return(iris_vars)
}
})
observe({
updateSelectInput(inputId = "variable", label = "Variable", choices = available_vars())
})
output$distPlot <- renderPlot({
req(input$dataset, input$variable)
print(input$dataset)
print(input$variable)
selected_dataset <- input_dataset()
selected_variable <- input$variable
filtered_data <- selected_dataset %>% select(selected_variable)
ggplot(filtered_data, aes(x = get(selected_variable))) +
geom_histogram()
})
}
# Run the application
shinyApp(ui = ui, server = server)
您可以尝试使用 freezReactiveValue()
功能,正如 Hadley Wickham 在掌握 shiny 中推荐的那样。 link: Freezing reactive inputs
library(shiny)
library(ggplot2)
library(dplyr)
# Define UI for application that draws a histogram
ui <- fluidPage(
titlePanel("Reactivity Test"),
# Sidebar with two input widgets
sidebarLayout(
sidebarPanel(
selectInput(inputId = "dataset",
label = "Input #1 - Dataset",
choices = c("mtcars", "iris")),
selectInput(inputId = "variable",
label = "Input #2 - Variable",
choices = NULL)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output, session) {
input_dataset <- reactive({
if(input$dataset == "mtcars") {
return(mtcars)
} else {
return(iris)
}
})
observeEvent(input$dataset, {
freezeReactiveValue(input, "variable")
updateSelectInput(session = session, inputId = "variable", choices = names(input_dataset()))
})
output$distPlot <- renderPlot({
ggplot(input_dataset(), aes(x = .data[[input$variable]])) +
geom_histogram()
})
}
# Run the application
shinyApp(ui = ui, server = server)
应用结构
我有一个带有典型侧边栏面板 + 主面板结构的 Shiny 应用程序。
- 侧边栏面板:侧边栏面板中有多个 selectInput 小部件,每个 selectInput 中的选择取决于 前一个 selectInput 的选择值。 (即,用户从 selectInput 1 中选择一个数据集并从 selectInput 2 中选择一个变量,其中 selectInput #2 中可用作“选择”的变量取决于输入 1 的选择)
- 主面板:有一个基本的 ggplot2 可视化,它取决于侧边栏面板中的 2 个输入选择(数据集和变量)。
问题
当用户在 selectInput #1 中选择新数据集时,selectInput #2(可用变量)和绘图都需要更新。我希望 selectInput #2 首先更新,然后是情节。但是,似乎情节总是在第二个 selectInput 有机会更新之前继续更新。这会导致绘图尝试渲染无效绘图——即尝试使用 iris 数据集渲染 mtcars 变量的绘图,反之亦然。
有没有一种方法可以优先考虑 selectInput #2 的反应性更新 在 renderPlot 的反应性更新之前发生?
备注
- 作为用户体验要求,我避免使用按钮来渲染绘图。 我需要情节实时动态更新基于 选择。
- 在我的 reprex 中,我包含了打印语句来描述情节 尝试使用无效的选择组合进行更新。
library(shiny)
library(ggplot2)
library(dplyr)
# Define UI for application that draws a histogram
ui <- fluidPage(
titlePanel("Reactivity Test"),
# Sidebar with two input widgets
sidebarLayout(
sidebarPanel(
selectInput(inputId = "dataset",
label = "Input #1 - Dataset",
choices = c("mtcars", "iris")),
selectInput(inputId = "variable",
label = "Input #2 - Variable",
choices = NULL)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
input_dataset <- reactive({
if (input$dataset == "mtcars") {
return(mtcars)
} else {
return(iris)
}
})
mtcars_vars <- c("mpg", "cyl", "disp")
iris_vars <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
available_vars <- reactive({
if (input$dataset == "mtcars") {
return(mtcars_vars)
} else {
return(iris_vars)
}
})
observe({
updateSelectInput(inputId = "variable", label = "Variable", choices = available_vars())
})
output$distPlot <- renderPlot({
req(input$dataset, input$variable)
print(input$dataset)
print(input$variable)
selected_dataset <- input_dataset()
selected_variable <- input$variable
filtered_data <- selected_dataset %>% select(selected_variable)
ggplot(filtered_data, aes(x = get(selected_variable))) +
geom_histogram()
})
}
# Run the application
shinyApp(ui = ui, server = server)
您可以尝试使用 freezReactiveValue()
功能,正如 Hadley Wickham 在掌握 shiny 中推荐的那样。 link: Freezing reactive inputs
library(shiny)
library(ggplot2)
library(dplyr)
# Define UI for application that draws a histogram
ui <- fluidPage(
titlePanel("Reactivity Test"),
# Sidebar with two input widgets
sidebarLayout(
sidebarPanel(
selectInput(inputId = "dataset",
label = "Input #1 - Dataset",
choices = c("mtcars", "iris")),
selectInput(inputId = "variable",
label = "Input #2 - Variable",
choices = NULL)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output, session) {
input_dataset <- reactive({
if(input$dataset == "mtcars") {
return(mtcars)
} else {
return(iris)
}
})
observeEvent(input$dataset, {
freezeReactiveValue(input, "variable")
updateSelectInput(session = session, inputId = "variable", choices = names(input_dataset()))
})
output$distPlot <- renderPlot({
ggplot(input_dataset(), aes(x = .data[[input$variable]])) +
geom_histogram()
})
}
# Run the application
shinyApp(ui = ui, server = server)