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!
If the first line of your R script is
— Trevor is staying at home (@TrevorABranch) September 30, 2019
setwd("C:")
I will come into your office and SET YOUR COMPUTER ON FIRE 🔥.
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:
Create a folder in your user directory called
r-projects-scrapbookCreate a subdirectory called
_templateand use a text editor to create a file namedrstudio-project-template.Rprojcontaining:
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:
Ask for Text — ensure “require an answer” is selected; unnamed projects are bad.
Set Value of Variable — this is the
project_nameused for the new folder and.Rprojfile.New Folder — format the
Name:field asToday's Date(formatted as05-01-1999) +_+project_namevariable. SetWhereto your projects location.Set Value of Variable — store the result as
full_pathfor later use.Find Finder Items — search
your-projects-home/_template, condition: Name isrstudio-project-template.Rproj.Filter Finder Items — same condition as above to discard the
full_pathdirectory from the results.Copy Finder Items — set
Variable:tofull_path.Rename Finder Items — set Name Single Item, Basename only to
project_name.Open Finder Items — set
Open with:to RStudio.app.