【问题标题】:What is correct method for observing multiple events in Shiny?在 Shiny 中观察多个事件的正确方法是什么?
【发布时间】:2020-09-16 21:57:56
【问题描述】:

每次用户选择不同的日期范围、要显示的小数位数或通用变量与所有变量集时,我都需要一个数据表来更新。下面的代码有效,但我认为它不“正确”。我需要代码响应的三个变量(input$descr_daterange、input$descr_radio 和 input$descr_decimals)位于 renderDataTable 函数中。这似乎不是正确的做法。

    observeEvent(input$descr_daterange, {
            descr_date_inds <- reactiveValues()
            descr_date_inds$begin_ind <- min(which(substr(inputData$qcdata$LDT,1,10) == input$descr_daterange[1]))
            descr_date_inds$end_ind <- max(which(substr(inputData$qcdata$LDT,1,10) == input$descr_daterange[2]))
            
            output$data_descr <- renderDataTable({
                    description <- descr(inputData$qcdata[descr_date_inds$begin_ind:descr_date_inds$end_ind,],transpose = TRUE, stats = input$descr_radio)
                    description <- description[-which(row.names(description) == 'RecNum'),]
                    
                    if ('N.Valid' %in% colnames(description) & 'Pct.Valid' %in% colnames(description)){
                            description <- description[,-which(colnames(description) == 'N.Valid' | colnames(description) == 'Pct.Valid')]}
                    
                    description <- round2(description[,2:ncol(description)],input$descr_decimals)
                    return(description)
                    autoWidth = TRUE
                    options=list(pageLength = 50)
            })
    })

我见过列出如下变量的代码示例:

  observeEvent(c(input$descr_daterange, input$descr_decimals, input$descr_radio),  
{...})

但是,我收到警告说只有列表中的第一个正在被使用。我该如何编写代码以使其正常工作?

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    你很接近:将所有反应输入放在括号内以触发触发器。

    试试这个:

    observeEvent({
      input$descr_daterange
      input$descr_decimals
      input$descr_radio
    }, {
      descr_date_inds <- reactiveValues()
      descr_date_inds$begin_ind <- min(which(substr(inputData$qcdata$LDT,1,10) == input$descr_daterange[1]))
      descr_date_inds$end_ind <- max(which(substr(inputData$qcdata$LDT,1,10) == input$descr_daterange[2]))
      
      output$data_descr <- renderDataTable({
        description <- descr(inputData$qcdata[descr_date_inds$begin_ind:descr_date_inds$end_ind,],transpose = TRUE, stats = input$descr_radio)
        description <- description[-which(row.names(description) == 'RecNum'),]
        
        if ('N.Valid' %in% colnames(description) & 'Pct.Valid' %in% colnames(description)){
          description <- description[,-which(colnames(description) == 'N.Valid' | colnames(description) == 'Pct.Valid')]}
        
        description <- round2(description[,2:ncol(description)],input$descr_decimals)
        return(description)
        autoWidth = TRUE
        options=list(pageLength = 50)
      })
    })
    

    顺便说一句:

    1. reactiveValues() 往往属于 在反应块之外,并在其中使用/引用。我不知道您的应用程序的其余部分,也不知道您打算如何使用它们,因此您可能根本不需要它(并且只是在使用它的列表/环境功能),在这种情况下,您携带的是不需要的这样做的开销。

    2. 你不应该在if 条件中使用&amp;|,除非它包含在anyall 或一些聚合函数中。 if 要求它的条件恰好是长度 1,而 &amp;/| 是矢量化逻辑与/或,这意味着它们的长度可以是 0 或更多。

      即使&amp;的两边都是length-1,还是有原因的:&amp;&amp;短路,&amp;没有。

      ### short-circuiting
      TRUE || stop("hello")
      # [1] TRUE
      
      ### not
      TRUE | stop("hello")
      # Error: hello
      

      最后,它有点声明性,因为通过强迫自己使用&amp;&amp;,你必须考虑条件的长度。通过使用&amp;,您暗示(对于正在阅读/使用您的代码的任何人)您将接受长度不是 1 的结果......这并不总是对 @ 有效987654337@.

    【讨论】:

    • 谢谢。这确实有效,但现在我收到一个带有 min(which(substr(inputData$qcdata$LDT,1,10) 部分代码的“-Inf”或“Inf”警告。我不太明白那个警告,因为input$descr_daterange 是 observeEvent 代码的表达式部分。我尝试将获取 input$descr_daterange 的三行放在单独的 observeEvent 函数中,并将 req(input$descr_datarange) 用于 renderDataTable 函数,但仍然出现警告。另外,renderDataTable也无法识别选项列表。无论如何,这种行为对我来说真的很奇怪。再次感谢!
    • min(which(FALSE)) 返回 Infmax(which(FALSE)) 返回 -Inf,因此请确保您 100% 确定匹配。
    猜你喜欢
    • 2011-06-11
    • 2011-01-04
    • 2022-01-10
    • 2020-12-18
    • 2021-12-06
    • 2013-03-01
    • 1970-01-01
    • 2018-11-28
    • 2014-09-24
    相关资源
    最近更新 更多