Angus
Angus

Reputation: 65

Conflict of Packages in RStudio, detach() fails to work

I am currently doing Logistic Regression on the 'birthwt' data set found within R. This data is found within the package 'MASS'.

However, when I use library(MASS) to retrieve the data, it masks the function of select() from the dplyr package. I use this function almost immediately in my analysis.

After loading the data, I attempt
detach("package:MASS", unload = TRUE)

but I am met with
‘MASS’ namespace cannot be unloaded: namespace ‘MASS’ is imported by ‘pbkrtest’, ‘car’, ‘lme4’ so cannot be unloaded

I wold really love to sort this out as I have completed all my necessary analysis on the data, but was met with this issue when attempting to knit.

Thank you in advance for any help!

Upvotes: 1

Views: 1212

Answers (1)

user2554330
user2554330

Reputation: 44867

You shouldn't choose unload = TRUE. The default is unload = FALSE, and that's what you need.

Here's the explanation:

In R, packages can be "loaded", which makes them available to other packages that import functions from them. They can also be "attached", which puts them on the search list, so that they are available to the user in the console. If a package is attached, it needs to be loaded, but the reverse is not true.

So if you run detach("package:MASS"), you will remove it from the search list, and in the console, running select() will no longer find the function in MASS. It will still be loaded, so will be available to the other packages that need it.

By the way, using the prefix form MASS::select() or dplyr::select() will work regardless of whether either or both packages are in your search list.

Upvotes: 4

Related Questions