1

I have the following R script ~/test.R :

print(.libPaths())
print(system(command = "whoami",ignore.stderr = TRUE))
library(lubridate)
ymd("2022-09-15")

If I run this script from the terminal with /opt/R/3.6.2/lib64/R/bin/Rscript test.R > test2.log I get the following output:

[1] "/home/domain/username/R/library/3.6.2"
[2] "/applis/R/site-library/x86_64-pc-linux-gnu/3.6.2" 
[3] "/opt/R/3.6.2/lib64/R/library"                     
username@domain
[1] 0
[1] "2022-09-15"

So it's working as intended and I have 3 paths for packages. Now let's run this script with cron :

* * * * * /opt/R/3.6.2/lib64/R/bin/Rscript $HOME/test.R > $HOME/test.log 2>&1

I get this for test.log:

[1] "/opt/R/3.6.2/lib64/R/library"
username@domain
[1] 0
Error in library(lubridate) : 
  aucun package nommé ‘lubridate’ n'est trouvé
Exécution arrêtée

So I only have one path for libraries, consequently lubridate is not found, because it's installed in /home/domain/username/R/library/3.6.2. I cannot install packages within /opt/R/3.6.2/lib64/R/library, so I'm looking for a way to add libpaths to crontab.

Augustin
  • 307
  • 2
  • 10
  • You are so close. So use `,libPaths()`, or one of the other options (see `help(Startup)`) to set your library paths for the root user. [ This question is also a duplicate a few times over. ] – Dirk Eddelbuettel Sep 15 '22 at 13:38
  • I don't have the root password. I found a workaround by adding `.libPaths(c(.libPaths(), "/home/domain/username/R/library/3.6.2/"))` at the beginning of my R file but it's a bit hacky... – Augustin Sep 15 '22 at 13:57
  • 1
    You do not need the root password. Read `help(.libPaths)` -- calling `.libPaths("/some/dir")` *appends* it does not overwrite so all you need is `.libPaths("//home/domain/username/R/library/3.6.2/")`. – Dirk Eddelbuettel Sep 15 '22 at 14:08
  • Correction, meant to say _pre-pends_ it. New additions always come first and are searched first. – Dirk Eddelbuettel Sep 15 '22 at 14:26

0 Answers0