首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >更新依赖反应图之前如何更新依赖的反应性selectInput?

更新依赖反应图之前如何更新依赖的反应性selectInput?
EN

Stack Overflow用户
提问于 2021-05-05 00:02:27
回答 1查看 978关注 0票数 4

App结构

我有一个闪亮的应用程序,典型的侧边栏面板+主板结构。

  • 边栏面板:侧栏中有多个selectInput小部件,其中每个selectInput中的选择取决于前面selectInput的选定值。(也就是说,用户从selectInput 1中选择一个数据集&从selectInput 2中选择一个变量,其中selectInput #2中作为“选择”可用的变量取决于输入1的选择)
  • 主面板:有一个基本的ggplot2可视化,它依赖于侧栏面板中的2个输入选择(数据集和变量)。

问题

当用户在selectInput #1中选择一个新的数据集时,selectInput #2 (可用变量)和绘图都需要更新。我希望selectInput #2首先更新,然后更新情节。然而,在第二次selectInput有机会更新之前,情节似乎总是在进行更新。这将导致试图呈现无效绘图的绘图--即尝试使用虹膜数据集呈现mtcar变量的绘图,反之亦然。

是否有办法在selectInput的反应性更新之前对renderPlot #2的反应性更新进行优先排序?

Notes

  • 作为UX的一项要求,我避免使用按钮来渲染情节。我需要的情节,以动态更新的实时基础上的选择。
  • 在我的reprex中,我包括了print语句来描述情节是如何用无效的选择组合进行更新的。
代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-05 00:20:11

您可以尝试使用freezReactiveValue()函数,就像哈德利·韦翰在掌握光泽方面所建议的那样。链接:冻结反应性输入

代码语言:javascript
复制
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)
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67393613

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档