Pass custom arguments to the Shiny app via URL using app_direct?

Hi all,

A few years back the following question was posed and answered regarding passing arguments to a Shiny application via the url (and for reference, the solution is to use session$clientData within your app):

The solution now works perfectly for apps at the endpoint my.address:8080/app/example?arg=value, however this does not work when using app_direct, e.g. my.address:8080/app_direct/example?arg=value.

Is it possible to extend this functionality to work when accessing your application via app_direct also?

Many thanks to the creators for a fantastic tool!

Best,
Jack

Hi Jack,

I have had success doing this in shiny by wrapping my ui in a function in the ui.R script. Then you can intercept the initial request to your application, and read the parameters before the rest of the ui or server.R runs. Note, this will still happen after your global.R runs.

Here’s an example . You can see I took it from a Hadley gist. In this case, our app is looking for either an OAuth2 token (if not running in ShinyProxy), a health check parameter, or skips it all if ShinyProxy has the environment variable for the OAuth2 token already. In this particular example, we also redirect away from the app if we need the user to login to the OAuth2 service.

ui <- fluidPage(... # All your ui stuff here)

uiFunc <- function(req) { #https://gist.github.com/hadley/144c406871768d0cbe66b0b810160528

  # Pull query parameters off the URL
  params <- parseQueryString(req$QUERY_STRING)

  # Check for the health-check parameter; If it's TRUE, return the health-check page
  if (isTRUE(params$health==TRUE)) {
    utcDttm <- Sys.time()
    attr(utcDttm, "tzone") <- "UTC"
    
    # Health-check HTML page
    jsonlite::toJSON({
      list("status" = "Healthy",
           "utc_dttm" = utcDttm,
           "AWS_EXECUTION_ENV" = Sys.getenv("AWS_EXECUTION_ENV"),
           "ECS_CONTAINER_METADATA_URI" = Sys.getenv("ECS_CONTAINER_METADATA_URI")
      )
    }, auto_unbox = TRUE)
  
  } else if (is.null(params$code) && !offline && Sys.getenv("SHINYPROXY_OIDC_ACCESS_TOKEN") == "") {   # Re-direct to MS for OAuth2 if we're not in ShinyProxy / offline 
    url <- oauth2.0_authorize_url(endpoint,app,scope="files.readwrite.all")
    redirect <- sprintf("location.replace(\"%s\");",url)
    tags$script(HTML(redirect))
  } else {  # The user has authed with MS (either through ShinyProxy or via previous re-direct) or we're offline
    return(ui)
  }
}

I should say I haven’t tried this with the ShinyProxy app direct url.