I have created an app in R shiny app to model how users choose which products to buy. The app records how users interact with it, primarily which buttons they click when choosing between two purchases (with multiple possible combinations). It saves users' interactions using observeEvent() to update a data.frame (results.df) within a list (user.instance). The list contains the choices the user made and the products that they were shown on the app to decide which one to purchase. The data in results.df is then appended to a Google Sheet using googlesheets4.
The issue that I am having is that the app works perfectly, writing to the required google sheet until it is deployed to shinyapps.io. I am using a non-interactive token generated through the Google Cloud API to authenticate, storing the credentials in a .json which is uploaded to shiny.io with app.R. The code for this is below. I have also added the service account email from the Google Cloud to the Google Sheet as an Editor.
# Google sheets authentification
# designate project-specific cache
options(gargle_oauth_cache = ".secrets")
When I access a Google Sheet using googlesheets4::read_sheet() it works perfectly, both locally and remotely. However, the app does not read or write to the Google Sheet when deployed to shiyapps.io. The code I have for this is below.
# Error catching, if data already exists
# ss: the id of the Google Sheet
## -----------------------------------------------------------------------------
## Server
## -----------------------------------------------------------------------------
server <- function(input, output, session) {
## ------------------------------------------
## Record Results on Tab Close
## ------------------------------------------
onStop(function() {
# Product Profiles Shown
results.df <<- as.data.frame(cj$sample)
ss <- "google_sheets_id"
tryCatch(expr = {
sheet_append(ss, results.df)
},
error = function(e) {
sheet_write(results.df, ss)
})}
There is no error in the logs on the shiny apps.io dashboard. I have included the output below.
Using an auto-discovered, cached token.
To suppress this message, modify your code or options to clearly consent to the use of a cached token.
The googlesheets4 package is using a cached token for
See gargle's "Non-interactive auth" vignette for more details:
<https://gargle.r-lib.org/articles/non-interactive-auth.html>
'myemail@gmail.com'
✔ Reading from "experimental_design_complete".
✔ Range 'experimental_design_complete'.
Shiny application exiting ...
Any help appreciated! I haven't been able to find an answer to this question anywhere and there is no error code or warning in the logs to indicate what exactly is going wrong. I have also added the sheet_write() outside of the tryCatch() but nothing changed.