Save changes from an app loaded to shinyapps.io

Question:

I'm developing an app in Shiny that I'm going to upload to shinyapps.io .

It consists of an already loaded table from which the user can select some row, and then a report is generated based on the selected row.

In addition, the user can change some data for the selected row and then an updated report is generated. However, the changed data only remains while the line is selected.

What I want is that when changing some data, the user can press a button to save this change. Thus, when he or another user enters the app's link, the changed data is already updated with the changes.

Answer:

One possible way is that each click on the salvar button saves all your data somewhere. This place could be on amazon's S3, some database, etc. But it can't be the local disk. I recommend using Dropbox.

rdrop2 has a really nice interface.

The best place to find a working example ishere .

A simple example, which is what's there is the following app:

library(shiny)

# Define the fields we want to save from the form
fields <- c("name", "used_shiny", "r_num_years")

# Shiny app with 3 fields that the user can submit data for
shinyApp(
  ui = fluidPage(
    DT::dataTableOutput("responses", width = 300), tags$hr(),
    textInput("name", "Name", ""),
    checkboxInput("used_shiny", "I've built a Shiny app in R before", FALSE),
    sliderInput("r_num_years", "Number of years using R", 0, 25, 2, ticks = FALSE),
    actionButton("submit", "Submit")
  ),
  server = function(input, output, session) {

    # Whenever a field is filled, aggregate all form data
    formData <- reactive({
      data <- sapply(fields, function(x) input[[x]])
      data
    })

    # When the Submit button is clicked, save the form data
    observeEvent(input$submit, {
      saveData(formData())
    })

    # Show the previous responses
    # (update with current response when Submit is clicked)
    output$responses <- DT::renderDataTable({
      input$submit
      loadData()
    })     
  }
)

With the loadData and saveData functions defined as follows:

library(rdrop2)
outputDir <- "responses"

saveData <- function(data) {
  data <- t(data)
  # Create a unique file name
  fileName <- sprintf("%s_%s.csv", as.integer(Sys.time()), digest::digest(data))
  # Write the data to a temporary file locally
  filePath <- file.path(tempdir(), fileName)
  write.csv(data, filePath, row.names = FALSE, quote = TRUE)
  # Upload the file to Dropbox
  drop_upload(filePath, dest = outputDir)
}

loadData <- function() {
  # Read all the files into a list
  filesInfo <- drop_dir(outputDir)
  filePaths <- filesInfo$path
  data <- lapply(filePaths, drop_read_csv, stringsAsFactors = FALSE)
  # Concatenate all data together into one data.frame
  data <- do.call(rbind, data)
  data
}

Note that you will need to setup rdrop2, which is well explained on the package page.

Scroll to Top