Flexdashboard,rhansontable:如何以编程方式访问用户更新的表? [英] Flexdashboard, rhandsontable: how to programmatically access user updated table?
问题描述
不是一个闪亮的程序员.简单的问题.在Flexdashboard应用程序中进行rhansontable.如何访问用户更新的列?示例代码:
Not a Shiny programmer. Simple question. rhandsontable in Flexdashboard app. How to access a column updated by the user? Sample code:
---
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调用的函数呢?我发现的复杂示例很难解读.感谢任何反馈.史蒂夫·M
So the code renders the table. A user could update the table by editing the table cells. But then how to reference the updated table to pass table columns to a function called with an actionButton? The complex examples I've found are difficult to decipher. Appreciate any feedback. SteveM
推荐答案
您可以按以下方式使用hot_to_r
you can use hot_to_r
as in
modified_table <- reactive({
hot_to_r(req(input$table_id)) ## req!
})
可以访问表的当前状态,包括用户的修改.因为hot_to_r
无法处理NULL
,所以需要req
. table_id
应该是用于renderRHandsontable
返回值的输出ID.
to get access to the current state of the table including modifications from the user. req
is needed because hot_to_r
can't deal with NULL
s. table_id
should be the output-id you use for the return value of renderRHandsontable
.
output$table_id <- renderRHandsontable({
rhandsontable(initial_table) ## need to call converter
})
您所指的复杂示例(例如这一个,# 64-81)允许表的双向连接,从某种意义上说,可以从用户和从服务器进行更新.但是,在此概述的简单设置中,modified_table
是用reactive
创建的,因此只能由用户更新.
The complex examles you are referring to (like this one, #64-81) allow a two-way connection of the tables in the sense that they can be updated both from the user and from the server. In this simple setup I outlined here however, modified_table
is created with reactive
so it can only be updated by the user.
我完全同意,如果返回值是data.frame
,则可以通过允许hot_to_r
中的NULL
并通过自动调用renderRHandsontable
中的rhandsontable
来使此程序包更加用户友好.必须与之合作.
I totally agree that this package could be made more user friendly by allowing NULL
in hot_to_r
and by automatically calling rhandsontable
in renderRHandsontable
if the return value is a data.frame
but this is what you will have to work with.
这是一个演示此设置的完整应用
Here is a full app demonstrating this setup
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
.
In order to access a specific column, you can use modified_table()$column_name
inside a reactive context.
这篇关于Flexdashboard,rhansontable:如何以编程方式访问用户更新的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!