Site Last Updated: May 16, 2020

New R users should check out the R basics, visualization, and econometrics pages.

Python doesn’t have a monopoly on machine learning; check out my R for machine learning page.

I have a Google Scholar profile, from my previous life as an academic.

# install_packages.R

I recommend writing and maintaining a script that keeps track of all the packages you regularly use. When re-imaging a workstation, or upgrading to the newest version of R, you can simply run it once (instead of installing packages one at a time). Here is an example:

install_packages = function(pkg) {
new_pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if (length(new_pkg))
install.packages(new_pkg, dependencies = T, Ncpus = 4)
}

packages = c(
"devtools",  # R Development Tools
"janitor",   # Clean Dirty Data
"plm",       # Panel Linear Models
"statnet"    # Network Analysis
)

install_packages(packages)
rm(install_packages, packages)

It’s worth your time to look for useful RStudio add-ins every so often. There are two add-ins in particular that make my life substantially easier:

1. “AlignAssign” quickly aligns assignment operators, which when combined with RStudio’s “Reformat Code” function, makes writing pretty R code rather painless.
2. “RStudio-Addin-Snippets” enables quick back-to-forward slash conversion (and vice-versa). This comes in handy when you are copying and pasting file paths from Windows (which use backslashes) into an R script (file paths in R must use forward slashes). Though you could escape every backslash in paths, it looks terrible and can make it more difficult to collaborate with colleagues programming on macOS or Linux (which both use forward slashes in paths).

For ease of use, I assign a keyboard shortcut to both of these add-ins in RStudio (Tools > Modify Keyboard Shortcuts).

devtools::install_github("seasmith/AlignAssign")       # Align Assignment Operators
devtools::install_github("sfr/RStudio-Addin-Snippets") # Convert Slashes

You can avoid using the library() command with the power of lapply() when more than a handful of packages need to be loaded. Combine suppressMessages() with invisible() to completely suppress output.
packages = c("tidyverse", "lubridate", "janitor", "readxl")
rm(packages)