【问题标题】:Shiny Reactivity Fails with data.tables闪亮的反应性因data.tables而失败
【发布时间】:2015-12-08 19:26:42
【问题描述】:

作为Modifying a reactive value should trigger an observe chunk 的后续行动,我进一步调查了该问题并意识到该问题可能源于我对data.tables 的使用。 data.tables 是通过引用修改的,这与 data.frames 不同,这使得它们使用起来更高效,但显然对 shiny 的反应不可见。

在下面的示例中,按下提交按钮会在 data.frame 情况下触发观察块,但在 data.table 情况下不会触发。一种解决方法可能是将值与 data.table 的更改相关联,这也有助于触发反应。

带有data.frame

shinyServer ( 

   function(input, output, session) {

      lReactiveValues = reactiveValues(a = data.frame(firstcol = runif(1)))

      voidaA = observeEvent(
         input$buttonCommit,
         {
            new = runif(1)
            cat(new,' one\t')
            lReactiveValues$a[letters[ncol(isolate(lReactiveValues$a))]] = new
         }
      )

      voidB = observe(priority = 50,{
         # input$buttonCommit
         cat(ncol(lReactiveValues$a))

         counter = runif(1)
         cat(counter,' two\t'); 

         if (counter > 0.5) {

            cat('\n')

            cat(ncol(lReactiveValues$a),' three\n')

         }
      }
      )

   }
)

带有data.table

shinyServer ( 

   function(input, output, session) {

      lReactiveValues = reactiveValues(a = data.table(firstcol = runif(1)))
      # lReactiveValues = reactiveValues(a = data.frame(firstcol = runif(1)))

      voidaA = observeEvent(
         input$buttonCommit,
         {
            new = runif(1)
            cat(new,' one\t')
            setnames(
               lReactiveValues$a[, newcol := new],
               'newcol',
               letters[ncol(isolate(lReactiveValues$a))]
            )
            cat(ncol(lReactiveValues$a))
         }
      )

      voidB = observe(priority = 50,{
         # input$buttonCommit
         cat(ncol(lReactiveValues$a))

         counter = runif(1)
         cat(counter,' two\t'); 

         if (counter > 0.5) {

            cat('\n')

            cat(ncol(lReactiveValues$a),' three\n')

         }
      }
      )

   }
)

ui.r

dashboardPage(

   dashboardHeader(

      title = "Analytics"

   ),

   ## Sidebar content
   dashboardSidebar(
   menuItem("Analysis", tabName = "tabAnalysis", icon = icon("calculator"))
   ),

   ## Body content
   dashboardBody(
      tabItems(
         tabItem(
            tabName = "tabAnalysis",
            actionButton("buttonCommit", "Commit!")
         )
      )
      #, style='width: 100%; height: 100%;'

   )
)

代码作用的简短摘要 - 按下按钮应将一些文本打印到控制台,其中包括字符串“one”。该按钮应进一步触发观察块,提示打印一些包含字符串“二”的文本。根据观察块中的 if 条件,可能会打印另一组包含“三”的文本。在 data.frame 的 server.r 情况下,此行为在应用程序运行时一直存在。在 data.table 的 server.r 案例中,这种行为发生在几次单击按钮之后,仅打印“one”字符串,而不会打印“two”和“three”。想一想,这种行为的翻转发生在第一次发现 if 条件为假之后。

【问题讨论】:

  • 你可以尝试copy强制修改对象在内存中的地址
  • 如果是这样,这是一个很好的提示。但是数据表通常是大型数据集,如果可以,我想避免复制。
  • 我用过很多 data.table 有光泽但从来没有作为反应值。我通过引用更新并使用反应函数,因此从未遇到任何问题。我想不出你的示例代码来推荐你一些解决方法。
  • @jangorecki - 好点。我已经为问题添加了描述。

标签: r data.table shiny


【解决方案1】:

如果您担心内存问题,这是一个 hacky 解决方案:

添加一个 DTControl 值:

lReactiveValues = reactiveValues(a = data.table(firstcol = runif(1)), DTControl = 0)

然后在参考更改后将其与以下内容配对:

lReactiveValues$a[, newcol := new]
lReactiveValues$DTControl <- lReactiveValues$DTControl + 1

然后在你的观察函数中添加这一行:

lReactiveValues$DTControl

因为您的 DTControl 更新,反应功能被触发。需要一点努力才能确保您始终将两者配对在一起,但它可以在没有任何内存负载的情况下覆盖此引用问题。

无论如何,这应该在闪亮中突出显示为一个问题。

【讨论】:

  • 谢谢,但我已经在我的问题中提到了这项工作。
【解决方案2】:

可以使用 [[ 或 $ 修改 data.table 并保存到反应式表达式中。如:

dt <- reactiveVal(data.table(a=c(1,2),b=c(10,20))

在反应式表达式中,以下将 b 的值从 10 更改为 100 并触发观察者

observeEvent(dt(), { print(dt()) })
observeEvent(input$mybutton, { 
  x <- dt()
  x[a==1][["b"]] <- 100
  dt(x)
})

使用您的示例代码和 reactiveValues

server <- shinyServer ( 
  
  function(input, output, session) {
    
    lReactiveValues = reactiveValues(a = data.table(firstcol = runif(1)))
    # lReactiveValues = reactiveValues(a = data.frame(firstcol = runif(1)))
    
    voidaA = observeEvent(
      input$buttonCommit,
      {
        new = runif(1)
        cat(new,' one\t')
        
        lRV <- lReactiveValues$a
        lRV[["newcol"]] <- new
        lReactiveValues$a <- lRV
        
        cat(ncol(lReactiveValues$a))
      }
    )
    
    voidB = observe(priority = 50,{
      # input$buttonCommit
      cat(ncol(lReactiveValues$a))
      
      counter = runif(1)
      cat(counter,' two\t'); 
      
      if (counter > 0.5) {
        
        cat('\n')
        
        cat(ncol(lReactiveValues$a),' three\n')
        
      }
    }
    )
    
  }
)

ui <- dashboardPage(
  
  dashboardHeader(
    
    title = "Analytics"
    
  ),
  
  ## Sidebar content
  dashboardSidebar(
    menuItem("Analysis", tabName = "tabAnalysis", icon = icon("calculator"))
  ),
  
  ## Body content
  dashboardBody(
    tabItems(
      tabItem(
        tabName = "tabAnalysis",
        actionButton("buttonCommit", "Commit!")
      )
    )
    #, style='width: 100%; height: 100%;'
    
  )
)

shinyApp(ui,server)

【讨论】:

    猜你喜欢
    • 2017-06-14
    • 2020-01-14
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 2018-10-30
    • 2022-12-07
    • 1970-01-01
    相关资源
    最近更新 更多