r闪亮:从另一个可混音中更新可混音 [英] r shiny: updating rhandsontable from another rhandsontable

查看:102
本文介绍了r闪亮:从另一个可混音中更新可混音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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!

干杯

哈里

推荐答案

rhansontable允许的日期格式

第一个问题是date列的格式.似乎在这里不允许使用POSIXct.根据 rhsonsontable的github文档,建议使用Sys.Date()中的Date.因此,替换

Allowed date formats in rhandsontable

The first problem is the format of the date column. It seems like POSIXct is not allowed here. According to the github documentation of rhandsontable, Date as in Sys.Date() is recommended. So replacing

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")

解决了此问题.警告

警告:错误为:没有将字符"强制为"NA"的方法或默认值

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

调用hot_to_r创建的

现在应该消失了.

created by the call to hot_to_r should be gone now.

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

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.

这是一个完整的解决方案.

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)

这篇关于r闪亮:从另一个可混音中更新可混音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆