golemexamples icon indicating copy to clipboard operation
golemexamples copied to clipboard

golemshinymanager: app_server is called twice

Open dmenne opened this issue 3 years ago • 1 comments

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.

dmenne avatar Aug 18 '22 08:08 dmenne

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.

dmenne avatar Aug 18 '22 08:08 dmenne