How do you locally save a reactive data frame that is modified during an R session, for use in your next R session? Whether that next R session involves restarting R, logging off/on your computer, etc. Without prompting the user to download and save a .csv or other file; I assume the best way to do this is to save the data frame to some type of temporary or archive folder on the user’s computer but I don't know how to set it this up. The user simply clicks the "Save" action button (a placeholder in the below MWE code) and the saved data frame becomes available for the next session. If the user doesn't click "Save", then the last saved data frame is retrieved when restarting the session, or if there is no archived data frame, then the session simply starts with the default data frame data.frame(x = c(1, 2, 3))
per the below MWE code.
The data will not be the same for all sessions and all connections: if one user updates the data, it would only affect the data that the user sees and no other user.
From researching this I understand there are many options, but I am looking for something simple for a relative newcomer like me to get started. Saving can be made more sophisticated later if advisable.
I'll add a "Reset" button later so the user can revert back to the original data frame in this example of data.frame(x = c(1, 2, 3)
).
MWE code:
library(rhandsontable)
library(shiny)
myDF <- data.frame(x = c(1, 2, 3))
ui <- fluidPage(
br(),
fluidRow(
column(6,
actionButton('addCol','Add'),
actionButton('savTbl','Save')
)
),
br(),
rHandsontableOutput('hottable')
)
server <- function(input, output, session) {
EmptyTbl <- reactiveVal(myDF)
observeEvent(input$hottable, {
EmptyTbl(hot_to_r(input$hottable))
})
output$hottable <- renderRHandsontable({
rhandsontable(EmptyTbl(),useTypes = FALSE)
})
observeEvent(input$addCol, {
newCol <- data.frame(c(1, 2, 3))
names(newCol) <- paste("Col", ncol(hot_to_r(input$hottable)) + 1)
EmptyTbl(cbind(EmptyTbl(), newCol))
})
}
shinyApp(ui, server)