Rachel
Rachel

Reputation: 129

R Shiny app multi-user authentication and Google Drive access

I would like to create a Shiny app in R where multiple users can log in to their personal Google account before the app loads. I was using the googleAuthR package (gar_shiny_auth() function) to do this. I need more than one user to be able to login to Google and access Google Drive. After authentication, the app automatically accesses Google Drive and downloads a Google Sheet, always named the same regardless of the user (e.g., Monitoring). Eventually I want to download three sheets, but for testing purposes, one is fine. Also, instead of downloading, each sheet could be saved to a variable of the same name Monitoring. I need to be able to manipulate the data (i.e., filtering, mutating, etc). Then the app creates a dashboard from those dataframes.

I have looked at quite a few stackoverflow questions, such as Multi-user authentication for googlesheets on an app deployed to shinyapps.io and How do you implement Google authentication in a shiny R app?. I also looked at package GoogleAuthR, googledrive, and googlesheets4, and I just can't figure out where I am going wrong. The following code throws an error when publishing to shinyapps.io "Cannot find Google credentials"


library("shiny")
library("shinydashboard")
library("shinyjs")
library("googleAuthR")
library("googlesheets4")
library("googledrive")
library("readxl")

# GOOGLE DRIVE

gar_set_client(web_json = "credentials.json", scopes = "https://www.googleapis.com/auth/drive", activate="web")


fileSearch <- function(query) {
  googleAuthR::gar_api_generator("https://www.googleapis.com/drive/v3/files/",
                                 "GET",
                                 pars_args=list(q=query),
                                 data_parse_function = function(x) x$files)()
}

## ui.R
ui <- fluidPage(title = "googleAuthR Shiny Demo",
                textInput("query", 
                          label = "Google Drive query", 
                          value = "mimeType != 'application/vnd.google-apps.folder'"),
                tableOutput("gdrive")
)

## server.R
server <- function(input, output, session){
  gar_shiny_auth(session)
  
  output$gdrive <- renderTable({
    #Directly access the file "monitoring"
    file_name <- "Monitoring"
    drive_file <- googledrive::drive_find(pattern = file_name)
    
    # Check if the file was found
    if (!is.null(drive_file)) {
      # Read the file content using googledrive::drive_download()
      file_content <- googledrive::drive_download(as_id(drive_file$id), type = 'csv')
      
      # Read it into a data frame
      df <- read.csv(text = file_content)
      return(df)
    } else {
        return(data.frame())
     
    }
  })
}


shinyApp(gar_shiny_ui(ui), server)

Upvotes: 1

Views: 332

Answers (0)

Related Questions