Masking Functions

What is Masking Functions?
I read in training that we have to put the {tidyverse} at the end to avoid the masking functions, what does it mean?

1 Like

@kmislamsaeed thank you for posting! I have moved your question to the R code area so that other readers can benefit from the responses.

It is possible that two packages contain functions with the same name. For example:

  • The package {dplyr} (which is part of the tidyverse meta-package) contains a function filter()
  • The package {stats} contains a function filter()

These are two different functions, they just both happen to be called “filter()”.

This situation is possible because each package is developed independently, and such overlap can occur as R evolves.

When you load packages, R loads them in the order written in your script. Here are two examples of loading packages:

pacman::p_load(rio, janitor, tidyverse, stats)

or

library(rio)
library(janitor)
library(tidyverse)
library(stats)

In both of the above examples, the {stats} package is loaded after the {tidyverse} package. This means that any following R code will use filter() from the {stats} package, by default.

So, if you have code like this below, in which you intend to use the filter() function from the tidyverse…

linelist %>%
   filter(case_def == "confirmed")

…this will probably result in an error or unexpected result, because the R is using filter() from {stats}, but you are providing the arguments for filter() from {dplyr}.

There are two ways to solve this:

  1. Explicitly tell R which package you want to use for each functions, using the :: syntax which connects the package name and its function name.
linelist %>%
   dplyr::filter(case_def == "confirmed")

or

  1. In your, command which loads packages, list tidyverse (and therefore dplyr) later
pacman::p_load(rio, janitor, stats, tidyverse)

We recommend #2 in most cases, because users are far more likely to use tidyverse on a routine basis.

I hope this helps!
Here is another explanation

2 Likes