Do you hate clicking in several places in RStudio to set up your working directory correctly? Or even worse, hard-coding it and have all your code break when you move your working folder?

There is a one-line trick to set up the working directory in RStudio to your current directory:

setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

But first, you need to install the rstudio api library:

install.packages("rstudioapi")