Skip to content

Commit 37257e7

Browse files
committed
Disable autoloading with a R/_disable_autoload.R file.
1 parent 270d9ff commit 37257e7

File tree

9 files changed

+26
-6
lines changed

9 files changed

+26
-6
lines changed

R/app.R

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,9 @@ initAutoReloadMonitor <- function(dir) {
314314
#' adjacent to the `app.R`/`server.R`/`ui.R` files.
315315
#'
316316
#' Since Shiny 1.5.0, this function is called by default when running an
317-
#' application. If it causes problems, you can opt out by using
318-
#' `options(shiny.autoload.r=FALSE)`. Note that in a future version of Shiny,
319-
#' this option will no longer be available. If you set this option, it will
317+
#' application. If it causes problems, there are two ways to opt out. You can
318+
#' either place a file named `_disable_autoload.R` in your R/ directory, or
319+
#' set `options(shiny.autoload.r=FALSE)`. If you set this option, it will
320320
#' affect any application that runs later in the same R session, potentially
321321
#' breaking it, so after running your application, you should unset option with
322322
#' `options(shiny.autoload.r=NULL)`
@@ -339,6 +339,13 @@ loadSupport <- function(appDir, renv=new.env(parent=globalenv()), globalrenv=glo
339339
}
340340

341341
helpersDir <- file.path(appDir, "R")
342+
343+
disabled <- list.files(helpersDir, pattern="_disable_autoload\\.r$", recursive=FALSE, ignore.case=TRUE)
344+
if (length(disabled) > 0){
345+
message("R/_disable_autoload.R detected; not loading the R/ directory automatically")
346+
return(invisible(renv))
347+
}
348+
342349
helpers <- list.files(helpersDir, pattern="\\.[rR]$", recursive=FALSE, full.names=TRUE)
343350

344351
if (length(helpers) > 0){

man/loadSupport.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
helper2 <- "abc"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
helper1 <- 123
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
helper2 <- "abc"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global <- "ABC"

tests/test-helpers/app6-disabled/server.R

Whitespace-only changes.

tests/test-helpers/app6-disabled/ui.R

Whitespace-only changes.

tests/testthat/test-app.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ test_that("loadSupport messages to inform about loading", {
2424
"Automatically loading 1 .R file")
2525
})
2626

27+
test_that("loadSupport skips if _disable_autoload.R found", {
28+
expect_message(loadSupport(test_path("../test-helpers/app6-disabled"), renv=environment(), globalrenv=NULL),
29+
"disable_autoload.R detected; not loading")
30+
expect_false(exists("helper1"))
31+
})
32+
2733
test_that("Can suppress sourcing global.R", {
2834
# Confirm that things blow up if we source global.R
2935
expect_error(loadSupport(test_path("../test-helpers/app3-badglobal")))

0 commit comments

Comments
 (0)