How to Find All Functions in an R Package

Is there a command in R to view all the functions present in a package?

You can use lsf.str.

For instance:

lsf.str("package:dplyr")

To list all objects in the package use ls

ls("package:dplyr")

Note that the package must be attached.

To see the list of currently loaded packages use

search()

Alternatively calling the help would also do, even if the package is not attached:

help(package = dplyr)

Finally, you can use RStudio which provides an autocomplete function. So, for instance, typing dplyr:: in the console or while editing a file will result in a popup list of all dplyr functions/objects.

How to find all functions in an R package?

I am guessing that you are just looking for help(package = caTools), which will open your browser to the relevant help page that lists all the functions in the "caTools" package.

You can also try: library(help = caTools), but that doesn't seem to capture everything. The nice thing about this latter approach is that you can capture the output in case you needed to refer to it somewhere else:

x <- library(help = caTools)
x$info[[2]]
# [1] "LogitBoost LogitBoost Classification Algorithm"
# [2] "base64encode Convert R vectors to/from the Base64 format"
# [3] "caTools-package Tools: moving window statistics, GIF, Base64,"
# [4] " ROC AUC, etc."
# [5] "colAUC Column-wise Area Under ROC Curve (AUC)"
# [6] "combs All Combinations of k Elements from Vector v"
# [7] "predict.LogitBoost Prediction Based on LogitBoost Classification"
# [8] " Algorithm"
# [9] "read.ENVI Read and Write Binary Data in ENVI Format"
# [10] "read.gif Read and Write Images in GIF format"
# [11] "runmad Median Absolute Deviation of Moving Windows"
# [12] "runmean Mean of a Moving Window"
# [13] "runmin Minimum and Maximum of Moving Windows"
# [14] "runquantile Quantile of Moving Window"
# [15] "runsd Standard Deviation of Moving Windows"
# [16] "sample.split Split Data into Test and Train Set"
# [17] "sumexact Basic Sum Operations without Round-off Errors"
# [18] "trapz Trapezoid Rule Numerical Integration"

List of functions of a package

The closest thing I've been able to find for this is:

help(,"rjags")

The first parameter specifies the searched thing, second one specifies the package. By keeping only the second one, I hope to get all help pages that relate to that package. This is equivalent of

help(package = "rjags")

This might not work in general though, as in ?help the functionality of omitting the first parameter is described as

topic is not optional: if it is omitted R will give

  • If a package is specified, (text or, in interactive use only, HTML) information on the package, including hints/links to suitable help

    topics.

How to find how many functions does a package in R have? e.g. how many functions does MASS package has?

after you load MASS library, try this:
length(ls('package:MASS')) to get a count of all objects

use mode param ls.str() to change what you get back:

  • for functions: length(ls.str('package:MASS', mode='function'))

find all functions (including private) in a package

you can use asNamespace:

> methods(cbind)
[1] cbind.data.frame cbind.grobGrid cbind.ts*

Non-visible functions are asterisked
> r <- unclass(lsf.str(envir = asNamespace("stats"), all = T))
> r[grep("cbind.ts", r)]
[1] ".cbind.ts" "cbind.ts"

cbind.ts in stats package is invisible but can find in envir = asNamespace("stats").

Get all sourced functions

I think the best way would be to source the file into a temporary environment. Query that environment for all the functions, then copy those values to the parent environment.

my_source <- function(..., local=NULL) {
tmp <- new.env(parent=parent.frame())
source(..., local = tmp)
funs <- names(tmp)[unlist(eapply(tmp, is.function))]
for(x in names(tmp)) {
assign(x, tmp[[x]], envir = parent.frame())
}
list(functions=funs)
}

my_source("script.R")

List of all functions in all packages on CRAN?

library(collidr)

# This data.frame is ~300k rows, here are the first 10

collidr::CRANdf[1:10, ]

# package_names function_names
# 1 A3 A3-package
# 2 A3 a3
# 3 A3 a3.base
# 4 A3 a3.gen.default
# 5 A3 a3.lm
# 6 A3 a3.r2
# 7 A3 housing
# 8 A3 multifunctionality
# 9 A3 plot.A3
# 10 A3 plotPredictions
...

List of all functions in base R?

As noted by user alistaire in a comment, help(package = 'base') will show an index of the functions in the base package.

However, “base R” is generally understood to encompass more than just the base package, namely the other packages that are also by default loaded by R. This list of packages is accessible via getOption('defaultPackages') (but note that this list is user modifiable, e.g. in ~/.Rprofile!).

As of R 3.6.1, this list is (and has been, for a while)

[1] "datasets"  "utils"     "grDevices" "graphics"  "stats"     "methods"

If you want to list all exported symbols from these packages, you could use, for instance

base_packages = getOption('defaultPackages')
names(base_packages) = base_packages
lapply(base_packages, function (pkg) ls(paste0('package:', pkg)))

Find out which function(s) from certain package were used

The function list.functions.in.file from the {NCmisc}package seems to do what you're looking for. It returns a list of all functions used within a script and returns them separated by the package they're from.

An example: When you run the function over this dummy code (saved as an R script) which runs a few examples with functions from {ggplot2}, {dplyr}, and {tidyr}...

# ggplot2 examples
library(ggplot2)
ggplot(data = cars, aes(x = speed, y = dist)) + geom_point()
qplot(data = diamonds, x = carat, y = price, color = color)

#dplyr examples
library(dplyr)
filter(mtcars, cyl == 8)
select_(iris, "Petal.Length")

#tidyr examples
library(tidyr)
gather(iris, key = flower_att, value = measurement,
Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)

df <- data.frame(x = c("a", "b"), y = c(3, 4), z = c(5, 6))
df %>% spread(x, y) %>% gather(x, y, a:b, na.rm = TRUE)

you get the following list as output:

$`c("package:dplyr", "package:stats")`
[1] "filter"

$`package:base`
[1] "c" "data.frame" "library"

$`package:dplyr`
[1] "select_"

$`package:ggplot2`
[1] "aes" "geom_point" "ggplot" "qplot"

$`package:tidyr`
[1] "gather" "spread"


Related Topics



Leave a reply



Submit