When you load libraries in .RProfile
they get attached very early in the R startup process, before the stats package is attached. The other way, you're attaching dplyr after stats has already been loaded. You can learn about R's startup process by typing ?Startup
. There it 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.
I've seen Hadley recommend against loading packages in .RProfile
for this reason, i.e. the discrepancies in package loading order, although personally I don't have strong feelings about it.
One possible solution is to simply add library(stats)
as the very first library call in your script, before loading dplyr.
Another (long term) option to avoid these sorts of issues more globally would be to transition your workflows from "a large collection of scripts" to one or more packages.