简体   繁体   English

检查当前是否附有r包

[英]Checking if an r package is currently attached

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 . 我的工作流程有问题,因为我在rmarkdown中采购了多个脚本,其中一些需要包dplyr ,其中一些使用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. 问题是,在rename功能在两个包是否存在以及dplyr当前连接的rename的功能plyr将无法正常工作。

How do I include in my scripts a function that checks if dplyr is attached, and, if it is, detach it? 如何在我的脚本中包含一个检查是否附加了dplyr的函数,如果是,则将其分离?

I know how to detach packages via detach("package:dplyr", unload = TRUE) . 我知道如何通过detach("package:dplyr", unload = TRUE)分离包detach("package:dplyr", unload = TRUE) What I don't know is how to check if a package is attached or not. 我不知道的是如何检查是否附加了包裹。

I agree the best approach is to use dplyr::rename and plyr::rename to explicitely call the function you want. 我同意最好的方法是使用dplyr::renameplyr::rename来明确地调用你想要的函数。

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) 
}

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: 如果你加载dplyr,那么plyr,你会得到一个警告:

You have loaded plyr after dplyr - this is likely to cause problems. 你在dplyr之后加载了plyr - 这可能会导致问题。 If you need functions from both plyr and dplyr, please load plyr first, then dplyr: library(plyr); 如果你需要plyr和dplyr的功能,请首先加载plyr,然后加载dplyr:library(plyr); library(dplyr) 库(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. 我的理解是,如果dplyr的函数被plyr弃用,那么dplyr效果不好,但是由于dplyr从plyr中弃用的函数是有效更新的,所以它们应该很好地运行。 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. 编辑:我重新阅读你的问题,你的问题是dplyr对plyr函数的弃用,所以我的观点与你不太相关,对不起。 I'll leave it here in case someone else needs the information, since it caused me issues a while back :P 我会把它留在这里以防其他人需要这些信息,因为它引起了我一段时间的问题:P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM