New RStudio projects directly from Spotlight

Do you want a way to quickly create and launch new RStudio projects directly from Spotlight, all saved in a sensible location? Automator has your back.
R
Published

May 15, 2020

I’ve created an Automator application to create new RStudio Projects, and it works fairly nicely.

When I run the script I’m prompted for a project name (e.g. tidytuesday-rock-salt) which then creates the following new directory:

/Users/charliejhadley/r-projects-scrapbook
├── 15-05-2020_tidytuesday-rock-salt
   └── tidytuesday-rock-salt.Rproj

… and voila, RStudio opens the project ready to start work.

If the concept of RStudio Projects is new to you, watch this video explaining their benefits.

If your scripts contain setwd(), please follow @JennyBryan’s advice and remove it!

Hadley Wickham shared his workflow for opening projects with Alfred back in 2018, but this only works for existing projects. The excellent {usethis} package includes create_project() which achieves much the same thing as my script, but it requires RStudio to already be open.

Recreating this on your machine

This solution only works for macOS users. It will just work provided you follow these instructions:

  1. Create a folder in your user directory called r-projects-scrapbook

  2. Create a subdirectory called _template and use a text editor to create a file named rstudio-project-template.Rproj containing:

Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

This is the secret sauce behind RStudio Projects. The .Rproj file is a plain text file containing instructions on how RStudio should behave when it opens the project.

Details of Automator workflow

Open Automator > Choose Application and add these actions in order:

  1. Ask for Text — ensure “require an answer” is selected; unnamed projects are bad.

  2. Set Value of Variable — this is the project_name used for the new folder and .Rproj file.

  3. New Folder — format the Name: field as Today's Date (formatted as 05-01-1999) + _ + project_name variable. Set Where to your projects location.

  4. Set Value of Variable — store the result as full_path for later use.

  5. Find Finder Items — search your-projects-home/_template, condition: Name is rstudio-project-template.Rproj.

  6. Filter Finder Items — same condition as above to discard the full_path directory from the results.

  7. Copy Finder Items — set Variable: to full_path.

  8. Rename Finder Items — set Name Single Item, Basename only to project_name.

  9. Open Finder Items — set Open with: to RStudio.app.