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.