golemexamples
golemexamples copied to clipboard
golemshinymanager: app_server is called twice
I noted that app_server in the shinymanager example is called twice, once before the login screen and once after.
Since global.R is not considered state-of-the art, where should I open the database connection to avoid doing it twice?
See also you comment :
things that need to be launched at runtime should be listed in the app_server function
Since https://github.com/ThinkR-open/golem/issues/6#issuecomment-518129149 was left in Limbo (use golem_options, but how, and how to close the connection), I have no clean solution for the case.
My workaround:
run_app <- function(
onStart = function(){
g <<- globals()
onStop(function() {
poolClose(g$pool)
})
},
...
And a file globals.R (not global.R):
# This is file globals(sic!).R, not Shiny special global.R
# Variables in this function are can be accessed via g$,
# as defined in run_app.R
# Warning: globals: no visible binding for '<<-' assignment to 'g'
utils::globalVariables("g")
globals = function(){
config = get_golem_config(NULL)
# Unencrypted SQLite for data
pool = create_data_tables_and_pool(config$sqlite_dir)
# Encrypted SQLite for shinymanager
create_auth_tables(config$sqlite_dir, config$passphrase)
mget(ls())
}
Can use g$config, g$pool everywhere.