dataverse-r-study icon indicating copy to clipboard operation
dataverse-r-study copied to clipboard

Using `require()` is a bad idea, and so is `install.packages()`

Open gaborcsardi opened this issue 2 years ago • 2 comments

Because they do not stop on errors, that is why they are "best performing". You are basically ignoring errors:

{ require("foobar") ; install.packages(tempfile()); message("\nNOT GOOD!!!\n") }

See how "NOT GOOD" is printed here:

Loading required package: foobar
Installing package into ‘/Users/gaborcsardi/Library/R/arm64/4.2/library’
(as ‘lib’ is unspecified)

NOT GOOD!!!

Warning messages:
1: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called ‘foobar’
2: package ‘/var/folders/ph/fpcmzfd16rgbbk8mxvy9m2_h0000gn/T//Rtmph0Pd7A/file12c3c78c562f5’ is not available for this version of R

A version of this package for your version of R might be available elsewhere,
see the ideas at
https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages

OTOH you could argue that if the script finishes anyway, then those packages were not really needed in the first place....

gaborcsardi avatar Nov 30 '22 07:11 gaborcsardi

Hey @gaborcsardi, Thanks for opening the issue! Ok interesting! That may suggest that the actual results are worse than in my experiment. Do you have a suggestion on how the require/install.packages bit can be improved?

atrisovic avatar Dec 05 '22 02:12 atrisovic

You can check if the package can be loaded (with library() or loadNamespace()) after the installation.

gaborcsardi avatar Dec 05 '22 07:12 gaborcsardi