Reputation: 7
I am creating a questionnaire using shiny and I would like to save the answers on dropbox. Everything works fine for a while if I run on its own drop_auth() and then I manually consent the access once the internet window is open. After I do that, I can also upload the app on shinyappsio and it save correctly the results, but after a few hours it stops saving the answers and when the button "submit" is pressed, an error appear saying "Disconnected from the server". I believe that this has to do with the fact that the tokens expire every 4 hours, but I cannot figure out how to solve this issue. I have taken the information from these links https://shiny.posit.co/r/articles/build/persistent-data-storage/#dropbox, https://deanattali.com/blog/shiny-persistent-data-storage/ and I was trying to use the authentication method described here for the package rdrop2 https://github.com/karthik/rdrop2#accessing-dropbox-on-shiny-and-remote-servers. Also, when I publish the app I make sure to include the file "httr_oauth" in the directory.
This is my code for the dropbox authentication and for saving the results
token <- drop_auth()
saveRDS(token, "droptoken.rds")
library(dplyr)
drop_acc() %>% data.frame()
token <- readRDS("droptoken.rds")
# Then pass the token to each drop_ function
drop_acc(dtoken = token)
fields <- c("a", "b", "c")
outputDir <- "responses"
delimiter <- ";"
saveData <- function(data) {
# Combine multiple answers into a single cell with the delimiter
for (field in fields) {
data[[field]] <- paste(data[[field]], collapse = delimiter)
}
# 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, path = outputDir)
}
loadData <- function() {
# Read all the files into a list
filesInfo <- drop_dir(outputDir)
filePaths <- filesInfo$path_display
data <- lapply(filePaths, drop_read_csv, stringsAsFactors = FALSE)
# Concatenate all data together into one data.frame
data <- do.call(rbind, data)
# Split multi-answer fields back into separate elements using the delimiter
for (field in fields) {
data[[field]] <- strsplit(data[[field]], delimiter)
}
data
}
At the end of the server I have then this code once the "submit" button is clicked:
formData <- reactive({
data <- sapply(fields, function(x) input[[x]])
data
})
observeEvent(input$submit, {
saveData(formData())
showModal(modalDialog(
title = "Submission Successful",
"Thank you for submitting your answers!",
footer = modalButton("Close")
))
})
Thanks to anyone that could help me!
Upvotes: 0
Views: 151