Source of multiple files in Rprofile

I am having a problem looking for two files in my .Rprofile. It seems that my second call to source () occurs before my first call to source has completed. Below is my .Rprofile and a description of the behavior I see.

.First <- function() {
  #### -- Packrat Autoloader (version 0.4.0.8) -- ####
  source("packrat/init.R")
  #### -- End Packrat Autoloader -- ####

  #### -- Auto load the CellSeg project functions and custom execution strategy -- ####
  source("R/source.R")
  #### -- End Autoloader -- ####
}

      

The above code is supposed to start the Packrat autoloader and then specify a few local functions that I am using for my project. Here's the behavior I find:

  • If I just post one of the files (packrat / init.R or R / source.R) the original file works as expected.
  • If I use both types of packrat autoloader, the source ("packrat / init.R") stops halfway through its execution. Then the second source, source ("R / source.R"), successfully completes and exits.

The source statement ("R / source.R") seems to run in the middle of the Packrat autoloader code. If I could verify that the origin call ("packrat / init.R") was modal, I think my errors would go away.

How can I fix this problem? Is there a way to ensure that the source ("packrat / init.R") method is modal?

I have added the source and packrat files that I am using. This appears to be a modal issue because I get the following output from the packrat bootstrap process when I upload both files. It stops partially by loading its dependencies (packrat / init.R boot file) and fails. When running the project in RStudio, this is the response I get:

Packrat is not installed in the local library -- attempting to bootstrap an installation...  
> No source tarball of packrat available locally  
> Using user-library packrat (0.4.0.12) to bootstrap the project  
Installing colorspace (1.2-4) ... OK (downloaded binary)  
Installing dichromat (2.0-0) ... OK (downloaded binary)  
Installing digest (0.6.4) ... OK (downloaded binary)  
Installing gtable (0.1.2) ... OK (downloaded binary)  
Installing jsonlite (0.9.10) ...  
>  

      

If I just send the packrat / init.R package to .Rprofile, the batch download is complete. Below is the output when starting the project when .Rprofile only returns the packrat / init.R file:

Packrat is not installed in the local library -- attempting to bootstrap an installation...  
> No source tarball of packrat available locally  
> Using user-library packrat (0.4.0.12) to bootstrap the project  
Installing colorspace (1.2-4) ... OK (downloaded binary)
Installing dichromat (2.0-0) ... OK (downloaded binary)
Installing digest (0.6.4) ... OK (downloaded binary)
Installing gtable (0.1.2) ... OK (downloaded binary)
Installing jsonlite (0.9.10) ... OK (downloaded binary)
Installing labeling (0.2) ... OK (built source)
Installing packrat (0.4.0.12) ... OK (built source)
Installing png (0.1-7) ... OK (downloaded binary)
Installing proto (0.3-10) ... OK (downloaded binary)
Installing RColorBrewer (1.0-5) ... OK (downloaded binary)
Installing Rcpp (0.11.2) ... OK (downloaded binary)
Installing snow (0.3-13) ... OK (downloaded binary)
Installing stringr (0.6.2) ... OK (downloaded binary)
Installing tiff (0.1-5) ... OK (downloaded binary)
Installing munsell (0.4.2) ... OK (downloaded binary)
Installing testthat (0.8.1) ... OK (downloaded binary)
Installing plyr (1.8.1) ... OK (downloaded binary)
Installing snowfall (1.84-6) ... OK (downloaded binary)
Installing reshape (0.8.5) ... OK (downloaded binary)
Installing reshape2 (1.4) ... OK (downloaded binary)
Installing scales (0.2.4) ... OK (downloaded binary)
Installing ggplot2 (1.0.0) ... OK (downloaded binary)
Installing GGally (0.4.7) ... OK (downloaded binary)
Packrat mode on. Using library in directory:
- "D:/cellSeg/packrat/lib"
> 

      

Here's the source files I used

packrat / init.r is the default automatic bootloader. It goes through the packrat.lock file, checks the dependencies, and downloads the missing dependencies through CRAN, GitHub and local repos:

local({

  libDir <- file.path('packrat', 'lib', R.version$platform, getRversion())

  if (is.na(Sys.getenv("RSTUDIO_PACKRAT_BOOTSTRAP", unset = NA)) &&
        suppressWarnings(requireNamespace("packrat", quietly = TRUE, lib.loc = libDir))) {

    # Check 'print.banner.on.startup' -- when NA and RStudio, don't print
    print.banner <- packrat::get_opts("print.banner.on.startup")
    if (print.banner == "auto" && is.na(Sys.getenv("RSTUDIO", unset = NA))) {
      print.banner <- TRUE
    } else {
      print.banner <- FALSE
    }
    return(packrat::on(print.banner = print.banner))
  }

  ## Bootstrapping -- only performed in interactive contexts
  if (interactive()) {

    ## Escape hatch to allow RStudio to handle initialization
    if (!is.na(Sys.getenv("RSTUDIO", unset = NA)) &&
          is.na(Sys.getenv("RSTUDIO_PACKRAT_BOOTSTRAP", unset = NA))) {
      Sys.setenv("RSTUDIO_PACKRAT_BOOTSTRAP" = "1")
      setHook("rstudio.sessionInit", function(...) {
        source("packrat/init.R")
      })
      return(invisible(NULL))
    }

    message("Packrat is not installed in the local library -- ",
            "attempting to bootstrap an installation...")

    ## We need utils for the following to succeed -- there are calls to functions
    ## in 'restore' that are contained within utils. utils gets loaded at the
    ## end of start-up anyhow, so this should be fine
    library("utils", character.only = TRUE)

    ## Install packrat into local project library
    packratSrcPath <- list.files(full.names = TRUE,
                                 file.path("packrat", "src", "packrat")
    )

    ## No packrat tarballs available locally -- try some other means of installation
    if (!length(packratSrcPath)) {

      message("> No source tarball of packrat available locally")

      ## There are no packrat sources available -- try using a version of
      ## packrat installed in the user library to bootstrap
      if (requireNamespace("packrat", quietly = TRUE) && packageVersion("packrat") >= "0.2.0.99") {
        message("> Using user-library packrat (",
                packageVersion("packrat"),
                ") to bootstrap this project")
      }

      ## Couldn't find a user-local packrat -- try finding and using devtools
      ## to install
      else if (requireNamespace("devtools", quietly = TRUE)) {
        message("> Attempting to use devtools::install_github to install ",
                "a temporary version of packrat")
        library(stats) ## for setNames
        devtools::install_github("rstudio/packrat")
      }

      ## Try downloading packrat from CRAN if available
      else if ("packrat" %in% rownames(available.packages())) {
        message("> Installing packrat from CRAN")
        install.packages("packrat")
      }

      ## Fail -- couldn't find an appropriate means of installing packrat
      else {
        stop("Could not automatically bootstrap packrat -- try running ",
             "\"'install.packages('devtools');     devtools::install_github('rstudio/packrat')\"",
             "and restarting R to bootstrap packrat.")
      }

      # Restore the project, unload the temporary packrat, and load the private packrat
      packrat::restore(prompt = FALSE, restart = TRUE)

      ## This code path only reached if we didn't restart earlier
      unloadNamespace("packrat")
      requireNamespace("packrat", lib.loc = libDir, quietly = TRUE)
      return(packrat::on())

    }

    ## Multiple packrat tarballs available locally -- try to choose one
    ## TODO: read lock file and infer most appropriate from there; low priority because
    ## after bootstrapping packrat a restore should do the right thing
    if (length(packratSrcPath) > 1) {
      warning("Multiple versions of packrat available in the source directory;",
              "using packrat source:\n- ", shQuote(packratSrcPath))
      packratSrcPath <- packratSrcPath[[1]]
    }


    lib <- file.path("packrat", "lib", R.version$platform, getRversion())
    if (!file.exists(lib)) {
      dir.create(lib, recursive = TRUE)
    }
    lib <- normalizePath(lib, winslash = "/")

    message("> Installing packrat into project private library:")
    message("- ", shQuote(lib))

    surround <- function(x, with) {
      if (!length(x)) return(character())
      paste0(with, x, with)
    }

    ## The following is performed because a regular install.packages call can fail
    peq <- function(x, y) paste(x, y, sep = " = ")
    installArgs <- c(
      peq("pkgs", surround(packratSrcPath, with = "'")),
      peq("lib", surround(lib, with = "'")),
      peq("repos", "NULL"),
      peq("type", surround("source", with = "'"))
    )
    installCmd <- paste(sep = "",
                        "utils::install.packages(",
                        paste(installArgs, collapse = ", "),
                        ")")

    fullCmd <- paste(
      surround(file.path(R.home("bin"), "R"), with = "\""),
      "--vanilla",
      "--slave",
      "-e",
      surround(installCmd, with = "\"")
    )
    system(fullCmd)

    ## Tag the installed packrat so we know it managed by packrat
    ## TODO: should this be taking information from the lockfile? this is a bit awkward
    ## because we're taking an un-annotated packrat source tarball and simply assuming it now
    ## an 'installed from source' version

    ## -- InstallAgent -- ##
    installAgent <- 'InstallAgent: packrat 0.4.0.8'

    ## -- InstallSource -- ##
    installSource <- 'InstallSource: source'

    packratDescPath <- file.path(lib, "packrat", "DESCRIPTION")
    DESCRIPTION <- readLines(packratDescPath)
    DESCRIPTION <- c(DESCRIPTION, installAgent, installSource)
    cat(DESCRIPTION, file = packratDescPath, sep = "\n")

    # Otherwise, continue on as normal
    message("> Attaching packrat")
    library("packrat", character.only = TRUE, lib.loc = lib)

    message("> Restoring library")
    restore(restart = FALSE)

    # If the environment allows us to restart, do so with a call to restore
    restart <- getOption("restart")
    if (!is.null(restart)) {
      message("> Packrat bootstrap successfully completed. ",
              "Restarting R and entering packrat mode...")
      return(restart())
    }

    # Callers (source-erers) can define this hidden variable to make sure we don't enter packrat mode
    # Primarily useful for testing
    if (!exists(".__DONT_ENTER_PACKRAT_MODE__.")) {
      message("> Packrat bootstrap successfully completed. Entering packrat mode...")
      packrat::on()
    }

    Sys.unsetenv("RSTUDIO_PACKRAT_BOOTSTRAP")

  }

})

      

The packrat.lock used is as follows:

PackratFormat: 1.4
PackratVersion: 0.4.0.12
RVersion: 3.0.3
Repos: CRAN=http://cran.rstudio.com,
    CRANextra=http://www.stats.ox.ac.uk/pub/RWin

Package: colorspace
Source: CRAN
Version: 1.2-4
Hash: b775bd08a10f968fef582304b7c3ab9c

Package: dichromat
Source: CRAN
Version: 2.0-0
Hash: 42be2136555fa4b6167605df56d8cd7c

Package: digest
Source: CRAN
Version: 0.6.4
Hash: e4bf0e79b15a352510aff0de905d31e2

Package: GGally
Source: CRAN
Version: 0.4.7
Hash: 0c64ebad884d013e202eb3458f9122b7
Requires: ggplot2, gtable, plyr, reshape, stringr

Package: ggplot2
Source: CRAN
Version: 1.0.0
Hash: c8bff66238347472f08b6a35608539ff
Requires: digest, gtable, plyr, proto, reshape2, scales

Package: gtable
Source: CRAN
Version: 0.1.2
Hash: 0cf4a2b54c02b35dec7e74a8ebed12a0

Package: jsonlite
Source: CRAN
Version: 0.9.10
Hash: 6e69e79691b668042c7f022bb680bba5

Package: labeling
Source: CRAN
Version: 0.2
Hash: 25f63495dfb737e67bd75fc1eb58ff2c

Package: munsell
Source: CRAN
Version: 0.4.2
Hash: fa8af6d040041439b249351e9d16ae9a
Requires: colorspace

Package: packrat
Source: github
Version: 0.4.0.12
Hash: e738f79a22521c57fad67a1679302385973cd958
GithubRepo: packrat
GithubUsername: rstudio
GithubRef: master
GithubSha1: e738f79a22521c57fad67a1679302385973cd958

Package: plyr
Source: CRAN
Version: 1.8.1
Hash: be21bad411e628f810a92212e17b5be7
Requires: Rcpp

Package: png
Source: CRAN
Version: 0.1-7
Hash: 7559dcd0a9f188351986d0494ec1516d

Package: proto
Source: CRAN
Version: 0.3-10
Hash: 668a9e87cedbdc5f56c396edf1bd2648

Package: RColorBrewer
Source: CRAN
Version: 1.0-5
Hash: 6a849b729bec970204ca88b96906adbc

Package: Rcpp
Source: CRAN
Version: 0.11.2
Hash: ef2b1a5e45d1461bd720e4ebbe48e601

Package: reshape
Source: CRAN
Version: 0.8.5
Hash: 3a8bcdf002df52627d35dfab189711f4
Requires: plyr

Package: reshape2
Source: CRAN
Version: 1.4
Hash: b210925e978fd52ad5eb0822a353e7ce
Requires: plyr, Rcpp, stringr

Package: scales
Source: CRAN
Version: 0.2.4
Hash: b8b1af97bd07818605223060386b00dd
Requires: dichromat, labeling, munsell, plyr, RColorBrewer

Package: snow
Source: CRAN
Version: 0.3-13
Hash: 16dca684ceb4baa0f96c1ca1b3314497

Package: snowfall
Source: CRAN
Version: 1.84-6
Hash: 0af464c3f656c307938e01793caff847
Requires: snow

Package: stringr
Source: CRAN
Version: 0.6.2
Hash: cdde0cfa0f52cffeaca23a11a3899779

Package: testthat
Source: CRAN
Version: 0.8.1
Hash: f296283e62e2ff299efa01e26cfc3eb1
Requires: digest

Package: tiff
Source: CRAN
Version: 0.1-5
Hash: bff6221f2ff93a5e6ddaf4314f97251a

      

Finally, here is a mock R / source.R file that I am using. Basically, it just loads the list of functions you want to use when you start your project.

# Project functions to be preloaded 
source('./R/generate/myFunction_1.R')
source('./R/generate/myFunction_2.R')
source('./R/generate/myFunction_3.R')
source('./R/generate/myFunction_4.R')

      

+3


source to share





All Articles