I'm trying to follow this example from the dataTable github which (as I understand it) saves a user edited dataTable to the server. However, I would like it to save as an RDS instead of a JSON file.
The DT example code:
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
})
}
)
I can read in my own RDS fine, and dataTable appears to edit it fine, but I can't figure out how to save the edits back to the original RDS. Any ideas?
A bit of background. I am trying to set up a settings file for my shiny app. I want the same settings to be persistent and to apply to all users (both of them) and for either user to be able to edit the settings file. Having an editable datatable appears to me to be the most obvious way to do it, but I'm open to other ideas.
So turns out I'd mis-named my variables which was why it was failing. As A. Suliman pointed out above all that was needed was to add "saveRDS(x, ".rds")". As penance for my sins I'll provide a commented version of the code sample above with what I think is more intuitive variable names. Hopefully that may help other people.
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
})
}
)