将 rhandsontable 保存到 .Rda

save rhandsontable to .Rda

我正在尝试生成一个简单的 table 并将用户输入的值保存到 Rda 文件中。我收到错误...非常感谢任何帮助。

Warning: Error in : $ operator is invalid for atomic vectors

library(shiny)
library(rhandsontable)
library(lubridate)

test_db <- data.frame(d = 20:52,
                      date = seq(ymd("2020/05/15"), by = "7 days", length.out = 33), 
                      i = vector("numeric", length = 33),
                      p = vector("numeric", length = 33),
                      t = vector("numeric", length = 33),
                      dm = vector("numeric", length = 33)
                      )


save(test_db, file = "test_db.Rda")


ui <- fluidPage(


  mainPanel(
    rHandsontableOutput('table'),
    actionButton("save", "Save Changes")
  )
) 


server <- function(input, output){

  data1 = load("test_db.Rda")

  output$table = renderRHandsontable(data1)



  saveData <- function(){
    finalDF <- hot_to_r( input$table )
    save(finalDF, file = "test_db.Rda")
  }

  observeEvent(input$save, saveData())



}


shinyApp(ui = ui, server = server)

问题是你误用了load()或混淆了RData and RDSload() 将存储在该文件中的所有对象不可见地加载到工作区中。 load()的return值如?load():

中所述

A character vector of the names of objects created, invisibly.

因此,不是分配对象本身,而是分配它们的名称!几乎总是,最好使用 RDS 文件。

这段代码能满足您的需求吗?

library(shiny)
library(rhandsontable)
library(lubridate)

test_db <- data.frame(d = 20:52,
                      date = seq(ymd("2020/05/15"), by = "7 days", length.out = 33), 
                      i = vector("numeric", length = 33),
                      p = vector("numeric", length = 33),
                      t = vector("numeric", length = 33),
                      dm = vector("numeric", length = 33)
)
saveRDS(test_db, file = "test_db.RDS")

ui <- fluidPage(
  mainPanel(
    actionButton("save", "Save Changes"),
    rHandsontableOutput('table')
  )
) 

server <- function(input, output){
  data1 <- readRDS("test_db.RDS")

  # render must contain a rhandsontable, not just the data
  my_handsomtable <- rhandsontable(data1)
  output$table  <- renderRHandsontable(my_handsomtable)

  saveData <- function(){
    finalDF <- hot_to_r(input$table)
    saveRDS(finalDF, file = "test_db.RDS")
  }

  observeEvent(input$save, saveData())
}

shinyApp(ui = ui, server = server)