Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Packages with the same function name

Tags:

r

dplyr

ggplot2

Libraries with the same function name in R seem to be very annoying. What is the easiest way to resolve issues like the following?

Attaching package: ‘dplyr’

The following objects are masked from ‘package:stats’:
filter, lag
The following objects are masked from ‘package:base’:
intersect, setdiff, setequal, union

adding library(stats) or calling the filter function as stats::filter and the other functions as it is shown below didn't work out for me.

library(ggplot2)
library(dplyr)
library(stats)
stats::filter
stats::lag 
base::union 
base::setdiff 
base::setequal 
base::intersect

# Reading in the data
data <- read.csv("data.csv", header = FALSE)

# Plots 
dataSummary  <- data %>% group_by(id) %>% summarise(data_count = x())
dataSummary
plotTest <- ggplot(dataSummary, aes(id, data_count)) + geom_bar(stat = 'identity')  + ggtitle("Test Title")
plot(plotTest) 

But this keeps giving the previous warning message before executing the plot function. Any pointers? or is there anyway to suppress these warnings and do the plotting?

like image 596
Desta Haileselassie Hagos Avatar asked Jan 26 '16 20:01

Desta Haileselassie Hagos


1 Answers

  1. If you just don't want the warnings to show up, load the package via

    library(dplyr, warn.conflicts = FALSE)
    

    However the major downside is that it just hides the problem, it doesn't stop the execution. If you need to actually use one of the masked functions, you can call it like stats::lag (@alistaire).

  2. Don't use packages that mask base functions. The general idea if running example("filter") (say) gives a different answer after loading a package is anti-social.

  3. Some packages "improve" the base functions, so masking isn't an issue.

  4. Order of loading the packages matters. First loaded package is the first in the search path if you are using a function that has been masked. See this answer for some insight.


This answer attempted to summarise the many comments that will (eventually) deleted.

like image 56
4 revs, 2 users 77% Avatar answered Oct 07 '22 14:10

4 revs, 2 users 77%