Using packrat with Rscript

840 views
Skip to first unread message

Gajus Miknaitis

unread,
Jul 11, 2017, 9:34:37 PM7/11/17
to packrat-discuss
Hi. I'm working in a specific environment where my scripts will be executed using Rscript, from a vanilla R installation, e.g:

Rscript my_script.R

I want to use packrat to bundle up the packages I need, but I won't be able to run packrat to unbundle them. Is there a way to bootstrap from the packrat bundle to use it to load itself? 

I've tried just untarring the bundle and then sourcing init.R from within my script, but that fails with an "Error in ensurePackageSymlink(source, target)" message. 

Kevin Ushey

unread,
Jul 12, 2017, 12:04:26 PM7/12/17
to Gajus Miknaitis, packrat-discuss
Can you provide the full error message? All that 'packrat::unbundle()' really does is untar the generated archive, and then (optionally) restore the Packrat library (so that packages previously used are available).

If you indeed won't have Packrat available at the time when you extract the bundle, then you'll have to make sure the package library is included in the bundle as well -- this is accomplished by calling e.g.

packrat::bundle(..., include.lib = TRUE)

In theory, you should also be able to just manually copy + paste the project to a new location (assuming the same kind of computer + OS + architecture) and it will still work.

Note that if you've copied the project's '.Rprofile', then any R session launched within that folder will automatically initialize + enter Packrat mode for that project.

Kevin

--
You received this message because you are subscribed to the Google Groups "packrat-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to packrat-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gajus Miknaitis

unread,
Jul 12, 2017, 12:15:23 PM7/12/17
to packrat-discuss, gajus.m...@gmail.com
The full error message is:

Error in ensurePackageSymlink(source, target) :
  Target '/home/gmiknaits/test/R_unbundle/packrat/lib-R/base' already exists and is not a symlink
Calls: source ... afterPackratModeOn -> symlinkSystemPackages -> ensurePackageSymlink
Execution halted

I did use the include.lib option when bundling (and for what it's worth I installed packrat from git using devtools) and can confirm that the libraries are present after untarring. After a little digging, I found that the error message is being thrown when R gets to this section in init.R:

if (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))
  }

If I comment that out and source init.R manually, then things seem to work all the way up until the packrat::on() call at the end:

> source('packrat/init.R')
Packrat is not installed in the local library -- attempting to bootstrap an installation...
> Installing packrat into project private library:
- '/home/gmiknaitis/test/R_unbundle/packrat/lib/x86_64-redhat-linux-gnu/3.2.0'
* installing *source* package ‘packrat’ ...
** R
** inst
** preparing package for lazy loading
** help
*** installing help indices
  converting help for package ‘packrat’
    finding HTML links ... done
    appDependencies                         html
    bundle                                  html
    clean                                   html
    disable                                 html
    init                                    html
    install                                 html
    install_local                           html
    migrate                                 html
    packify                                 html
    packrat-external                        html
    packrat-mode                            html
    packrat-options                         html
    packrat-resources                       html
    packrat                                 html
    repos_create                            html
    repos_upload                            html
    repository-management                   html
    restore                                 html
    search_path                             html
    snapshot                                html
    snapshotImpl                            html
    status                                  html
    unbundle                                html
    unused_packages                         html
** building package indices
** testing if installed package can be loaded
* DONE (packrat)
> Attaching packrat
> Restoring library
> Packrat bootstrap successfully completed. Entering packrat mode...
Error in ensurePackageSymlink(source, target) :
  Target '/home/gmiknaitis/test/R_unbundle/packrat/lib-R/base' already exists and is not a symlink
Kevin

To unsubscribe from this group and stop receiving emails from it, send an email to packrat-discu...@googlegroups.com.

Kevin Ushey

unread,
Jul 12, 2017, 12:18:12 PM7/12/17
to Gajus Miknaitis, packrat-discuss
One possibility as to why this is occurring -- the symlinks in 'lib-R' are being turned into either plain empty files or directories after unbundling, and Packrat is failing to remove the pre-existing files (out of an over-abundance of caution).

I think this should be fixed on the Packrat side -- since Packrat 'owns' the files within that directory, it shouldn't feel bad about overwriting them.

Thanks,
Kevin

To unsubscribe from this group and stop receiving emails from it, send an email to packrat-discuss+unsubscribe@googlegroups.com.

Kevin Ushey

unread,
Jul 12, 2017, 12:24:07 PM7/12/17
to Gajus Miknaitis, packrat-discuss
Looking at the Packrat package sources, it looks like we should actually be handling this case correctly already -- can you try a couple things?

If you haven't already, can you try installing the development version of Packrat, and using that?

devtools::install_github("rstudio/packrat")

Can you also let me know what the contents of the 'lib-R' directory is? Try something like:

tree -F packrat/lib-R

from the command line, executed from the unbundled project directory?

Thanks,
Kevin

Gajus Miknaitis

unread,
Jul 12, 2017, 12:27:58 PM7/12/17
to packrat-discuss, gajus.m...@gmail.com
I have the dev version installed. I don't have tree on the machine I'm working on, but I can confirm that lib-R contains real, not empty files, which are identical to the original system files. 

One thing I've noticed is that lib-R contains symlinks when created (using packrat::init), but the bundling process (using packrat::bundle(include.lib=TRUE)) ends up packing up the actual files that the symlinks point to. 

Kevin Ushey

unread,
Jul 12, 2017, 12:32:04 PM7/12/17
to Gajus Miknaitis, packrat-discuss
That would explain the behavior you're seeing, then -- Packrat should be ignoring those symlinks, not copying in their contents.

I've filed this on the GitHub repository as https://github.com/rstudio/packrat/issues/388. In the interim, you can safely remove the contents of that folder -- Packrat should automatically refresh it next time you initialize the project.

To unsubscribe from this group and stop receiving emails from it, send an email to packrat-discuss+unsubscribe@googlegroups.com.

Gajus Miknaitis

unread,
Jul 12, 2017, 12:41:58 PM7/12/17
to packrat-discuss, gajus.m...@gmail.com
Ok, confirmed that things work after removing the lib-R directory and contents after unbundling. I assume I could also just remove the symlinks in lib-R prior to bundling? 

Gajus Miknaitis

unread,
Jul 12, 2017, 12:53:20 PM7/12/17
to packrat-discuss, gajus.m...@gmail.com
In case it is helpful to others, I can confirm that the following works:

packrat::init()
install.packages('my_packages_go_here' )
packrat::snapshot()

then remove all symlinks in packrat/lib-R and do

packrat::bundle(include.lib=TRUE)

The bundle produced this way works as expected. It can be untarred using standard linux tools and starting up vanilla R in the directory with the .Rprofile from packrat (or source 'packrat/init.R' manually, which I need to do for other reasons) results in a correctly functioning R environment.

Thanks for your help, Kevin!
Reply all
Reply to author
Forward
0 new messages