ashokkrish / CougarStats

5 stars 2 forks source link

Sourcing files in global.R has side-effects #29

Closed bryce-carson closed 1 month ago

bryce-carson commented 1 month ago

Unless _all of the definitions in all the files__ in the R folder are idempotent and purely functional (without side effects) then the side effects are less predictable and may change if a new file is added to the load order without care.

https://github.com/ashokkrish/CougarStats/blob/0461b95186f49f51b2e3b1d28e22520455d3d055/global.R#L31-L55

For more information see https://shiny.posit.co/r/reference/shiny/latest/loadsupport.

[!note]

loadSupport

Loads all of the supporting R files of a Shiny application. Specifically, this function loads any top-level supporting .R files in the R/ directory adjacent to the app.R/server.R/ui.R files.

loadSupport(
  appDir = NULL,
  renv = new.env(parent = globalenv()),
  globalrenv = globalenv()
)

Details

Since Shiny 1.5.0, this function is called by default when running an application. If it causes problems, there are two ways to opt out. You can either place a file named "_disable_autoload.R" in your R/ directory, or set options(shiny.autoload.r = FALSE). If you set this option, it will affect any application that runs later in the same R session, potentially breaking it, so after running your application, you should unset option with options(shiny.autoload.r = NULL)

The files are sourced in alphabetical order (as determined by list.files). global.R is evaluated before the supporting R files in the R/ directory.

Emphasis added

bryce-carson commented 1 month ago

I'm going to close this for now because it's not something we will work on.