Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name of a package for a given function in R [duplicate]

Tags:

r

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?

like image 255
nhern121 Avatar asked May 11 '12 14:05

nhern121


People also ask

How do you call a function from a specific 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.

What is package in R programming?

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


1 Answers

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.

like image 96
Ben Bolker Avatar answered Nov 14 '22 23:11

Ben Bolker