Masked objects loading dplyr - r

Everytime I load the dplyr package the console shows a warning message.
warning message Some objetcs are masked from other packages. I think this is because the objects have the same name. For example:
Filter has this usage in dplyr packagefilter(.data, ..., .preserve = FALSE)
Filter has this usage in stats package filter(x, filter, method = c("convolution", "recursive"), sides = 2, circular = FALSE, init)
How can I unmask the filter object from stats package if I need to use it?
Regards

You are correct that they are simply packages that share the same name. The comments above basically answer the question already. Theoretically if you have a conflict of functions you want to avoid, you can also select which you prefer, like so:
library(conflicted)
conflict_prefer("slice", # the function
"dplyr") # the package
And R will tell you which it will use as your primary:
[conflicted] Will prefer dplyr::slice over any other package
However that is an extra step and I prefer usually to name it explicitly like dplyr::slice as mentioned in the comments instead.

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

Basic dyplr functions give an error: "check_dots_used"

Trying to figure out why I am getting this error (never seen it before, google is no help):
Error in check_dots_used(action = warn) : unused argument (action = warn)
I am getting the error for the very basic trial below, but also for group_by/count, but not filter or select.
x <- c(1, 2, 3, 4, 5)
y <- runif(5)
tibble(x, y)
Error in check_dots_used(action = warn) : unused argument (action = warn)
I had the same problem: urganmax's solution to update to ellipsis-0.3.1 in a comment did it for me. Moving this as an answer so it is easier to find.
I had to update/re-install tidyverse and the ellipsis package explicitly:
install.packages("tidyverse")
install.packages("ellipsis")
Then restart your session and library your packages (or unload and reload your packages).
I'm pretty certain that your version of the ellipsis package (part of the tidyverse) is out of sync with the other tidyverse packages you have. update.packages() should work ... (if you want to live dangerously, update.packages(ask=FALSE) to update all packages without asking for confirmation for each one).
From the tidyverse NEWS file:
All functions that take ... have been instrumented with functions from the ellipsis package to warn if you’ve supplied arguments that are ignored (typically because you’ve misspelled an argument name) (#573).

What are the double colons (::) in R?

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

Lubridate Objects Masked After Loading Data.Table

When I load the data.table package after having already loaded the lubridate package, I get the following error message:
Loading required package: data.table
data.table 1.9.4 For help type: ?data.table
*** NB: by=.EACHI is now explicit. See README to restore previous behaviour.
Attaching package: ‘data.table’
The following objects are masked from ‘package:lubridate’:
hour, mday, month, quarter, wday, week, yday, year
Does anyone know a) what's causing this issue and b) how to prevent these objects within lubridate from being masked?
UPDATE:
The issue associated with the above is that I'm using the quarter function from the lubridate package and, after loading the data.table package, I can no longer do so in the same way.
Specifically, when I run quarter(Date, with_year=TRUE) (where Date is a vector of class = Dates), I now get the following error: Error in quarter(Date, with_year = TRUE) : unused argument (with_year = TRUE).
If I simply, quarter(Date), then I can get the desired output without the attached year. For example, if Date is set as simply May 15, 2015 (today), then quarter(Date) will yield 2 (since we're in the 2nd quarter of 2015), but I'd like it to yield 2015.2, hence the importance of the with_year = TRUE option.
Obviously, I can overcome this by using paste to bind together the year and the output of quarter(Date), but I'd prefer to avoid that work-around.
An object name in a package namespace is masked when a new object is defined with the same name. This can be done by the user assigning the name, or by attaching another package that has an object of the same name.
data.table and lubridate have overlapping function names. If you want the lubridate version to be the default, then the easiest solution is to load data.table first, then load lubridate---thus it will be the data.table versions of these functions that is masked by the "newer" lubridate versions.
library(data.table)
library(lubridate)
Otherwise, the solution is to use :: (as in package::function) to fully specify which version of the function you want to use, for example:
lubridate::quarter(Date, with_year = T)
Another option, which involves a little less typing but is perhaps a little less clear as well, would be to alias the lubridate functions you want in the global environment at the start of your script.
quarter = lubridate::quarter
Any use of quarter() later in the script will use the lubridate version of the function.
Yet another option is the conflicted package, which provides a system for preferring a function from one package. It is a bit more intense and intentional, you should definitely read the documentation before using it, but your script might include something like this:
library(conflicted)
conflict_prefer("quarter", "lubridate")
The package conflicted provides various alternatives and is a good practice to use it while loading libraries to be clear on the masking.
https://github.com/r-lib/conflicted

Remove/detach a package in a R session

My question is some packages share the same function name. How can I tell R which package that I want to use this function from?
I tried to load the package that I wanted to use again in the code but it still did not work. My case is the select in MASS and dplyr. I want to use dplyr but the error is always unused argument...
You can use the :: operator:
iris %>%
head(n = 3) %>%
dplyr::select(Sepal.Length)
See here for details.
Or detach MASS ala this post.

Resources