1

I've tried to create a "shortcut" that checks if a package is installed and naturally thought of going for ifelse. Below is a reproducible example:

ifelse(!"tree"%in%names(installed.packages()),install.packages("tree"),library(tree))

EDIT::

The package is downloaded but I still get this error presumably because the above continues to be called somehow.

Error in ans[test & ok] <- rep(yes, length.out = length(ans))[test & ok] : replacement has length zero In addition: Warning message: In rep(yes, length.out = length(ans)) : 'x' is NULL so the result will be NULL

The above appears to work except after downloading the package, an error is returned that I can't seem to figure out exactly how to solve:

I could use a different way to achieve the same but would really love to know what's going on here. Thanks!

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
  • 2
    `ifelse()` will evaluate all of its parameters and try to return a vector the same length as the input. It's not really appropriate in this case (conditional execution). You should use a standard `if/else` expression. – MrFlick Feb 19 '19 at 17:05
  • 1
    `names(installed.packages())` returns `NULL`, you can get the package names with `rownames()` – astrofunkswag Feb 19 '19 at 17:08
  • @MrFlick makes sense. Thanks! – NelsonGon Feb 19 '19 at 17:10

0 Answers0