Gregor Kvas
Gregor Kvas

Reputation: 161

R default packages loaded despite using .Rprofile

I use R 3.5.1 in RGui/RStudio and a '.Rprofile' file in my user's home directory with a single entry to preload the package 'tidyverse': library(tidyverse)

When wanting to use the filter() function of package 'dplyr' it gets masked by the filter() function of package 'stats', that has been loaded as a default package AFTER sourcing '.Rprofile' in the R startup process.

This behaviour seems to be contradictory to what ?Startup tells us: "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."

Can someone tell me please, why the default packages like 'stats' are beeing loaded despite of using a user profile file? Thanks a lot!

Upvotes: 6

Views: 881

Answers (2)

Captain Tyler
Captain Tyler

Reputation: 594

.Rprofile runs before R loads basic libraries load package stats first

library('stats', warn.conflicts = TRUE, verbose = TRUE) 
library('dplyr', warn.conflicts = TRUE, verbose = TRUE)

Or alternatively, load the conflicted package after loading your libraries. That way, the order does not matter:

conflicted::conflicts_prefer(dplyr::filter())

Upvotes: 2

Gregor Kvas
Gregor Kvas

Reputation: 161

My question has already been answered here: R dplyr filter not masking base filter? [duplicate]

As outlined before, the documentation of ?Startup 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.

Unfortunately this can be easily misunderstood and therefore had initially led to my question. The phrase "only the base package is loaded" means, that only the base package will be loaded as the very first package in the startup process, but other default packages like stats will be loaded AFTER the packages sourced via an user .Rprofile file.

This is the reason why the filter() function of package dplyr being loaded in an user .Rprofile file during the startup process gets masked by the filter() function of the default package stats, being loaded AFTER the sourced user .Rprofile file.

Upvotes: 2

Related Questions