r 闪亮:从另一个 rhandsontable 更新 rhandsontable

r shiny: updating rhandsontable from another rhandsontable

希望你一切都好。我正在尝试创建一个闪亮的仪表板,用户可以借此从另一个更新一个 rhandsontable。我的代码如下:

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)

每当我 运行 代码时,我都会收到以下错误:

Warning: Error in as: no method or default for coercing “character” to “NA” 

我这辈子都做不到!非常感谢任何帮助!

干杯,

哈利

rhandsontable 中允许的日期格式

第一个问题是 date 列的格式。这里似乎不允许使用 POSIXct。根据the github documentation of rhandsontable,建议DateSys.Date()。所以替换

date.range <- as.POSIXct((seq(start.date,end.date,by="day")), origin = "1970-01-01")

date.range <- as.Date((seq(start.date,end.date,by="day")), origin = "1970-01-01")

解决了这个问题。警告

Warning: Error in as: no method or default for coercing “character” to “NA”

调用 hot_to_r 创建的 hot_to_r 现在应该消失了。

同时更新两个表

为了使 table1 中的所有更改影响 table2,反之亦然,您可以使用相同的反应值在服务器端存储表。

这是一个完整的工作解决方案。

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)