在Rprofile中获取多个文件

时间:2014-08-25 21:13:51

标签: r customization rprofile

我在.Rprofile中获取两个文件时遇到了问题。似乎我的第二个source()调用是在我的第一个源调用完成之前发生的。下面是我的.Rprofile和我看到的行为的描述。

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

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

上面的代码应该运行Packrat的自动加载器,然后提供一些我用于项目的本地函数。这是我找到的行为:

  1. 如果我只是提供其中一个文件(packrat / init.R或R / source.R),则源文件将按预期工作。
  2. 如果我同时发送了packrat自动加载器, source(“packrat / init.R”),则会在执行过程中停止一半。然后第二个源 source(“R / source.R”)执行并成功完成。
  3. 似乎源(“R / source.R”)语句在Packrat的自动加载器代码中间执行。如果我能确保源(“packrat / init.R”)调用是模态的,我认为我的错误会消失。

    如何解决此问题?有没有办法确保源(“packrat / init.R”)调用模式?

    我添加了我使用的源文件和packrat文件。这似乎是一个模态问题,因为当我获取两个文件时,我从packrat引导程序进程得到以下输出。它通过加载其依赖项(packrat / init.R引导程序)而停止,并以静默方式失败。在RStudio的项目启动时,这是我收到的回复:

    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) ...  
    >  
    

    如果我只是在.Rprofile中输入packrat / init.R,则packrat引导程序将成功运行完成。以下是.Rprofile仅获取packrat / init.R文件时项目启动时的输出:

    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"
    > 
    

    以下是我使用的源文件

    packrat / init.r是packrat的默认自动加载程序。它通过一个packrat.lock文件,检查依赖项并通过CRAN,GitHub和本地存储库加载缺少的依赖项:

    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's 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's 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")
    
      }
    
    })
    

    我使用的packrat.lock如下:

    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
    

    最后,这是我正在使用的R / source.R文件的模型。基本上,它只是加载要在项目启动时使用的函数列表。

    # 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')
    

0 个答案:

没有答案