1

I am trying to measure the peak memory usage of an R script using /usr/bin/time. The following code works fine:

Rscript testMemoryUsage.R

as does:

time Rscript testMemoryUsage.R

However, the following doesn't work:

/usr/bin/time Rscript testMemoryUsage.R

The error is due to packages not being found:

"Error in library(tidyverse) : there is no package called ‘tidyverse’ Calls: suppressPackageStartupMessages -> withCallingHandlers -> library Execution halted Command exited with non-zero status 1"

I realize there is not much reproducible code but at a conceptually level, why does the shell built in time command work here but not the executable /usr/bin/time? How can I get /usr/bin/time to use the same R environment where the packages are installed? Thank you.

David_G
  • 77
  • 4
  • Is `Rscript` wrapped by an alias? A shell function? We need to know how it's implemented before we can say how to fix this. Run `type Rscript` (assuming your shell really is bash) and [edit] the output into the question. – Charles Duffy Apr 05 '23 at 19:35

1 Answers1

0

This makes no sense unless you set some things in your per-user dotfiles that are not invoked via /usr/bin/time. Witness:

$ Rscript -e 'length(Sys.getenv())'
[1] 67
$ time Rscript -e 'length(Sys.getenv())'
[1] 67

real    0m0.355s
user    0m0.289s
sys     0m0.062s
$ /usr/bin/time Rscript -e 'length(Sys.getenv())'
[1] 67
0.31user 0.07system 0:00.39elapsed 98%CPU (0avgtext+0avgdata 65044maxresident)k
0inputs+8outputs (0major+17430minor)pagefaults 0swaps
$ 

I used the number of environment variables R knows as a test here, you could equally do

$ Rscript -e 'print(.libPaths())'
[1] "/usr/local/lib/R/site-library" "/usr/lib/R/site-library" 
[3] "/usr/lib/R/library"           
$ 

to drill down where/how the setups differ for you. But in short it has to be with how you control how R runs / is set up for you.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725