Is there a way to detect if an app is running on shinyapps.io?

I wondered if there was an environment argument or similar to detect if the Shiny app is running on shinyapps.io. Reason being I need to enact special behaviour for shinyapps.io applications for my authentication flows based on this OAuth2 flow by Joe Cheng.

Locally and on my local Shiny server applications everything works ok, but on Shinyapps.io the applications are running without loading the options set, defaulting to the package default, as well as the Rook request is not reading the URL, instead showing a localhost URL with a random port number I can't detect before deploying the app. I would like to give users the option to hardcode the URL to redirect too, but only if the application is on shinyapps.io. An example is this test deployment which is running the below code:

library(shiny)
library(googleAuthR)
library(googleAnalyticsR) 
library(tidyverse)

## attempt to set options
options(googleAuthR.webapp.client_id = "1080525199262-qecndq7frddi66vr35brgckc1md5rgcl.apps.googleusercontent.com",
        googleAuthR.webapp.client_secret = "XXX",
        googleAuthR.scopes.selected = "https://www.googleapis.com/auth/analytics.readonly",
        googleAuthR.client_id = "1080525199262-outv8jk106qla9e9n03pfrcclqkk7vsa.apps.googleusercontent.com",
        googleAuthR.client_secret = "XXXX",
        googleAuthR.verbose = 1)

log_option <- function(x){
  cat("\n Option: ",x, " ",getOption(x))
}

## ui.R
ui <- fluidPage(title = "googleAnalyticsR Test Deployment",
                
      authDropdownUI("auth_menu"),
      
      textOutput("viewid"),
      textOutput("client_id")
      
)

## server.R
server <- function(input, output, session){

  gar_shiny_auth(session)
  
  al <- reactive(ga_account_list())
  view_id <- callModule(authDropdown, "auth_menu", ga.table = al)
  
  output$viewid <- renderText(view_id())
  
  output$client_id <- renderText(getOption("googleAuthR.webapp.client_id"))

}
log_option("googleAuthR.client_id")
log_option("googleAuthR.webapp.client_id")

shinyApp(gar_shiny_ui(ui, login_ui = silent_auth), server)

1 Like

The recommended approach here is to use the config package (which is set to shinyapps on Shinyapps.io).

Some docs on the topic, focused on databases but relevant for any code that you might need to alter based on environment: https://docs.rstudio.com/shinyapps.io/applications.html#config-package

Explained generically here as well: https://db.rstudio.com/best-practices/portable-code/

2 Likes

Great thanks! Will check that out.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.