首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R闪亮:从另一个rhandsontable更新rhandsontable

R闪亮:从另一个rhandsontable更新rhandsontable
EN

Stack Overflow用户
提问于 2017-06-28 21:09:56
回答 1查看 1.8K关注 0票数 2

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

代码语言:javascript
复制
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)

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

代码语言:javascript
复制
Warning: Error in as: no method or default for coercing “character” to “NA” 

我不能为了我的一生让它起作用!任何帮助都是非常感谢的!

干杯,

哈里

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-29 00:00:52

rhandsontable中允许的日期格式

第一个问题是date列的格式。这里似乎不允许使用POSIXct。根据rhandsontable的github文档的说法,DateSys.Date()一样是推荐的。所以取代

代码语言:javascript
复制
date.range <- as.POSIXct((seq(start.date,end.date,by="day")), origin = "1970-01-01")

使用

代码语言:javascript
复制
date.range <- as.Date((seq(start.date,end.date,by="day")), origin = "1970-01-01")

解决了这个问题。警告

警告:在as中出错:没有强制“字符”为“NA”的方法或默认设置

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

同时更新两个表

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

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

代码语言:javascript
复制
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)
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44812595

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档