Rhsiny:根据更新的 rhandsontable 对象自动更新输出对象

Rhsiny: Automatically update an output object based on updated rhandsontable object

我有一个包含两个 table 的应用程序。一个 table 是一个 renderhandsontable 对象,另一个只是一个 rendertable 对象。我希望当我更新我的 renderhandsontable 对象时它自动更新我的 rendertable 对象。我的 renderhandontable 对象是由应用中其他地方的数据使用多个过滤器创建的。

我在这里引用了几篇非常有用的帖子来帮助我在创建可用于多个输出对象(例如

)的反应式 table 方面取得进展

Get selected rows of Rhandsontable

但我似乎无法越过这最后一道障碍。我还尝试添加一个按钮(使用 eventReactive),这样当我按下它时 table 会更新而不是自动更新,但那里没有运气(自动肯定是首选) .

我在下面创建了我的 server 代码的过度简化版本。

#dummy data
x = c('A','A','A', 'B','B', 'c')
y = c('G1', 'G1', 'G1', 'G2', 'G2','G3')
z = c('100', '200', '300', '400','500','600')

b=data.frame('Category' = x,
             'Group' = y,
             'Total' = z)

#create reactive object to be used in multiple places
test <- reactive({

  t <-filter(b, b$Category %in% input$cat & b$Group %in% input$group)

  return(t)

})

output$test_table <- renderTable({

  tbl = data.frame(matrix(0, ncol = 4, nrow = 4))

#I know something needs to be done prior to this step to get updated values #of test()

  tbl[1,1] <- test()[1,3]

  return(tbl)
})


output$contents <- renderRHandsontable({

  rhandsontable(test())

})

我可以让我的 table 正确显示并开始更新数据,但是一旦我对我的 table 进行更新,它就不会反映在我的第二个 table.

我真的为此苦苦挣扎了很长一段时间,所以任何帮助或提示都将不胜感激!

请通过input$my_id阅读this. You can access the rhandsontable params。要获取当前数据,请使用 input$my_id$params$data.

这就是我认为你想要的:

library(shiny)
library(rhandsontable)

ui <- fluidPage(rHandsontableOutput("contents"),
                tableOutput("test_table"),
                tableOutput("test_table_subset"))

server <- function(input, output) {

  # dummy data
  x = c('A', 'A', 'A', 'B', 'B', 'C')
  y = c('G1', 'G1', 'G1', 'G2', 'G2', 'G3')
  z = c('100', '200', '300', '400', '500', '600')

  b = data.frame('Category' = x,
                 'Group' = y,
                 'Total' = z)

  # create reactive object to be used in multiple places
  test <- reactive({
    t <- b # dplyr::filter(b, b$Category %in% input$cat & b$Group %in% input$group)
    return(t)
  })

  output$contents <- renderRHandsontable({
    rhandsontable(test())
  })

  contentsTableDat <- reactive({
    req(input$contents)
    hot_to_r(input$contents)
  })

  output$test_table <- renderTable({
    contentsTableDat()
  })

  output$test_table_subset <- renderTable({
    contentsTableDat()[1, 3]
  })
}

shinyApp(ui = ui, server = server)