Rstudio: Build Source Package not picking up user libPath

Background

I'm developing a local package bitstomach which depends on another local package 'spekex'. Both are installed locally at ~/R/x86_64-pc-linux-gnu-library/dev/. The path that directory is added to .libPath in my ~/.Rprofile

  • Ubuntu 18.04
  • R version 3.6.1
  • RStudio 1.2.1335

Problem

When I use the IDE's Build Source Package, I receive an error that the other local package, spekex, is not available.

Updating bitstomach documentation
Loading bitstomach
Error: Dependency package(s) 'spekex' not available.
Backtrace:
     █
  1. ├─base::suppressPackageStartupMessages(...)
  2. │ └─base::withCallingHandlers(expr, packageStartupMessage = function(c) invokeRestart("muffleMessage"))
  3. └─devtools::document(roclets = c("rd", "collate", "namespace"))
  4.   ├─withr::with_envvar(...)
  5.   │ └─base::force(code)
  6.   └─roxygen2::roxygenise(pkg$path, roclets)
  7.     └─roxygen2:::load_code(base_path)
  8.       └─pkgload::load_all(path, compile = FALSE, helpers = FALSE, attach_testthat = FALSE)
  9.         └─pkgload:::load_imports(path)
 10.           └─pkgload:::abort_for_missing_packages(res, imports$package)

Investigation

It appears that the issue arises from using roxygen. Turning off the "Generate documentation with Roxygen" avoids the error.

It appears that .Rprofile is not being read so libPaths isn't being updated. I checked this by creating a symlink /usr/lib/R/library/spekex to ~/R/x86_64-pc-linux-gnu-library/dev/spekex. Doing so avoids the error.

Running the command devtools::document(roclets = c('rd', 'collate', 'namespace')) from the IDE works. Presumably because in that context the libPaths has been updated.

My issue sounds similar to this topic

Question

Does this seem like it is a case of Roxygen not reading my .Rprofile, or there something else that might be going on?

What can I try to ensure that .Rprofile is used by Roxygen when using the IDE's Build Source Package functionality?

We run devtools::document() and friends within a child R process spawned with --vanilla, so library paths customized in a .Rprofile are not seen.

However, the active library paths for the session (as reported by .libPaths()) should still be delegated to the child R process through the R_LIBS environment variable, so I'm not sure why this isn't happening in your case.

1 Like

Ran into this again

Decided to clear out the usual places where packages get installed.

  • "/usr/local/lib/R/site-library"
  • "/usr/lib/R/site-library"
  • "/usr/lib/R/library"

That led to a more easily tractable problem of an error about devtools not being installed.

==> devtools::document(roclets = c('rd', 'collate', 'namespace'))

Error in loadNamespace(name) : there is no package called ‘devtools’
Calls: suppressPackageStartupMessages ... loadNamespace -> withRestarts -> withOneRestart -> doWithOneRestart
Execution halted

Exited with status 1.

I know devtools is installed, and I know it's installed in ~/R/x86_64-pc-linux-gnu-library/dev/. From the error, we know that location doesn't end up in .libPaths() when Build>Document runs from Rstudio.

The solution.

Setting env in Rstudio.desktop file

Modifying the .desktop file that launches the Rstuio application on Ubuntu /usr/share/applications/rstudio.desktop per "Launching desktop application with an environment variable" section in https://help.ubuntu.com/community/EnvironmentVariables

Change from

Exec=/usr/lib/rstudio/bin/rstudio %F

to

Exec=env R_LIBS_USER=~/R/x86_64-pc-linux-gnu-library/dev/ /usr/lib/rstudio/bin/rstudio %F

The adds the R_LIBS_USER environment variable to Rstudio's environment. That value then gets read by /usr/lib/R/library/base/R/Rprofile when a new R process starts. That Rprofile adds the value to the libPaths. The result is that it will apply to for Rscript --vanilla which is essentially how the Documentation task gets run.

The environment var has to be added to the invocation of Rstudio, because the Rstudio GUI is the parent process of the call to run Rscript and do the Document task. So setting the env var in someplace like bashrc or bash_profile won't affect the Rstudio environment.

Things that don't work

Setting libs in ~/.Rprofile

.libPaths( "~/R/x86_64-pc-linux-gnu-library/dev/" )

Running Build > Document does not pick up the .libPaths here.
No suprise, Document is probably running with Rscript --vanilla

Setting ENV var R_LIBS_USER in ~/.bashrc

export R_LIBS_USER=/path/to/R/library

This allows /usr/lib/R/library/base/R/Rprofile to pick up the R library location so it applies when running Rscript --vanilla. This can be seen with:

Rscript --vanilla -e '.libPaths()'

However, this doesn't get picked up by when running Build>Document because Rstudio is not spawning a new shell, it's just starting a new process.

Would you mind filing this as a bug report at https://github.com/rstudio/rstudio/issues?

I can do that. I think this is probably a feature request to specify the additional lib paths to use for build tools.

What is the Windows (Win 10, specifically) equivalent of this? I cannot locate a file that resembles rstudio.desktop in the install directory (C:/Program Files/RStudio) or in C:/Users/my_name/AppData/Local/RStudio.

Another fix would be welcome, of course.

Thanks in advance.