16

I am having trouble with my workflow because I am sourcing multiple scripts in rmarkdown, some of which require the package dplyr and some of which use plyr.

The problem is that the rename function exists in both packages and if dplyr is currently attached the rename function in plyr won't work.

How do I include in my scripts a function that checks if dplyr is attached, and, if it is, detach it?

I know how to detach packages via detach("package:dplyr", unload = TRUE). What I don't know is how to check if a package is attached or not.

llewmills
  • 2,959
  • 3
  • 31
  • 58
  • 4
    probably easier to be explicit in the function call ... `plyr::rename` – user20650 Jun 06 '16 at 23:59
  • 2
    Rather than modifying the attached packages (which may not be what the user wants!) you should be specific and use `dplyr::rename` or `plyr::rename`. – Gregor Thomas Jun 07 '16 at 00:00
  • Or better yet, refactor your code to use one version of rename consistently. – Hong Ooi Jun 07 '16 at 00:22
  • Thank you @user20650 @Gregor and @Hong Ooi. Good suggestion. Does this mean that every function call you should specify the package prior to the function, or do you just mean only do it for the packages that you know ahead of time don't play nicely together? In future i will be using `data.table::setnames` to rename specific columns instead – llewmills Jun 07 '16 at 00:41
  • @ Hong Ooi I don't know what you mean when you say 'refactor your code to use one version of rename consistently? – llewmills Jun 07 '16 at 00:55

2 Answers2

27

I agree the best approach is to use dplyr::rename and plyr::rename to explicitely call the function you want.

However, if you did want to check if a package is attached, and then detatch it I use

if("plyr" %in% (.packages())){
  detach("package:plyr", unload=TRUE) 
}
SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
1

Worth noting here is that the packages themselves warn you to load them in a specific order. If you load dplyr, then plyr, you'll get a warning:

You have loaded plyr after dplyr - this is likely to cause problems. If you need functions from both plyr and dplyr, please load plyr first, then dplyr: library(plyr); library(dplyr)

My understanding is that dplyr doesn't work well if its functions get deprecated by plyr, but since the functions that dplyr deprecates from plyr are effectively updates, they should play nicely. So just make sure you load them in the right order:

library(plyr)
library(dplyr)

EDIT: I re-read your question, and your problem is a deprecation of plyr function by dplyr, so my point isn't very relevant to you, sorry. I'll leave it here in case someone else needs the information, since it caused me issues a while back :P

rosscova
  • 5,430
  • 1
  • 22
  • 35
  • Thankyou @rosscova. The problem is I have an rmarkdown document sourcing a lot of different scripts with a lot of code in them, written over a fairly long time period so it is difficult for me to remember where dplyr has popped up in those scripts (and what scripts *those* scripts have sourced). I guess these are just things you learn with experience. – llewmills Jun 07 '16 at 00:47