Possible Duplicate:
How do you you determine the namespace of a function?
I don't know how to do this... How do you know the package name for a certain function in R? I would like to have a function that given the name of a function, returns the name of the package that owns it. Any suggestion?
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.
We can find the functions inside a package by using lsf.
R packages are extensions to the R statistical programming language. R packages contain code, data, and documentation in a standardised collection format that can be installed by users of R, typically via a centralised software repository such as CRAN (the Comprehensive R Archive Network).
There may be better solutions, but find("functionname")
seems to work reasonably well? However, it only works for loaded packages.
> find("strwidth")
[1] "package:graphics"
> find("qplot")
character(0)
> library(ggplot2)
> find("qplot")
[1] "package:ggplot2"
>
(If you need the raw name of the package you can use gsub("^package:","",results)
)
(The answers to the previous question linked by Andrie include this answer; they don't give the bit about gsub
, and they all seem to share the issue of not finding non-loaded packages.)
Here's a quick hack to find functions even in non-loaded packages:
findAllFun <- function(f) {
h <- help.search(paste0("^",f,"$"),agrep=FALSE)
h$matches[,"Package"]
}
findAllFun("qplot")
## "ggplot2"
findAllFun("lambertW")
## "emdbook" "VGAM"
> findAllFun("xYplot")
## "Hmisc" "lattice"
If you need to find functions in non-installed packages (i.e. searching CRAN), then findFn
from the sos
package will be your friend.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With