[英]r shiny: updating rhandsontable from another rhandsontable
I hope you're well. 我希望你很好。 I am trying to create a shiny dashboard whereby a user is able to update one rhandsontable from another. 我正在尝试创建一个闪亮的仪表板,使用户能够从另一个仪表盘更新另一个。 My code is as follows: 我的代码如下:
library(shiny)
library(rhandsontable)
channel <- c("TV","Radio","Digital")
start.date <- as.Date("2017-01-01")
end.date <- as.Date("2017-01-07")
date.range <- as.POSIXct((seq(start.date,end.date,by="day")), origin = "1970-01-01")
date.range <- as.data.frame(date.range)
colnames(date.range) <- c("date")
date.range[channel] <- 0
table1 <- date.range
table2 <- date.range
#Define the tables.
ui <- fluidPage(
br(),
fluidRow(
column(4, rHandsontableOutput("table1output")),
column(4, rHandsontableOutput("table2output"))
))
server <- function(input,output,session){
table <- reactiveValues()
table$table1 <- table1
table$table2 <- table2
#define reactive values as table1 and table2
output$table1output <- renderRHandsontable({rhandsontable(table$table1)})
output$table2output <- renderRHandsontable({rhandsontable(table$table2)})
#rhandsontable outputs
observeEvent(input$table1output,{
df <- hot_to_r(input$table1output)
df <- as.data.frame(df)
table$table2 <- df
})
#if a user updates table1 table2 should also update.
observeEvent(input$table2output,{
df <- hot_to_r(input$table2output)
df <- as.data.frame(df)
table$table1 <- df
})
#if a user updates table2 table1 should also update.
}
shinyApp(ui = ui, server = server)
Whenever I run the code i get the following errors: 每当我运行代码时,都会出现以下错误:
Warning: Error in as: no method or default for coercing “character” to “NA”
I can't for the life of me get this to work! 我一辈子都做不到! Any help would be very much appreciated! 任何帮助将不胜感激!
Cheers, 干杯,
Harry 掠夺
The first problem is the format of the date
column. 第一个问题是date
列的格式。 It seems like POSIXct
is not allowed here. 似乎这里不允许POSIXct
。 According to the github documentation of rhandsontable , Date
as in Sys.Date()
is recommended. 根据rhandsontable的github文档,建议使用Sys.Date()
Date
。 So replacing 所以更换
date.range <- as.POSIXct((seq(start.date,end.date,by="day")), origin = "1970-01-01")
with 同
date.range <- as.Date((seq(start.date,end.date,by="day")), origin = "1970-01-01")
solves this issue. 解决了这个问题。 The warning 警告
Warning: Error in as: no method or default for coercing “character” to “NA” 警告:错误为:没有将“字符”强制转换为“ NA”的方法或默认方法
created by the call to hot_to_r
should be gone now. 调用hot_to_r
创建的创建现在应该消失了。
In order to make all changes in table1
affect table2
and vice versa, you can use the same reactive value to store the tables on the server side. 为了使table1
所有更改都影响table2
,反之亦然,您可以使用相同的反应性值将表存储在服务器端。
Here is a full working solution. 这是一个完整的工作解决方案。
library(shiny)
library(rhandsontable)
channel <- c("TV","Radio","Digital")
start.date <- as.Date("2017-01-01")
end.date <- as.Date("2017-01-07")
date.range <- as.Date((seq(start.date,end.date,by="day")), origin = "1970-01-01")
date.range <- as.data.frame(date.range)
colnames(date.range) <- c("date")
date.range[channel] <- 0
table1 <- date.range
table2 <- date.range
#Define the tables.
ui <- fluidPage(
br(),
fluidRow(
column(4, rHandsontableOutput("table1output")),
column(4, rHandsontableOutput("table2output"))
))
server <- function(input,output,session){
table <- reactiveValues()
table$table1 <- table1
#DEFINE ONLY TABLE1
output$table1output <- renderRHandsontable({rhandsontable(table$table1)})
output$table2output <- renderRHandsontable({rhandsontable(table$table1)})
#rhandsontable outputs
observeEvent(input$table1output,{
df <- hot_to_r(input$table1output)
df <- as.data.frame(df)
table$table1 <- df
}, ignoreInit = TRUE, ignoreNULL = TRUE
)
#if a user updates table1 table2 should also update.
observeEvent(input$table2output,{
df <- hot_to_r(input$table2output)
df <- as.data.frame(df)
table$table1 <- df
}, ignoreInit = TRUE, ignoreNULL = TRUE
)
#if a user updates table2 table1 should also update.
}
shinyApp(ui = ui, server = server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.