Installing tidyverse is the first thing I did on my new work computer. It installs >50% of the packages I use on a daily basis. But, when I call library(tidyverse), only a few of them load! Is there a reason some load but not the others? How did that decision get made?
I assumed so. The data analyst who received data that doesn't require some string manipulation before they are ready to do anything else is a lucky person.
What would be really cool is the ability to customize what gets loaded, say, by setting an environment variable. That would complicate reproducibility, though, and could be tweaked in one's .Rprofile right now anyway.
More practical may be a function to load a minimal set (dplyr, tidyr and ggplot2, maybe?) and a maximal one (all the top-level packages), though the option may encourage bad coding habits/loading unused packages.
that you could call at the start of each session, or just put the actual library calls there so they're loaded on startup. As mentioned before, neither is recommended if you're worried about reproducibility.
Gotcha. I share a lot of code with coworkers, and it's only about 5 more lines of code at the beginning, so I'll just keep doing library(magrittr) at the top of the files. I probably should learn how to use .Rprofile at some point, though.
Mine's actually empty at the moment aside from some quoted-out .libPaths shenanigans that caused as much trouble as it saved.
I do use .Renviron to set environment variables for packages that require external resources, like reticulate and sergeant. It could also be used as a way to keep API keys out of scripts, but keyring is a much more secure alternative.
The reason I am intimidated by .Rprofile is because of .libPaths shenanigans! And keyring is great! Before I found out about that, I was keeping my database credentials in a CSV file stored locally -- not the best option!
FWIW I think it's a bad idea to load data analysis packages in your .Rprofile. It increases the likelihood your code won't be reproducible because you've forgotten to load an important package. (And similarly that's why library(tidyverse) will never be customisable - it has to work the same way on everyone's computers)
You can customize what packages get loaded into R generally by using the defaultPackages argument to options() and sticking it in .Rprofile. I do this for packages like bookdown or blogdown because these are not packages that users will need to load in order to run my code.
This avoids preloading any packages when you start R while at the same time causes those extra packages to be attached whenever tidyverse is attached.
PS. If you use startup::startup() in ~/.Rprofile, you can put the above in a standalone file e.g. ~/.Rprofile.d/package=tidyverse.R, which is then only process if and only if tidyverse is installed.
This has the same problem mentioned by @hadley earlier, though: you’re making your analysis harder to reproduce. At the very least, this should be wrapped in if (interactive()) {…}.
Yeah, I think it's best to reserve ~/.Rprofile tweaks for packages you use interactively, like (as @rdpeng suggests), blogdown and bookdown. In my ~/.Rprofile I have:
if (interactive()) {
suppressMessages(require(devtools))
suppressMessages(require(usethis))
suppressMessages(require(testthat))
}
One thing that surprised me is that dplyr doesn't appear to use the %<>% operator from magrittr, even though %>% is used. I really like the former! But I can always library(magrittr), so it's no biggie.
FWIW, I love stringr but encounter people who feel strongly about other string packages. They might not like a stringr default in library(tidyverse). I sense that there's a balance here between making people's lives easy and annoying them.