This question already has answers here:
Closed 10 years ago.
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?
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.
Related
This question already has answers here:
Closed 10 years ago.
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?
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.
This question already has answers here:
Error: could not find function ... in R
(10 answers)
Closed 1 year ago.
I am not able to use is.square.matrix() function for my matrix in R.
library(tidyverse)
install.packages(c("readr", "dplyr", "haven"))
install.packages(c("psych"))
install.packages("matlib")
matr <- cov(na.omit(airquality))
is.square.matrix(matr)
I am getting error for this function could not find is.square.matrix. Can someone help mw in figuring out what I am missing here?
is.square.matrix seems to be from matrixcalc package
# // if not installed, then install the package and load it
#install.packages('matrixcalc')
library(matrixcalc)
is.square.matrix(matr)
[1] TRUE
If we don't know from which package it is loaded, then use ?? to do the check on R console and then it opens a webpage with the help pages of similar/exact functions
??is.square.matrix
gives
This question already has answers here:
Load multiple packages at once
(10 answers)
Closed 3 years ago.
Instead of loading 30 packages each with the library function is it possible to do this in a loop?
pckgs = c("readr", "dplyr")
sapply(pckgs, library)
Background:
Before loading, i test if the packages are installed. For doing so i already have the package names in the form of c("readr", ..., "dplyr") and was wondering if i can also load the package in a loop instead of writing 30 times library().
What i tried:
I simplified to one package:
sapply("readr", library)
sapply("readr", function(lib) library(lib))
sapply("readr", function(lib) library(get(lib)))
Spoiler:
I wanted to post this question and then decided to check for a "force character" in the parameter and got lucky. (It is a bit weird asking a question and answering it yourself, but when i read this i felt motivated enough :) https://stackoverflow.com/help/self-answer
The parameter character.only can be used for that.
Example:
pckgs = c("readr", "dplyr")
sapply(pckgs, library, character.only = TRUE)
I want to acquire the list of functions, defined in the package and exported, but not the ones that were imported from other packages?
The following solutions are nice, but list also function re-exported:
Seeking Functions in a Package
getNamespaceExports() is mentioned in one of the answers to the question you linked; luckily, there is a companion to it, getNamespaceImports(). Then we can just find the setdiff() between the two. For example:
devtools_exports <- getNamespaceExports("devtools")
devtools_imports <- getNamespaceImports("devtools")
devtools_exported_not_imported <- setdiff(devtools_exports, devtools_imports)
"install_github" %in% devtools_exports
# [1] TRUE
"install_github" %in% devtools_exported_not_imported # comes from remotes
# [1] FALSE
Actually, I found one more solution that seems to work well:
unclass(lsf.str(envir = asNamespace('myPackage')))
The benefit is that I don't get these system variables:
"system.file" "library.dynam.unload" ".__global__"
I know ls("package:grid") and find.funs("package:grid") in mvbutils but apparently neither of them can find non-exported functions and methods that are only accessible internally or with ::: or getAnywhere.
I've had to source the files in the /R directory of the source package and use ls() on a clean global environment, but there must be a better way, no?
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").
This appears to be something of a perennial here.
If it's this one-liners you're after then this should be a contender (credit #Joshua):
ls(getNamespace("grid"), all.names=TRUE)
(Link is to a question that was asked after the above, but closely related).
As grid is a base package and I haven't yet moved up to R 3... I'm getting 756 functions with Version 2.15.1. vs. 503 from the unclass solution.