相关输入的闪亮优先级

发布于 2025-02-10 18:20:29 字数 1280 浏览 4 评论 0原文

考虑使用三个输入的这个应用程序,所有这些应用程序彼此相关...

library(tidyverse)
library(shiny)
df <- mtcars %>% 
  split(.$vs)


ui <- fluidPage(
  sidebarPanel(
    radioButtons("no1", "Select", choices = names(df)),
    selectInput("no2", "select", choices = NULL),
    selectInput("no3", "select", choices = NULL)
  ),
  mainPanel(plotOutput("plot"))
)

server <- function(input, output, session){
  
 rv <- reactiveValues()  
 
 observe({
   req(input$no1)
   updateSelectInput(session,"no2", choices = df[[input$no1]]$cyl)
 }, priority = 10)
  
 observe({
   rv$after_v1_v2 <- df[[input$no1]] %>% filter(cyl == input$no2)
 }, priority = 9)
   
   
  observe({
    req(input$no1)
    req(input$no2)
    updateSelectInput(session,"no3", choices = rv$after_v1_v2$am )
  }, priority = 8)
  
  observe({
    rv$after_v3 <- rv$after_v1_v2 %>% filter(am == input$no3)
  }, priority = 7)
  
  
  output$plot <- renderPlot({
    Sys.sleep(2)
    rv$after_v3 %>% 
      ggplot(aes(disp, hp)) + 
       geom_point()
  })
  
  outputOptions(output, "plot", priority = 1)
}

shiny::shinyApp(ui, server)

...我如何防止在更新侧面输入之前呈现图。如您所见,优先参数无效。更新应依次独立进行哪些输入更改,例如No1&gt;&gt; no2&gt;&gt; NO3,然后最后是情节。

我知道有一个debounce(),但我不想“手动”放慢应用程序。

Consider this app with three inputs and all are related to each other...

library(tidyverse)
library(shiny)
df <- mtcars %>% 
  split(.$vs)


ui <- fluidPage(
  sidebarPanel(
    radioButtons("no1", "Select", choices = names(df)),
    selectInput("no2", "select", choices = NULL),
    selectInput("no3", "select", choices = NULL)
  ),
  mainPanel(plotOutput("plot"))
)

server <- function(input, output, session){
  
 rv <- reactiveValues()  
 
 observe({
   req(input$no1)
   updateSelectInput(session,"no2", choices = df[[input$no1]]$cyl)
 }, priority = 10)
  
 observe({
   rv$after_v1_v2 <- df[[input$no1]] %>% filter(cyl == input$no2)
 }, priority = 9)
   
   
  observe({
    req(input$no1)
    req(input$no2)
    updateSelectInput(session,"no3", choices = rv$after_v1_v2$am )
  }, priority = 8)
  
  observe({
    rv$after_v3 <- rv$after_v1_v2 %>% filter(am == input$no3)
  }, priority = 7)
  
  
  output$plot <- renderPlot({
    Sys.sleep(2)
    rv$after_v3 %>% 
      ggplot(aes(disp, hp)) + 
       geom_point()
  })
  
  outputOptions(output, "plot", priority = 1)
}

shiny::shinyApp(ui, server)

...how I prevent that the plot is rendered before the sidepanel inputs are updated. As you can see the priority parameters have no effect. The update should take place sequentially independent which input is changed e.g. no1 >> no2 >> no3 and then finally the plot.

I know there is a debounce(), but I don't want to "manually" slow down the app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角卖回忆 2025-02-17 18:20:34

使用FreezereActiveValue找到了解决方案。

library(tidyverse)
library(shiny)
df <- mtcars %>% 
  split(.$vs)


ui <- fluidPage(
  sidebarPanel(
    radioButtons("no1", "Select", choices = names(df)),
    selectInput("no2", "select", choices = NULL),
    selectInput("no3", "select", choices = NULL)
  ),
  mainPanel(plotOutput("plot"))
)

server <- function(input, output, session){
  
 rv <- reactiveValues()  
 
 observe({
   updateSelectInput(session,"no2", choices = df[[input$no1]]$cyl)
   freezeReactiveValue(input, "no3")
 }, priority = 10)
  
   
  observe({
    rv$after_v1_v2 <- df[[input$no1]] %>% filter(cyl == input$no2)
    updateSelectInput(session,"no3", choices = rv$after_v1_v2$am )
    freezeReactiveValue(input, "no3")
  }, priority = 8)
  
  observe({
    req(input$no3)
    rv$after_v3 <- rv$after_v1_v2 %>% filter(am == input$no3)
  }, priority = 7)
  
  
  output$plot <- renderPlot({
    req(input$no1)
    req(input$no2)
    req(input$no3)
    Sys.sleep(2)
    rv$after_v3 %>% 
      ggplot(aes(disp, hp)) + 
       geom_point()
  })
  
  outputOptions(output, "plot", priority = 1)
}

shiny::shinyApp(ui, server)

Found a solution using freezeReactiveValue.

library(tidyverse)
library(shiny)
df <- mtcars %>% 
  split(.$vs)


ui <- fluidPage(
  sidebarPanel(
    radioButtons("no1", "Select", choices = names(df)),
    selectInput("no2", "select", choices = NULL),
    selectInput("no3", "select", choices = NULL)
  ),
  mainPanel(plotOutput("plot"))
)

server <- function(input, output, session){
  
 rv <- reactiveValues()  
 
 observe({
   updateSelectInput(session,"no2", choices = df[[input$no1]]$cyl)
   freezeReactiveValue(input, "no3")
 }, priority = 10)
  
   
  observe({
    rv$after_v1_v2 <- df[[input$no1]] %>% filter(cyl == input$no2)
    updateSelectInput(session,"no3", choices = rv$after_v1_v2$am )
    freezeReactiveValue(input, "no3")
  }, priority = 8)
  
  observe({
    req(input$no3)
    rv$after_v3 <- rv$after_v1_v2 %>% filter(am == input$no3)
  }, priority = 7)
  
  
  output$plot <- renderPlot({
    req(input$no1)
    req(input$no2)
    req(input$no3)
    Sys.sleep(2)
    rv$after_v3 %>% 
      ggplot(aes(disp, hp)) + 
       geom_point()
  })
  
  outputOptions(output, "plot", priority = 1)
}

shiny::shinyApp(ui, server)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文