how to make access to googlesheet work with shinyapp.io

1,937 views
Skip to first unread message

p...@lbl.gov

unread,
Jun 8, 2017, 12:09:19 PM6/8/17
to Shiny - Web Framework for R
Dear Group,
My goal is to write several parameters from each session (started by different users) of a shiny app to a googlesheet. I used googlesheet package to add a row to an existing googlesheet:
    gapgeo<-gs_title("test-gs_geo2")
    gs_add_row(gapgeo,ws="Sheet1",input=c(geo_timestamp,latitude,longititude), verbose = FALSE)

The test on my desktop Rstudio works great. There is a .httr-oauth in my home directoryI think used to authenticate and access the correct googlesheet. I tried to copy this file to the shiny app directory the deploy to shinyapp.io. This function did not work on the shinyapp.io, I got no input in the googlesheet(error: readRDS(file) : unknown input format). My guess is it has problem locating the right googlesheet. How do I solve this issue? Or is there any alternatives, such as , can I write into a local file and is there a way to assess that file? 


Ping

Conal Monaghan

unread,
Jun 17, 2017, 1:04:51 AM6/17/17
to Shiny - Web Framework for R
Hi Ping, Let me know if this helps. The app below should work once you alter the google login for your settings. As soon as the participant clicks the submit button, their data is added to google sheets instantly.

 First, one needs to setup the authorisation token and a worksheet in googlesheets to use. See below for the setup that works with the app below. 

    Note that before this will work you will need to run the following in your console (once installed googlesheets() ) 
                 1)   ttt <- gs_auth()                                                       # now follow the html. prompts to login
                 2) saveRDS(ttt, "ttt.rds")                                              # then copy ttt.rds file to the app's Dir() 
                 3) Now one needs to create a worksheet to use using the following code:

         Data <- gs_new("Data") %>% 
                    gs_ws_rename(from = "Sheet1", to = "Data")      
 
                 4) Insert the titles that we want
           Data <- Data %>% 
                       gs_edit_cells(ws = "Data", input = cbind("Score1", "Score2", "Time", "Mean"), trim = TRUE)                # Note! you will need to have manually add one row of data to your google sheet otherwise it will error




UI
----------------------------------------------------------------------------------------

library(shiny)
library("googlesheets")                                                                                                                                                  # Don't forget to install.packages("googlesheets")
library("DT")                                                                                                                                                                   # Don't forget to install.packages("DT")

shinyUI(fluidPage(                                                                                                                                                          # Open UI
  "survey demo",                                                                                                                                                             # Title 
  
  # Questions 
  selectInput(inputId = "Score1",                                                                                                                                     # What we are calling the object
              label = "The Question 1",                                                                                                                                 # Question
              choices = c("Disagree Strongly" = 1, "Disagree" = 2, "Disagree Somewhat " = 3,                                         # Responses
                          "Neither Agree nor Disagree" = 4, "Agree Somewhat" = 5, "Agree" = 6,
                          "Agree Strongly" = 7)            
             ), 
  
  selectInput(inputId = "Score2",                                                                                                                                  # What we are calling the object
              label = "The Question 1",                                                                                                                              # Question
              choices = c("Disagree Strongly" = 1, "Disagree" = 2, "Disagree Somewhat " = 3,                                        # Responses
                          "Neither Agree nor Disagree" = 4, "Agree Somewhat" = 5, "Agree" = 6,
                          "Agree Strongly" = 7)            
             ), 
  
  radioButtons("radio", label = h3("Do you consent to having your anonymous data stored for research"),                # insert Radio for data storage consent
               choices = list("Sure" = 1, "No, erase all evidence I was here" = 2
                              )),
  # Submitbutton
  actionButton(inputId = "Action", label = "Submit"), tags$hr(),                                                                                   # Create submit action button
  
  dataTableOutput('mytable')                                                                                                                                      # Data display with downloads using DT
          ))                                                                                                                                                                      # Close Shiny UI and fluid page



Server
---------------------------------------------------------------------------------------------------------------
library(shiny)
library("googlesheets")                                                                                                                                                  # Don't forget to install.packages("googlesheets")
library("DT")                                                                                                                                                                   # Don't forget to install.packages("DT")
suppressMessages(library(dplyr))                                                                                                                                 # Don't forget to install.packages("dplyr")

gs_auth(new_user = FALSE, gs_auth(token = "ttt.rds"))                                                                                              # login from token file "ttt.rds" in dir(). see post for how to do this

shinyServer(function(input, output) {                                                                                                                           #  Open Shiny Server
   
Results <- reactive(c(Score1, Score2, Sys.Date(), mean(Score1,Score2)))                                                              #  Create reactive data to input
  
                                    ##### Function 1, Add data when action button is pressed  ####
                                                    
observeEvent(input$Action, {                                                                                                                                        #  Observe event action from Actionbutton
  Data <- Data %>% 
    gs_add_row(ws = "Data", input = Results() )                                                                                                             #  When actionbutton is pressed this will add their data to the good .doc                    
                           })
                                                                              
                
                             #####     Function 2, Create a data display with download option     ####

output$mytable <- renderDataTable({gs_read(Data)}, filter = 'top', extensions = c('Scroller', 'Buttons'), options = list(         # use the Scroller and  Buttons extensions for scroll options and download options respectively
  dom = 'Bfrtip',
  buttons = 
    list('copy', 'print', list(
      extend = 'collection',
      buttons = c('csv', 'excel', 'pdf'),                                                                                                                                           #  Modify the downloads extension to make it nicer (places download options in menu under "Download")
      text = 'Download'
                               )  # close list
                               )  # close list
                                                                                           )                                                                                                   # close list
                                 ,  caption = 'Table X: This is a simple caption for the table.')                                                                 # Adds the caption. Note, I tried to add " %>% formatDate('Time', 'toDateString') " here but does not work
                                     }                                                                                                                                                         # Close shiny server function
            )                                                                                                                                                                                  # Close shiny server








Conal Monaghan

unread,
Oct 23, 2017, 8:46:36 AM10/23/17
to Shiny - Web Framework for R
Hi Ping,
      Did this solution work? just so that anyone else who has similar issues will know what happened. 

 Cheers,
     Conal
Reply all
Reply to author
Forward
0 new messages