Skip to content

Error in source(global_r, local = FALSE) #298

Closed
rstudio/rmarkdown
#2012

Description

@liaojiahui-r

Suddenly encountered this ERROR!

Loading required package: shiny Error in source(global_r, local = FALSE) : attempt to use zero-length variable name Calls: <Anonymous> -> do.call -> <Anonymous> -> <Anonymous> -> source Execution halted

This appeared just after I created a new .Rmd file (with Rstudio notebook). I copied exactly the same old app‘s code to this new notebook, it failed to run (what's more the old .Rmd now failed too).

I did some search it seems very few people run into this problem, but its very annoying and I can‘t fix it.

Any help will be highly appreciate!

Screen Shot 2021-01-19 at 9 38 25 AM


R version 4.0.3 (2020-10-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.7

Activity

  1. cderv commented on Jan 19, 2021

    @cderv
    Collaborator

    Hi! Thanks for the report.

    I can't reproduce this error only with the information you gave. However, we get a hint from the error message.

    Do you have a file called global.R (or with a different case) ?
    If this file is found it will be source which seems to happen according to the error message you have. (Which contains source(global_r, local = FALSE))

    If you have that file as I believe, can you check the content ? This is where the error comes from it seems.

    If it is expected that this file is sourced and the content is correct, can you share your issue as a reproducible example ? Or at least with more detail about the content.

    Thank you.

  2. liaojiahui-r commented on Jan 19, 2021

    @liaojiahui-r
    Author

    I now realized my .Rmd file name "global XXX.Rmd" could be true problem!

    Maybe we should not use any thing contains "global" as file name?

    I'm not so good at R and flexdashboard is so user-friendly for people like me! Many thanks!

  3. cderv commented on Jan 19, 2021

    @cderv
    Collaborator

    I now realized my .Rmd file name "global XXX.Rmd" could be true problem!

    No it should be only with a global.R file. But what is odd after

    Let's take a step back in debugging.

    1. You did not share your session info. Are you running last versions of packages ? If not you can try to update to last CRAN version.
    2. Can you then run rmarkdown::run("<path to your Rmd file>") ? It should error as you shown. Then can you run traceback() (or better rlang::last_trace() if you have rlang installed) and post the result here ?
      Thank you!
  4. liaojiahui-r commented on Jan 20, 2021

    @liaojiahui-r
    Author

    Here I tried to reproducing the issue with minimum code.

    We need 2 .Rmd files and 1 .R file (for sourcing) to reproducing the issue.

    If there is ohter .Rmd file named [global r***.Rmd] (here I use “global reproducing flex template.Rmd” as an example) in the SAME Folder then here comes the issue!

    Information of

    traceback() AND
    rlang::last_trace() just after run rmarkdown::run("<my .Rmd>") AND
    R session

    is pasted on the bottom.

    ——————————————————————————————

    1st "NEW flex template.Rmd"


    title: "if there is ohter .Rmd file named [global r***.Rmd] in the same folder then comes the issue!"
    output:
    flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    runtime: shiny

    if there is a .Rmd file named [global r***.Rmd] in the same folder then comes the issue!

    library(shiny)
    library(flexdashboard)
    

    Column {data-width=300}

    Give me a number:

    sliderInput(
      "a_number",
      label = "Give me a number:",
      min = 5,
      max = 10,
      value = 5,
      step = 1
    )
    

    Column {data-width=300}

    Now reproducing the bug

    source("plot something.R")
    
    renderPlot({
      plot_sth(give_me_a_munber = input$a_number)
    })
    

    ——————————————————————————————

    2nd "global reproducing flex template.Rmd"


    title: "if .Rmd file name is [global r***.Rmd] then the issue comes!"
    output:
    flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    runtime: shiny

    library(shiny)
    library(flexdashboard)
    

    Column {data-width=650}

    Give me a number:

    sliderInput(
      "a_number",
      label = "Give me a number:",
      min = 5,
      max = 10,
      value = 5,
      step = 1
    )
    

    Column {data-width=350}

    Now reproducing the bug

    source("plot something.R")
    
    renderPlot({
      plot_sth(give_me_a_munber = input$a_number)
    })
    

    ——————————————————————————————

    3rd "plot something.R" file to source

    library(ggplot2)
    
    plot_sth <- function(give_me_a_munber) {
      ggplot(
        data.frame(x = 1:give_me_a_munber, y = rep(1, give_me_a_munber)),
        aes(x, y, size = x)
      ) +
        geom_point(show.legend = FALSE) +
        scale_size_continuous(range = c(1, 20)) +
        theme_void()
    }
    

    ———————————————————————————————

    if I Run rmarkdown::run("~/Downloads/NEW flex template.Rmd")

    rmarkdown::run("~/Downloads/NEW flex template.Rmd")
    Loading required package: shiny
    Error in source(global_r, local = FALSE) :
    attempt to use zero-length variable name

    rlang::last_trace()

    <error/rlang_error>
    Can't show last error because no error was recorded yet
    Backtrace:
    █

    1. └─rlang::last_trace()
    2. └─rlang::last_error()

    ———————————————————————————————

    traceback()

    5: source(global_r, local = FALSE)
    4: appParts$onStart()
    3: (function (appDir = getwd(), port = getOption("shiny.port"),
    launch.browser = getOption("shiny.launch.browser", interactive()),
    host = getOption("shiny.host", "127.0.0.1"), workerId = "",
    quiet = FALSE, display.mode = c("auto", "normal", "showcase"),
    test.mode = getOption("shiny.testmode", FALSE))
    {
    on.exit({
    handlerManager$clear()
    }, add = TRUE)
    if (.globals$running) {
    stop("Can't call runApp() from within runApp(). If your ",
    "application code contains runApp(), please remove it.")
    }
    .globals$running <- TRUE
    on.exit({
    .globals$running <- FALSE
    }, add = TRUE)
    oldOptionSet <- .globals$options
    on.exit({
    .globals$options <- oldOptionSet
    }, add = TRUE)
    shinyOptions(appToken = createUniqueId(8))
    ops <- options(warn = max(1, getOption("warn", default = 1)),
    pool.scheduler = scheduleTask)
    on.exit(options(ops), add = TRUE)
    if (is.null(getShinyOption("cache"))) {
    shinyOptions(cache = MemoryCache$new())
    }
    appParts <- as.shiny.appobj(appDir)
    appOps <- appParts$options
    findVal <- function(arg, default) {
    if (arg %in% names(appOps))
    appOps[[arg]]
    else default
    }
    if (missing(port))
    port <- findVal("port", port)
    if (missing(launch.browser))
    launch.browser <- findVal("launch.browser", launch.browser)
    if (missing(host))
    host <- findVal("host", host)
    if (missing(quiet))
    quiet <- findVal("quiet", quiet)
    if (missing(display.mode))
    display.mode <- findVal("display.mode", display.mode)
    if (missing(test.mode))
    test.mode <- findVal("test.mode", test.mode)
    if (is.null(host) || is.na(host))
    host <- "0.0.0.0"
    workerId(workerId)
    if (inShinyServer()) {
    ver <- Sys.getenv("SHINY_SERVER_VERSION")
    if (utils::compareVersion(ver, .shinyServerMinVersion) <
    0) {
    warning("Shiny Server v", .shinyServerMinVersion,
    " or later is required; please upgrade!")
    }
    }
    setShowcaseDefault(0)
    .globals$testMode <- test.mode
    if (test.mode) {
    message("Running application in test mode.")
    }
    if (is.character(appDir)) {
    desc <- file.path.ci(if (tolower(tools::file_ext(appDir)) ==
    "r")
    dirname(appDir)
    else appDir, "DESCRIPTION")
    if (file.exists(desc)) {
    con <- file(desc, encoding = checkEncoding(desc))
    on.exit(close(con), add = TRUE)
    settings <- read.dcf(con)
    if ("DisplayMode" %in% colnames(settings)) {
    mode <- settings[1, "DisplayMode"]
    if (mode == "Showcase") {
    setShowcaseDefault(1)
    if ("IncludeWWW" %in% colnames(settings)) {
    .globals$IncludeWWW <- as.logical(settings[1,
    "IncludeWWW"])
    if (is.na(.globals$IncludeWWW)) {
    stop("In your Description file, IncludeWWW ",
    "must be set to True (default) or False")
    }
    }
    else {
    .globals$IncludeWWW <- TRUE
    }
    }
    }
    }
    }
    if (is.null(.globals$IncludeWWW) || is.na(.globals$IncludeWWW)) {
    .globals$IncludeWWW <- TRUE
    }
    display.mode <- match.arg(display.mode)
    if (display.mode == "normal") {
    setShowcaseDefault(0)
    }
    else if (display.mode == "showcase") {
    setShowcaseDefault(1)
    }
    require(shiny)
    if (is.null(port)) {
    for (i in 1:20) {
    if (!is.null(.globals$lastPort)) {
    port <- .globals$lastPort
    .globals$lastPort <- NULL
    }
    else {
    while (TRUE) {
    port <- p_randomInt(3000, 8000)
    if (!port %in% c(3659, 4045, 6000, 6665:6669,
    6697)) {
    break
    }
    }
    }
    tmp <- try(startServer(host, port, list()), silent = TRUE)
    if (!inherits(tmp, "try-error")) {
    stopServer(tmp)
    .globals$lastPort <- port
    break
    }
    }
    }
    on.exit({
    .globals$onStopCallbacks$invoke()
    .globals$onStopCallbacks <- Callbacks$new()
    }, add = TRUE)
    unconsumeAppOptions(appParts$appOptions)
    if (!is.null(appParts$onStop))
    on.exit(appParts$onStop(), add = TRUE)
    if (!is.null(appParts$onStart))
    appParts$onStart()
    server <- startApp(appParts, port, host, quiet)
    shinyOptions(server = server)
    on.exit({
    stopServer(server)
    }, add = TRUE)
    if (!is.character(port)) {
    browseHost <- host
    if (identical(host, "0.0.0.0")) {
    browseHost <- "127.0.0.1"
    }
    else if (identical(host, "::")) {
    browseHost <- "::1"
    }
    if (httpuv::ipFamily(browseHost) == 6L) {
    browseHost <- paste0("[", browseHost, "]")
    }
    appUrl <- paste("http://", browseHost, ":", port, sep = "")
    if (is.function(launch.browser))
    launch.browser(appUrl)
    else if (launch.browser)
    utils::browseURL(appUrl)
    }
    else {
    appUrl <- NULL
    }
    callAppHook("onAppStart", appUrl)
    on.exit({
    callAppHook("onAppStop", appUrl)
    }, add = TRUE)
    .globals$reterror <- NULL
    .globals$retval <- NULL
    .globals$stopped <- FALSE
    ..stacktraceoff..(captureStackTraces({
    while (!.globals$stopped) {
    ..stacktracefloor..(serviceApp())
    }
    }))
    if (isTRUE(.globals$reterror)) {
    stop(.globals$retval)
    }
    else if (.globals$retval$visible)
    .globals$retval$value
    else invisible(.globals$retval$value)
    })(appDir = structure(list(httpHandler = function (req)
    {
    if (!identical(req$REQUEST_METHOD, "GET"))
    return(NULL)
    if (!isTRUE(grepl(uiPattern, req$PATH_INFO)))
    return(NULL)
    textConn <- file(open = "w+")
    on.exit(close(textConn))
    showcaseMode <- .globals$showcaseDefault
    if (.globals$showcaseOverride) {
    mode <- showcaseModeOfReq(req)
    if (!is.null(mode))
    showcaseMode <- mode
    }
    testMode <- .globals$testMode %OR% FALSE
    bookmarkStore <- getShinyOption("bookmarkStore", default = "disable")
    if (bookmarkStore == "disable") {
    restoreContext <- RestoreContext$new()
    }
    else {
    restoreContext <- RestoreContext$new(req$QUERY_STRING)
    }
    withRestoreContext(restoreContext, {
    uiValue <- NULL
    if (is.function(ui)) {
    if (length(formals(ui)) > 0) {
    uiValue <- ..stacktraceon..(ui(req))
    }
    else {
    uiValue <- ..stacktraceon..(ui())
    }
    }
    else {
    if (getCurrentRestoreContext()$active) {
    warning("Trying to restore saved app state, but UI code must be a function for this to work! See ?enableBookmarking")
    }
    uiValue <- ui
    }
    })
    if (is.null(uiValue))
    return(NULL)
    renderPage(uiValue, textConn, showcaseMode, testMode)
    html <- paste(readLines(textConn, encoding = "UTF-8"), collapse = "\n")
    return(httpResponse(200, content = enc2utf8(html)))
    }, serverFuncSource = function ()
    {
    server
    }, onStart = function ()
    {
    global_r <- file.path.ci(dir, "global.R")
    if (file.exists(global_r)) {
    source(global_r, local = FALSE)
    }
    shiny::addResourcePath("rmd_resources", pkg_file("rmd/h/rmarkdown"))
    }, options = list(), appOptions = list(appDir = "/Users/liaojiahui/Downloads",
    bookmarkStore = NULL)), class = "shiny.appobj"), launch.browser = function (url)
    {
    url <- paste(url, file_rel, sep = "/")
    browser <- getOption("shiny.launch.browser")
    if (is.function(browser)) {
    browser(url)
    }
    else {
    utils::browseURL(url)
    }
    })
    2: do.call(shiny::runApp, shiny_args)
    1: rmarkdown::run("~/Downloads/NEW flex template.Rmd")

    ———————————————————————————————

    Screen Shot 2021-01-20 at 12 59 46 PM

    Screen Shot 2021-01-20 at 1 00 29 PM

    ———————————————————————————————

    R session

    R version 4.0.3 (2020-10-10)
    Platform: x86_64-apple-darwin17.0 (64-bit)
    Running under: macOS Catalina 10.15.7

    Matrix products: default
    BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
    LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

    Random number generation:
    RNG: Mersenne-Twister
    Normal: Inversion
    Sample: Rounding

    locale:
    [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

    attached base packages:
    [1] stats graphics grDevices utils
    [5] datasets methods base

    other attached packages:
    [1] shiny_1.5.0

    loaded via a namespace (and not attached):
    [1] Rcpp_1.0.6 digest_0.6.27
    [3] later_1.1.0.1 mime_0.9
    [5] R6_2.5.0 xtable_1.8-4
    [7] jsonlite_1.7.2 magrittr_2.0.1
    [9] evaluate_0.14 rlang_0.4.10
    [11] promises_1.1.1 rmarkdown_2.6
    [13] flexdashboard_0.5.2 tools_4.0.3
    [15] tinytex_0.28 fastmap_1.0.1
    [17] httpuv_1.5.5 xfun_0.20
    [19] yaml_2.2.1 compiler_4.0.3
    [21] htmltools_0.5.1 knitr_1.30

  5. cderv commented on Jan 20, 2021

    @cderv
    Collaborator

    Thanks a lot for this ! It explains the error !

    There is indeed a bug because global.R is seen a a regex pattern when no global.R filename found. And that is why it matches your Rmd file.

    This is a very specific bug because you have a file with name containing global.r, with . being any character (in your case a space)

    Thanks for this investigation. Glad we found this !

    I'll submit a fix

  6. added 2 commits that reference this issue on Jan 20, 2021
    78ddee2
    95eed06
  7. cderv commented on Jan 20, 2021

    @cderv
    Collaborator

    This should be fixed now in the dev version of rmarkdown.

    You can install with

    remotes::install_github("rstudio/rmarkdown")
  8. cderv commented on Jan 20, 2021

    @cderv
    Collaborator

    @rich-iannone I think you maintain this project now. You can close this issue (I can't so the closes PR did not close this issue)

  9. added a commit that references this issue on May 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions