My Shiny application has several inputs that are used to determine several parameters of the generated chart. It is very likely that the user will spend several minutes going through all the possible options until he is satisfied with the result. Obviously, the plot can be exported in different formats, but it is possible that the user will want to recreate the same plot with different data later, or maybe just change one small detail.
Because of this, I need to offer the user a way to export all of his settings and save this file for later use. I developed an approach, but it does not work well. I use reactiveValuesToList to get the names of all input elements and save as a simple text file with the format inputname=inputvalue . This is the downloadHandler on server.R :
output$bt_export <- downloadHandler( filename = function() { "export.txt" }, content = function(file) { inputsList <- names(reactiveValuesToList(input)) exportVars <- paste0(inputsList, "=", sapply(inputsList, function(inpt) input[[inpt]])) write(exportVars, file) })
This works fine, but the download is not going very smoothly. Since I did not (and could not figure out how) to save the input type, I have to update the values blindly. Here is how I do it:
importFile <- reactive({ inFile <- input$fileImport if (is.null(inFile)) return(NULL) lines <- readLines(inFile$datapath) out <- lapply(lines, function(l) unlist(strsplit(l, "="))) return(out) }) observe({ imp <- importFile() for (inpt in imp) { if (substr(inpt[2], 0, 1) == "#") { shinyjs::updateColourInput(session, inputId = inpt[1], value = inpt[2]) } else { try({ updateTextInput(session, inputId = inpt[1], value = inpt[2]) updateNumericInput(session, inputId = inpt[1], value = inpt[2]) updateSelectInput(session, inputId = inpt[1], selected = inpt[2]) }) } } })
Besides the shinyjs::colorInput , which can be recognized by the beginning of # , I have to use try() for the rest. This works partially, but some inputs are not updated. Checking the exported file manually shows that there is input that has not been updated, so I believe updating 100+ entries right away is not a good idea. Also the try() does not look very good and probably not a good idea.
The application is close to completion, but is likely to be updated in the future by adding / modifying some inputs. This is acceptable if it even makes some of the "old" exported inputs invalid, as I try to maintain backward compatibility. But I'm looking for an approach that doesn't just write hundreds of lines to update inputs one by one.
I was thinking about using save.image() , but just using load() does not restore application inputs. I also looked at a way to somehow update all the inputs at once, and not one by one, but came up with nothing. Is there a better way to export all user inputs to a file and then load them all? It does not matter if it improves this work, or if a completely different approach works.