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)
}
}