Save changes from a loaded app to shinyapps.io

Asked

Viewed 116 times

3

I’m developing an app in Shiny that I’m going to upar at shinyapps.io.

It consists of an already loaded table that 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 from 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. So, when he or another user enters the app link the changed data is already updated with the changes.

1 answer

2


One possible way is that every click on the button salvar you save all your data somewhere. That place might be on the S3 of Amazon, some database, etc. But it might not be the local disk. I recommend using Dropbox.

The rdrop2 has a very cool interface.

The best place to find a working example is here.

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 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 will need to setup rdrop2, which is well explained on the package page.

  • Man, thank you so much, I already did some tests with the shinyapps.io and everything is going well! I made the scheme to create a token with token<-drop_auth() and click on?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.