This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Masked functions in R
R: Masked Functions
function naming conflicts
If I have two packages: A and B. Say there is function named funfun in A and there is function named funfun in B too. When I load A and B, how do I use the first funfun?
require(A)
require(B)
If I want to use funfun in A, how do I write this?
You can explictily refer to a package and function combination like this:
A::funfun
B::funfun
In unusual circumstances, you may have to refer to functions that are not exported in the namespace, in which case you need to use:
A:::funfun
B:::funfun
(But this would be unusual, and since non-exported functions do not form part of the package API, these functions could change without warning in subsequent releases of a package.)
Related
This question already has an answer here:
Is it a good practice to call functions in a package via ::
(1 answer)
Closed 2 years ago.
I don't like that you can have R package conflicts in function calls, or namespace collisions. For instance, plyr and dplyr have functions with the same names, so if you have them both loaded you need to know which functions these are so you can attach and detach packages appropriately. Sure, in this example, dplyr is meant to replace plyr which is why conflicts emerge, but this can hypothetically happen with any number of packages. What a nightmare! This isn't an issue in Python because you prefix the function call with the name/alias of the package you imported, e.g. pd.melt().
So, my question is: is there any equivalent way to do this in R? Can you manage package conflicts by explicitly referencing the package in a function call?
I see someone asked basically the same question here six years ago, and it remains unsolved. The only answer offered is to check out the conflicted package. This is a start, lending transparency to the conflicts, but you have to dig through the package to find anything better than that (update: see comments below to find references to which features of the package are most useful for this issue).
Update
There are a number of solutions in the comments, including those found in this post. But, while that post answers my question, it doesn't ask it, which might be part of the reason I didn't find it in my search. It starts with the assumption that you already know the quick and dirty solution of using the proper prefix syntax. So, it might be best to leave this post up as a non-duplicate for future searchers.
Just prefix the package name with a double colon:
<package>::<function>()
For instance:
ggplot2::ggplot(data=data, ggplot2::aes(x=x)) +
ggplot2::geom_histogram()
This question already has answers here:
R: What do you call the :: and ::: operators and how do they differ?
(1 answer)
What are the double colons (::) in R?
(2 answers)
Closed 3 years ago.
I am new to R, when reading functions documentation how do I interpret ::?
How do I actually read the syntax?
readxl::read_excel()
Not sure if in R studio I can find any information about
In R there are different packages available,
"readxl" is one of them.
When you install a package, you are ready to use its functionalities.
If you just need a sporadic use of a few functions or data inside the
package you can access them with the notation packagename::functionname().
For example, since you have installed the readxl package, you can explore one of its functionality called read_excel() to import/read excel sheet.
Command to check what functions and data are contained in a package.
help(package = "packageName")
This question already has an answer here:
What is the benefit of import in a namespace in R?
(1 answer)
Closed 6 years ago.
I noticed that some answers on SO contain the use of pkg::name where name is typically a function.
What is the advantage of this over library(pkg); ... name() or require(pkg); ... name()? R help, (help("::")) says
For a package pkg, pkg::name returns the value of the exported variable name in namespace pkg, ... The namespace will be loaded if it was not loaded before the call, but the package will not be attached to the search path.
Does this mean that the function is used without the additional memory loss of loading the entire package, (ie, is it equivalent to import <function> from <package>) in python? Or is it simply a means of telling R use the function from this package when there may be ambiguities?
My question relates the use of :: in an Rscript or directly in the console and so is not a duplicate of the linked question as the OP in that question is discussing the use of of functions from the stats4 package during a package development project. On the other hand, there appear to be answers within this post that shed some light on my question, however. Thanks for the link. (Note the following discussion on Meta: duplicates flag)
It avoids namespace collisions but it still has to load the pkg.
Example => I did this:
pryr::mem_used()
dplyr::filter(mtcars, cyl==4)
pryr::mem_used()
in one R instance and:
pryr::mem_used()
library(dplyr)
filter(mtcars, cyl==4)
pryr::mem_used()
in another.
mem before/after for the 1st was: 27.7 MB / 30.6 MB
mem before/after for the 2nd was: 27.7 MB / 30.7 MB
I didn't do multiple tests or see if the difference was rounding or something else, but no there were no real savings there IMO.
There are two main reasons why I use this notation:
Disambiguation: Some packages provide functions that have the same name as those of base R or of functions from other packages. Loading such libraries therefore replaces certain functions. This effect is referred to as "masking". In such cases I consider it a better way of coding to use the notation package::function(), in order to clarify which of the homonymous functions is used - even if it is the function that has been loaded most recently into the namespace and it is therefore not necessary for obtaining the desired output. If a function is masked by another package it is the only way to address it.
For example, library(raster)
contains (and loads into the namespace) a function called stack() which is different from the base R function stack() in the utils package. In order to still use the function stack() from base R it should be called with utils::stack() once the raster library has been loaded.
Accessing functions that are not exported into the namespace: This case is much less frequent and slightly different. Some libraries foo.pkg contain functions which are not loaded into the namespace with library(foo.pkg). As a result, in such cases library(foo.pkg) does not help in order to access these functions.
The only example where I encounter this situation on a regular basis is the quite useful function cbind.na() from the qpcR package. It can only be accessed by specifying qpcR:::cbind.na(). Note that three colons are required in this case.
Upon reconsideration, there could also be a third reason for me to use this notation: code compactness. If I know that I will only need one specific function of a package, possibly only once in the code, without being interested in the rest of the functionalities offered by that package, then I find that the notation package::function() is preferable. This may not give any tangible advantage in R, but I adopted this style from the general advice in other programming languages to avoid namespace pollution.
This question already has answers here:
Show names of everything in a package
(4 answers)
Closed 5 years ago.
Is there an easy, friendly way to list all functions of a package without downloading those huge PDFs (package references)? I need this for getting me familiar with the package, finding proper functions etc.
I tried ?rjags but it doesn't do what I expected.
Load the package (for example the carpackage). Then use ls()
ls("package:car")
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
R: Masked Functions
I am using the packages moments and fUnitRoots. Both of them have a function called "kurtosis" (to be more precise, the package fUnitRoots calls the package timeDate which also has the function "kurtosis"). They give very different results, because their formulas is slightly different. I want to use the function skewness from the library moments, but if the two libraries are loaded, the function used is the one from fUnitRoots.
How can I specify from which library I want the given function to be used, without unloading one of the libraries?
Use the :: operator. The syntax is package::name. So it would be moments::kurtosis.