shiny icon indicating copy to clipboard operation
shiny copied to clipboard

loadSupport warns for sourcing R files in a package even if _disable_autoload.r is present

Open freedin opened this issue 4 years ago • 4 comments

It is possible to opt out from the loadSupport default behaviour, e.g. when inside a package, by including a file _disable_autoload.r. However, even after including this file, the warning "Loading R/ subdirectory for Shiny application, but this directory appears to contain an R package. Sourcing files in R/ may cause unexpected behavior." appears. I suggest removing this warning. It causes issues e.g. when one wants to upgrade warnings to errors for debugging purposes. One solution could be to issue the warning further down in the function, like so:

loadSupport <- function (appDir = NULL, renv = new.env(parent = globalenv()), 
    globalrenv = globalenv()) 
{
    require(shiny)
    if (is.null(appDir)) {
        appDir <- findEnclosingApp(".")
    }
    descFile <- file.path.ci(appDir, "DESCRIPTION")
    if (!is.null(globalrenv)) {
        globalPath <- file.path.ci(appDir, "global.R")
        if (file.exists(globalPath)) {
            withr::with_dir(appDir, {
                sourceUTF8(basename(globalPath), envir = globalrenv)
            })
        }
    }
    helpersDir <- file.path(appDir, "R")
    disabled <- list.files(helpersDir, pattern = "^_disable_autoload\\.r$", 
        recursive = FALSE, ignore.case = TRUE)
    if (length(disabled) > 0) {
        return(invisible(renv))
    }
    if (file.exists(file.path.ci(appDir, "NAMESPACE")) || (file.exists(descFile) && 
        identical(as.character(read.dcf(descFile, fields = "Type")), 
            "Package"))) {
        warning("Loading R/ subdirectory for Shiny application, but this directory appears ", 
            "to contain an R package. Sourcing files in R/ may cause unexpected behavior.")
    }
    helpers <- list.files(helpersDir, pattern = "\\.[rR]$", recursive = FALSE, 
        full.names = TRUE)
    helpers <- sort_c(helpers)
    helpers <- normalizePath(helpers)
    withr::with_dir(appDir, {
        lapply(helpers, sourceUTF8, envir = renv)
    })
    invisible(renv)
}

freedin avatar Apr 07 '21 07:04 freedin

Hey,

I just came to the repo to report the same issue.

Would be nice to have no warning here — even more as it is not shown with the options(shiny.autoload.r) options.

ColinFay avatar Apr 17 '21 22:04 ColinFay

Just wanted to confirm this is still an issue, and link PR #3513 that was meant to fix it.

riccardoporreca avatar Jan 10 '24 11:01 riccardoporreca

Hi @cpsievert, Tagging you to get some eyes on this as it causes difficult to debug issues when deploying to shinyapps.io when using Shiny app package based frameworks like golem. It's been nearly three years and seems like a relatively simple fix.

Can shiny::loadSupport either: a. Function the way it says in the documentation e.g. be disabled when option shiny.autoload.r = FALSE or disable_autoload.R is present in the R directory -or- b. Mirror pkgload::load_all when a DESCRIPTION is present at the top-level e.g. heeding the order specified in the Collate directive?

We've had to write Readmes about the need to prefix any new files to enforce alphabetical load order because of this behavior in all of our shinyapps.io deployed apps.

yogat3ch avatar May 21 '24 21:05 yogat3ch

I think this was fixed in the latest release? https://github.com/rstudio/shiny/blame/main/NEWS.md#L44

cpsievert avatar May 21 '24 21:05 cpsievert