Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the package name of a function in R? [duplicate]

I'm debugging some code and I think I might have twice the same function in 2 packages.

I want to output the package name of the function as it would be executed by the R console.

Examples :

  • function_package_name(print) # --> base
  • function_package_name(select) # --> dplyr

I cannot simply use ?select because I think it links to the choice of 2 packages: dplyr and MASS.

How can I know which select function I'm using ?

NB : this is NOT a duplicate of list all functions on CRAN, nor of Find the package names from a function name in R, so sos::findFn() is not an acceptable answer ! I'm not looking for potential other functions named like this one, I'm looking for the package name of the one I'm currently using !

like image 714
Dan Chaltiel Avatar asked Jun 22 '17 10:06

Dan Chaltiel


People also ask

How do I specify a function from a package in R?

Every time you start a new R session you'll need to load the packages/libraries that contain functions you want to use, using either library() or require() . If you load a package with the same function name as in another package, you can use packageName::functionName() to call the function directly.

How can I see what packages are in an R package?

We can find the functions inside a package by using lsf. str function but we need to load the package prior to knowing the functions inside.

How do I clear loaded packages in R?

You can do both by restarting your R session in RStudio with the keyboard shortcut Ctrl+Shift+F10 which will totally clear your global environment of both objects and loaded packages.


1 Answers

Perhaps even most convenient, if you are just after the package name:

environmentName(environment(select))

The advantage is that this produces a string rather than an environment object.

like image 182
jkt Avatar answered Oct 05 '22 03:10

jkt