Save changes to an app loaded in shinyapps.io

3

I'm developing an application on Shiny that I'll upload to shinyapps.io .

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

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

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

    
asked by anonymous 10.01.2017 / 20:44

1 answer

2

One possible way is that with each click on the salvar button you save all your data somewhere. This place can be in S3 of amazon, some database, etc. But it can not be the local disk. I recommend using Dropbox.

rdrop2 has a very cool interface.

The best place to find a working example is here .

A simple example, which is what is 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 functions loadData and saveData 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
}

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

    
10.01.2017 / 22:53