r - 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 调用的函数?我发现的复杂例子很难解读。感谢任何反馈。史蒂夫
解决方案
你可以hot_to_r
使用
modified_table <- reactive({
hot_to_r(req(input$table_id)) ## req!
})
访问表的当前状态,包括来自用户的修改。req
需要,因为hot_to_r
无法处理NULL
s。 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
,可以使这个包更加用户友好,但这是你必须使用的。renderRHandsontable
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
在反应式上下文中使用。
推荐阅读
- app-inventor - 如何使我的测验应用程序在每个问题中都没有随机答案?
- reactjs - React / Graphql / Apollo,更新状态和更改路由后不重新获取数据
- node.js - Laravel - 部署后反应不改变(heroku)
- if-statement - 在 SAS 中使用 if between else
- java - 如何修复 NetBeans 8.0.2 输出错误消息?
- powershell - 从子文件夹复制文件并重命名
- amazon-ec2 - 端点 api 网关测试正常通过,但部署时出现错误 404
- mysql - 使用过的命令不允许再次使用
- javascript - 翻译后的div里面的Modal不占整个屏幕
- c# - unity,delegate 无法实现transform