【问题标题】:shiny debounce doesn't render initial plot闪亮的去抖不会呈现初始情节
【发布时间】:2019-04-02 06:07:19
【问题描述】:

如果我向 get_data() 反应表达式添加去抖动,则第一次检索数据时,绘图不会呈现。但是,更改数据(通过选择新的 mpg)会导致绘图随后呈现。为什么是这样?有解决方法吗? 这是一个简单的最小示例来演示该问题。尝试删除 %>% debounce(500) 以查看它在没有它的情况下是否按预期工作:

if (interactive()) {
  options(device.ask.default = FALSE)

  library(shiny)
  library(magrittr)

  ui <- fluidPage(
    selectInput("select", label = "select mpg", choices = c(mtcars$mpg, ""), selected = ""),
    plotOutput("plot")
  )

  server <- function(input, output, session) {
    get_data <- reactive({
      req(input$select)
      mtcars[mtcars$mpg == input$select,]
      }) %>% debounce(500)

    get_plot <- reactive({
      data <- get_data()
      print(data)
      plot(get_data())
      }) 

      output$plot <- renderPlot({
        get_plot()
      })
  }

  shinyApp(ui, server)
}

【问题讨论】:

    标签: r shiny debounce


    【解决方案1】:

    这里发生了几件事。我认为我们不允许在选择输入中有重复项。 mtcars$mpg 里面有重复的值。将初始值设置为"" 也会导致奇怪的行为。如果你真的希望初始图是空的以及去抖动,我们可以将它设置为" "。这就是它的样子。

    if (interactive()) {
      options(device.ask.default = FALSE)
    
      library(shiny)
      library(magrittr)
    
      ui <- fluidPage(
        selectInput("select", label = "select mpg", choices = c(" ",unique(mtcars$mpg)),selected = " "),
        plotOutput("plot")
      )
    
      server <- function(input, output, session) {
        get_data <- reactive({
          req(input$select)
          if(!is.na(as.numeric(input$select))) mtcars[mtcars$mpg == input$select,] else NULL
        }) %>% debounce(500)
    
        get_plot <- reactive({
          req(get_data())
          data <- get_data()
          print(data)
          plot(get_data())
        }) 
    
        output$plot <- renderPlot({
          get_plot()
        })
      }
    
      shinyApp(ui, server)
    }
    

    否则,如果您对初始情节感到满意,则可以使用以下方法。注意使用unique()很重要

    if (interactive()) {
      options(device.ask.default = FALSE)
    
      library(shiny)
      library(magrittr)
    
      ui <- fluidPage(
        selectInput("select", label = "select mpg", unique(mtcars$mpg)),
        plotOutput("plot")
      )
    
      server <- function(input, output, session) {
        get_data <- reactive({
          req(input$select)
          mtcars[mtcars$mpg == input$select,]
        }) %>% debounce(500)
    
        get_plot <- reactive({
          req(get_data())
          data <- get_data()
          print(data)
          plot(get_data())
        }) 
    
        output$plot <- renderPlot({
          get_plot()
        })
      }
    
      shinyApp(ui, server)
    }
    

    我什至尝试用selectizeInput("select", label = "select mpg", choices = unique(mtcars$mpg),multiple = TRUE,options = list(maxItems = 1)) 替换选择输入,这仍然会导致问题。

    【讨论】:

    • 这似乎对我的 reprex 有效,所以给出了答案。但是,它仍然不适用于我更复杂的应用程序。我将尝试重建一个您的解决方案不起作用的示例。
    • 在我更复杂的应用程序中,我必须删除反应中的 req 才能加载初始图。我使用 validate 作为没有 req 的解决方法。
    猜你喜欢
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    • 2019-06-19
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 2021-05-10
    • 2019-01-21
    相关资源
    最近更新 更多