/ Home / Blog

Launching RStudio from the command line

January 6, 2020

Open RStudio from the terminal like VS Code #

A quick Zsh or Bash function to open an RStudio project from the command line similar to opening Visual Studio Code with:

$ code .

Configure #

Place the function below in your .zshrc or comparable .bashrc configuration and re-source:

rs () {
  if [ -z "$1" ] ; then
    dir="."
  else
    dir="$1"
  fi
  cmd="proj <- list.files('$dir', pattern = '*.Rproj$', full.names = TRUE); if (length(proj) > 0) { system(paste('open -na Rstudio', proj[1])) } else { cat('No .Rproj file found in directory.\n') };"
  Rscript -e $cmd
}
This function requires a local installation of R to work (i.e. Rscript).

Use #

$ rs

will either yield

No .Rproj file found in directory.

or open the project in the current working directory in RStudio.

Have fun 🚀 ✨

If you have any questions or feedback, feel free to reach out on X @siegerts.