I'm facing some plyr
vs. dplyr
problems in my code, so I would like to detach the plyr
packge in case it has been loaded before.
This is how far I got,
ifelse(sum(grepl("package:plyr",search()))==0,
"plyr not loaded",
detach("package:plyr"))
but this code throws an
Error in ifelse(sum(grepl("package:plyr", search())) == 0,
"plyr not loaded", :
substitution has length 0
Additional warning:
In rep(no, length.out = length(ans)) :
'x' is NULL so the result will be NULL
Here's a solution:
if(any(grepl("package:plyr", search()))) detach("package:plyr") else message("plyr not loaded")
## plyr not loaded
library("plyr")
## if(any(grepl("package:plyr", search()))) detach("package:plyr") else message("plyr not loaded")
Generally you want to use if
and else
for program control and ifelse()
only for vectorized logical operations, like data recoding.