Flexdashboard、rhandsontable:如何以编程方式访问用户更新 table?

Flexdashboard, rhandsontable: how to programmatically access user updated table?

不是闪亮的程序员。简单的问题。 Flexdashboard 应用程序中的 rhandsontable。如何访问用户更新的列?示例代码:

---
title: "Test"
runtime: shiny
output: 
      flexdashboard::flex_dashboard:
      orientation: columns
      vertical_layout: fill
---

```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
require(dplyr)
require(tidyverse)
require(rhandsontable)

hour <- 1:24
required <- c(2, 2, 2, 2, 2, 2, 8, 8, 8, 8, 4, 4, 3, 3, 3, 3, 6, 6, 5, 5, 5, 5, 3, 3)
required <- as.integer(required)
on_duty <- as.integer(rep(0, 24))
start <- on_duty

df <- data.frame(hour, required, on_duty, start)

```
Inputs {.sidebar data-width=w}
-----------------------------------------------------------------------

```{r Inputs}

```

Column {data-width=200}
-----------------------------------------------------------------------

### Chart A

```{r}

global <- reactiveValues(df = df)

rHandsontableOutput("dftbl1")

    output$dftbl1 = renderRHandsontable({
    rhandsontable(global$df, selectCallback = TRUE, readOnly = FALSE)
    })

```

因此代码呈现 table。用户可以通过编辑 table 单元格来更新 table。但是,如何引用更新后的 table 以将 table 列传递给使用 actionButton 调用的函数?我发现的复杂示例很难破译。感谢任何反馈。史蒂夫

您可以像

那样使用 hot_to_r
modified_table <- reactive({
  hot_to_r(req(input$table_id)) ## req!
})

以访问 table 的当前状态,包括来自用户的修改。 req 是必需的,因为 hot_to_r 无法处理 NULLtable_id 应该是您用于 renderRHandsontable 的 return 值的输出 ID。

output$table_id <- renderRHandsontable({ 
  rhandsontable(initial_table) ## need to call converter
})

您所指的复杂测试(如 this one、#64-81)允许 table 的双向连接,因为它们可以从用户和来自服务器。然而,在我在这里概述的这个简单设置中,modified_table 是使用 reactive 创建的,因此它只能由用户更新。

我完全同意通过在 hot_to_r 中允许 NULL 并在 renderRHandsontable 中自动调用 rhandsontable 如果 return 值是 data.frame 但这是您必须使用的值。

这是演示此设置的完整应用程序

library(shiny)
library(rhandsontable)

ui <- fluidPage(
  rHandsontableOutput("table_id"),
  tableOutput("second_table")
)

server <- function(input, output, session) {
  initial_table <- head(iris)

  output$table_id <- renderRHandsontable({
    rhandsontable(initial_table) ## need to call converter
  })

  modified_table <- reactive({
    hot_to_r(req(input$table_id)) ## req!
  })

  output$second_table <- renderTable({
    modified_table()
  })
}

shinyApp(ui, server)

为了访问特定的列,您可以在反应上下文中使用 modified_table()$column_name