1

I am working on Mac OSX, using v3.6.3 of R and using renv. In Rstudio and R, I can load the libraries of my installed packages, e.g library(ggplot2) works. However when I run a script using Rscript I get the message

Error in library(ggplot2) : there is no package called ‘ggplot2’

According to this SO answer, I need to make sure that the value of

Sys.getenv('R_LIBS_USER') in R.exe

is the same as the value of

Rscript.exe -e ".libPaths()"

But the value is the same, both are pointing to the renv-system-library in my project folder.

So how do I fix this?

Obromios
  • 15,408
  • 15
  • 72
  • 127
  • It is possible that the `.libPaths()` order could be the issue. Can you print the first path from the Rscript – akrun Apr 13 '20 at 21:04

2 Answers2

2

It may be better to specify the lib.loc in library call

library(ggplot2, lib.loc = '/path/where/library/is/located')
akrun
  • 874,273
  • 37
  • 540
  • 662
  • I tried that, but received the error message ```no library trees found in 'lib.loc'``` – Obromios Apr 13 '20 at 23:30
  • @Obromios that is strange. Can you check the `.libPaths()`, it could be more than one. Usually, the local one is first checked ffor the package – akrun Apr 13 '20 at 23:31
  • That prints out ``` "/Users/Chris/Sites/app_name/renv/library/R-3.6/x86_64-apple-darwin18.7.0" [2] "/private/var/folders/5_/p_yl0439059b7_jdqzrm0hr40000gr/T/RtmptdHcWN/renv-system-library"```, which is the same as what I am feeding into lib.loc – Obromios Apr 13 '20 at 23:46
  • ok, can you check in which one of them the ggplot2 is insstalled. It is possible that when you call from the script without the loc, it is checking a diffferent location – akrun Apr 13 '20 at 23:49
  • @Obromios it could be also due to permission. – akrun Apr 13 '20 at 23:56
  • We are getting somewhere, but not there yet. The ggplot2 is under ```/Users/Chris/Sites/covid_tracker/renv/library/R-3.6/x86_64-apple-darwin18.7.0```, but when I load ggplot2 library with lib.loc equal to that, I get ``` Error: package or namespace load failed for ‘ggplot2’: .onLoad failed in loadNamespace() for 'pillar', details: call: utils::packageVersion("vctrs") error: there is no package called ‘vctrs’``` – Obromios Apr 14 '20 at 00:04
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/211587/discussion-between-obromios-and-akrun). – Obromios Apr 14 '20 at 00:07
0

I managed to solve this. akruns answer was useful, it did not work, but pointed me incorrect direction. The answer did not work because using it, I received the following error:

    Error: package or namespace load failed for ‘ggplot2’:
 .onLoad failed in loadNamespace() for 'pillar', details:
  call: utils::packageVersion("vctrs")
  error: there is no package called ‘vctrs’

Now vctrs was in the '/path/where/library/is/located' so I think dependent packages were not being loaded from that path but the default for Rscript. Putting a print(.libPaths() in the script gave

"/usr/local/Cellar/r/3.6.3_1/lib/R/library"

instead of

[1] "/Users/Chris/Sites/app_name/renv/library/R-3.6/x86_64-apple-darwin18.7.0"
[2] "/private/var/folders/5_/p_yl0439059b7_jdqzrm0hr40000gr/T/RtmptdHcWN/renv-system-library"

for .libPaths() in Rstudio. Looking at the ruby program that was actually running the Rscript program, I found it was being run with the --vanilla option, i.e

Rscript --vanilla script_name

Removing the --vanilla option fixed the problem. I think that scripts with the --vanilla option stopped working because I reinsalled R using brew to fix another problem I was having and as part of this issued this command:

brew link --overwrite r 
Obromios
  • 15,408
  • 15
  • 72
  • 127