How do I use functions in one R package masked by another package?
You could run into deeper problems, but at the top level car::recode
should do the trick. Not sure what happens if recode uses functions that are also masked.
What does The following object is masked from 'package:xxx' mean?
The message means that both the packages have functions with the same names. In this particular case, the testthat
and assertive
packages contain five functions with the same name.
When two functions have the same name, which one gets called?
R will look through the search
path to find functions, and will use the first one that it finds.
search()
## [1] ".GlobalEnv" "package:assertive" "package:testthat"
## [4] "tools:rstudio" "package:stats" "package:graphics"
## [7] "package:grDevices" "package:utils" "package:datasets"
## [10] "package:methods" "Autoloads" "package:base"
In this case, since assertive
was loaded after testthat
, it appears earlier in the search path, so the functions in that package will be used.
is_true
## function (x, .xname = get_name_in_parent(x))
## {
## x <- coerce_to(x, "logical", .xname)
## call_and_name(function(x) {
## ok <- x & !is.na(x)
## set_cause(ok, ifelse(is.na(x), "missing", "false"))
## }, x)
## }
<bytecode: 0x0000000004fc9f10>
<environment: namespace:assertive.base>
The functions in testthat
are not accessible in the usual way; that is, they have been masked.
What if I want to use one of the masked functions?
You can explicitly provide a package name when you call a function, using the double colon operator, ::
. For example:
testthat::is_true
## function ()
## {
## function(x) expect_true(x)
## }
## <environment: namespace:testthat>
How do I suppress the message?
If you know about the function name clash, and don't want to see it again, you can suppress the message by passing warn.conflicts = FALSE
to library
.
library(testthat)
library(assertive, warn.conflicts = FALSE)
# No output this time
Alternatively, suppress the message with suppressPackageStartupMessages
:
library(testthat)
suppressPackageStartupMessages(library(assertive))
# Also no output
Impact of R's Startup Procedures on Function Masking
If you have altered some of R's startup configuration options (see ?Startup
) you may experience different function masking behavior than you might expect. The precise order that things happen as laid out in ?Startup
should solve most mysteries.
For example, the documentation there says:
Note that when the site and user profile files are sourced only the
base package is loaded, so objects in other packages need to be
referred to by e.g. utils::dump.frames or after explicitly loading the
package concerned.
Which implies that when 3rd party packages are loaded via files like .Rprofile
you may see functions from those packages masked by those in default packages like stats, rather than the reverse, if you loaded the 3rd party package after R's startup procedure is complete.
How do I list all the masked functions?
First, get a character vector of all the environments on the search path. For convenience, we'll name each element of this vector with its own value.
library(dplyr)
envs <- search() %>% setNames(., .)
For each environment, get the exported functions (and other variables).
fns <- lapply(envs, ls)
Turn this into a data frame, for easy use with dplyr.
fns_by_env <- data_frame(
env = rep.int(names(fns), lengths(fns)),
fn = unlist(fns)
)
Find cases where the object appears more than once.
fns_by_env %>%
group_by(fn) %>%
tally() %>%
filter(n > 1) %>%
inner_join(fns_by_env)
To test this, try loading some packages with known conflicts (e.g., Hmisc
, AnnotationDbi
).
How do I prevent name conflict bugs?
The conflicted
package throws an error with a helpful error message, whenever you try to use a variable with an ambiguous name.
library(conflicted)
library(Hmisc)
units
## Error: units found in 2 packages. You must indicate which one you want with ::
## * Hmisc::units
## * base::units
How can I prevent a library from masking functions
- It is possible to prevent some functions from being masked?
I don't believe so but I could be wrong. I'm not sure what this would look like
- Is it possible to mask "the masking function" (e.g. xgboost::slice) with an early imported function (e.g. dplyr::slice)?
If you're asking about just or use in an interactive session you can always just define slice
to be the function you actually want to use like so
slice <- dplyr::slice
and then you can use slice
as if it is the dplyr version (because now it is).
Masked functions in R
UsingR::QQPlot()
and/or QRMlib::QQPlot()
Try to load preferred library last, since it will mask the "unwanted" function...
All the best!
list of masked functions in R
in R base:
conflicts(detail=TRUE)
And to find the list of environments that contain a version of
getAnywhere(x = "functionA")
Note: getAnywhere
also finds the functions which are not exported. and that are hence not creating conflicts.
A better (simpler) result could be obtained using:
x = "functionA"
names(which(sapply(search(), FUN = function(env) exists(x, env, inherits = FALSE, mode = "function"))))
Masking methods in R
The conflicted
package (see here) now offers a potential solution to this problem. With conflicted
loaded, you get more explicit error messages about conflicting function names. You also can use conflict_prefer
(details here) to specify which package's function you want to use by default and which should be masked.
For example, here is a recent error I got when attempting to use the function parallel
from the nFactors
package:
# Error: [conflicted] `parallel` found in 2 packages.
# Either pick the one you want with `::`
# * nFactors::parallel
# * lattice::parallel
# Or declare a preference with `conflict_prefer()`
# * conflict_prefer("parallel", "nFactors")
# * conflict_prefer("parallel", "lattice")
I then added
conflict_prefer("parallel", "nFactors")
right after the code loading my libraries at the beginning of the script to make sure that parallel
would call nFactors::parallel
in my code.
How to unmask functions masked by `library(...)`?
As someone suggested in the comments, just redefine the vanilla baz
.
baz <- bar::baz
R: Masked Functions
Use stats::reorder()
to reference the version in stats.
Related Topics
Filter Data Frame Rows Based on Values in Vector
Making a Stacked Bar Plot for Multiple Variables - Ggplot2 in R
Cumulative Sum That Resets When 0 Is Encountered
For Loop Over Dygraph Does Not Work in R
How to Remove Empty Factors from Ggplot2 Facets
Longest Common Substring in R Finding Non-Contiguous Matches Between the Two Strings
Subsetting a Data.Table Using !=<Some Non-Na> Excludes Na Too
Finding Point of Intersection in R
Issue with Geom_Text When Using Position_Dodge
Add "Filename" Column to Table as Multiple Files Are Read and Bound
Merge and Perfectly Align Histogram and Boxplot Using Ggplot2
Meaning of Ddply Error: 'Names' Attribute [9] Must Be the Same Length as the Vector [1]
Combine Rows in Data Frame Containing Na to Make Complete Row
Ggplot2: Change Order of Display of a Factor Variable on an Axis
Ggplot Separate Legend and Plot
Reading Multiple Files and Calculating Mean Based on User Input
Subsetting Data.Table Using Variables with Same Name as Column