Seeking Functions in a Package not imported from other packages - r

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__"

Related

What is '!!' and '::' functions in R? [duplicate]

I am following a tutorial in Rbloggers and found the use of double colons, I looked online, but I couldn't find an explanation for their use.
Here is an example of their use.
df <- dplyr::data_frame(
year = c(2015, NA, NA, NA),
trt = c("A", NA, "B", NA)
)
I understand it creates a data frame but I don't understand their purpose.
As you probably have looked up the help page by now usage of :: helps to access the exact function from that specific package. When you load dplyr you probably got a message as follows..
The following objects are masked from ‘package:base’:
intersect, setdiff, setequal, union
So, for instance, if you would like to use intersect function from dplyr or base package, you need to specify using the :: double colons. Usage will be as follows
mtcars$model <- rownames(mtcars)
first <- mtcars[1:20, ]
second <- mtcars[10:20, ]
dplyr::intersect(first, second)
base::intersect(first, second)
Update: Added additional explanation
Note: The sequence you load libraries determine the preferential access of the specific functions. Developers of different package tend to use same function names. However, when R encounters a function, it runs through the different libraries that particular session has loaded in a sequential manner. You can check the packages in a session by running (.packages())
[1] "tidyr" "data.table" "dplyr" "stats"
[5] "graphics" "grDevices" "utils" "datasets"
[9] "methods" "base"
As you can see in my example session above, tidyr is the last library I loaded, which is r session 1st entry. So, when you use any function in your code , first it is searched in tidyr -> then data.table -> then dplyr and so on, finally the base package is looked up. So, in this process when there is function name overlaps between packages the one which loaded the last masks the previous ones. To avoid this masking, you specify in R code where to look for the function. Hence, here base::intersect, will use the function from base library instead of the dplyr. Alternatively, you can use to avoid loading of complete library. There are positives and negatives with this. Read the links and learn more.
run and check the differences.
Here are some resources for you to get an understanding.
Compare library(), require(), ::
Namespace
There may be multiple functions with the same name in multiple packages. The double colon operator allows you to specify the specific function you want:
package::functionname

Is it possible to get the environment that a value originated from? [duplicate]

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.

Check if a function name is used in existing CRAN packages

I am creating an R package that I plan to submit to CRAN. How can I check if any of my function names conflict with function names in packages already on CRAN? Before my package goes public, it's still easy to change the names of functions, and I'd like to be a good citizen and avoid conflicts where possible.
For instance, the packages MASS and dplyr both have functions called "select". I'd like to avoid that sort of collision.
There are a lot of packages (9008 at the moment, Aug 2016), so it is almost certainly better to only look at a subset you want to avoid clashes with. Also, to re-emphasise some of the good advice in the comments (just for the record in case comments get deleted, or hidden):
sharing function names with other packages is not really a big problem, and not worth avoiding beyond, perhaps avoiding clashes with common packages that are most likely to be loaded at the same time (thanks #Nicola and #Joran)
Unnecessarily avoiding re-usue of names "leads to bad function names because the good ones are taken" (#Konrad Rudolph)
But, if you really want to check all the packages, perhaps to at least know which packages use the same names as yours, you can get a vector of the package names by
crans <- available.packages()[, "Package"]
# A3 abbyyR abc ABCanalysis abc.data abcdeFBA
# "A3" "abbyyR" "abc" "ABCanalysis" "abc.data" "abcdeFBA"
length(crans)
# [1] 9008
You can then install them in bulk using
N = 4 # only using the 1st 4 packages here -
# doing it for the whole lot will take a lot of time and disk space!!!
install.packages(crans[1:N])
Then you can get a list of the function names in these packages with
existing_functions = sapply(1:N, function(i) ls(getNamespace(crans[i])))

Find functions with specific arguments

How can you find the names and locations of all the functions that have a specific argument? Is there a way to find them for functions in the global environment, attached packages, and installed packages?
I assume that you ask the question just to not lose Ben great answer.
Here I slightly modify Ben answer to search for any argument :
uses_arg <- function(x,arg)
is.function(fx <- get(x)) &&
arg %in% names(formals(fx))
For example to get function with na.rm argument :
basevals <- ls(pos="package:base") ## package name : here I use the base package
basevals[sapply(basevals,uses_arg,'na.rm')]
EDIT
better to name argument of ls in conjunction with asNamespace :
basevals <- ls(asNamespace('base'))
basevals[sapply(basevals,uses_arg,'na.rm')]

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

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.

Resources