r dataTable 保存为 RDS 而不是 jso

r dataTable save as RDS rather than jso

我正在尝试遵循 this example from the dataTable github,它(据我所知)将用户编辑的数据表保存到服务器。但是,我希望它保存为 RDS 而不是 JSON 文件。

DT示例代码:

library(shiny)
library(DT)
shinyApp(
  ui = fluidPage(
    DTOutput('x1')
  ),
  server = function(input, output, session) {
    x = iris
    x$Date = Sys.time() + seq_len(nrow(x))
    output$x1 = renderDT(x, selection = 'none', editable = TRUE)

    proxy = dataTableProxy('x1')

    observeEvent(input$x1_cell_edit, {
      info = input$x1_cell_edit
      str(info)
      i = info$row
      j = info$col
      v = info$value
      x[i, j] <<- DT::coerceValue(v, x[i, j])
      replaceData(proxy, x, resetPaging = FALSE)  # important
    })
  }
)

我可以在自己的 RDS 中正常读取,而且 dataTable 似乎可以很好地对其进行编辑,但我不知道如何将编辑内容保存回原始 RDS。有什么想法吗?

一些背景知识。我正在尝试为我闪亮的应用程序设置一个设置文件。我希望相同的设置能够持久并应用于所有用户(他们两个),并且任何一个用户都能够编辑设置文件。拥有一个可编辑的数据表在我看来是最明显的方法,但我对其他想法持开放态度。

原来我错误地命名了我的变量,这就是它失败的原因。正如 A. Suliman 指出的那样,所有需要的只是添加 "saveRDS(x, ".rds")"。作为对我的罪过的忏悔,我将提供上面代码示例的注释版本,其中包含我认为更直观的变量名称。希望这可以帮助其他人。

library(shiny)
library(DT)
library(textclean)

shinyApp(

  ui = fluidPage(
    DTOutput('tableObject') # name of table object
  ),

  server = function(input, output, session) {
    inputData = readRDS("somewhere.rds") #read data in
    output$tableObject = renderDT(inputData, selection = 'none', editable = TRUE)

    proxy = dataTableProxy('tableObject')

    observeEvent(input$tableObject_cell_edit, { # replace tableObject with your variable name eg. input$example_cell_edit
      info = input$tableObject_cell_edit # and again
      str(info)
      i = info$row
      j = info$col
      v = info$value
      saveRDS(sRego, # save timestamped copy of old data
        mgsub(paste('settings/Rego ', Sys.time(), '.rds', sep = ""),
              pattern = c(":", " "),
              replacement = c("-", "_")))
      inputData[i, j] <<- DT::coerceValue(v, inputData[i, j]) # replace variable
      replaceData(proxy, inputData, resetPaging = FALSE) # replace variable
      saveRDS(inputData, "somewhere.rds") # write new data out

    })
  }
)