首页 > 解决方案 > Flexdashboard,rhandsontable:如何以编程方式访问用户更新的表?

问题描述

不是闪亮的程序员。简单的问题。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)
    })

```

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

标签: rshinyflexdashboardrhandsontable

解决方案


你可以hot_to_r使用

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

访问表的当前状态,包括来自用户的修改。req需要,因为hot_to_r无法处理NULLs。 table_id应该是您用于返回值的输出 ID renderRHandsontable

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

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

我完全同意,如果返回值为 a ,则允许NULL进入hot_to_r并自动调用rhandsontable,可以使这个包更加用户友好,但这是你必须使用的。renderRHandsontabledata.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在反应式上下文中使用。


推荐阅读