New RStudio projects directly from Spotlight

By Charlie Joey Hadley | May 15, 2020

I’ve finally created an Automator application (download here) to create new RStudio Projects, and it works fairly nicely:

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

├── 15-05-2020_tidytuesday-rock-salt
│   └── tidytuesday-rock-salt.Rproj

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

If the concept of RStudio Projects is new to you, please watch my 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. This can be a bit messy if you re-open RStudio in a state where lots of objects need to be loaded.

Recreating this on your machine

This solution only works for macOS users. You can download a copy of my application onto your machine, it will just work provided you follow these instructions:

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

  • Create a subdirectory called _template and use a text editor to create a file named rstudio-project-template.Rproj that contains the following:

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, my example makes RStudio behave according your preferences.

Details of Automator workflow

Firstly, this workflow does not check if a duplicate project exists. The script does prepend project names with the date, in attempt to support folks who regularly create projects called foobar but it is not robust in any way.

If you want to recreate the workflow on your machine, follow these instructions:

Choose a location on your machine for your projects (your-projects-home), on my machine I chose charliejhadley/r-projects-scrapbook1. Remember you must follow the instruction above to nest the _template/rstudio-project-template.Rproj file into this directory.

Open Automator > Choose Application

Add these actions:

  1. Ask for Text
  • Ensure that “require an answer” is selected, unnamed projects are bad
  1. Set Value of Variable
  • This is the project_name we will use for creating the new folder and .Rproj file
  1. New Folder

The Name: field needs to formatted as follows:

  • Add the variable Today's Date

    • Right-click the variable and select Edit…
    • Set Format: Custom format…
    • Drag the blocks into the field in the following order:
      • Day of Month (shown as 5)
      • Month (shown as January)
      • Year (shown as 1999)
    • Type a - between each of the blocks
    • Right-click the 5 entry and select 05
    • Right-click the January and select 01
    • Check that the date format is identical do 05-01-1999
    • Click done
  • Type a _ after the Today's Date variable

  • Drag the project_name variable after the _

Set the Where field to your chosen location for projects

  1. Set Value of Variable
  • This full_path variable will be used to add the .Rproj file later
  1. Find Finder Items

In this application we’re creating an empty project, in others we might want to select multiple files from our _template directory.

This action will absorb the folder that we’ve just created, and the search results. For this reason we add the conditional.

  • Set the search path to your-projects-home/_template

  • Set the following condition:

    • All of the following are true
      • Name is rstudio-project-template.Rproj
  1. Filter Finder Items

Frustratingly, the previous action contains both the search results and the full_path directory. Add the same condition as before to discard the full_path directory:

  • Set the following condition:

    • All of the following are true
      • Name is rstudio-project-template.Rproj
  1. Copy Finder Items
  • Set the Variable: field as the full_path variable
  1. Rename Finder Items
  • Set the initial field as Name Single Item

  • Set Name: Basename only to project_name

  1. Open Finder Items
  • Set Open with: to

  1. It’s advisable to choose a location not in your iCloud as this add a further prompt every time you run the application.↩︎