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:
/Users/charliejhadley/r-projects-scrapbook
├── 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!
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 🔥.
Classic rant by @JennyBryan https://t.co/5cwCytqC0N
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.
I've documented my workflow for opening @RStudio projects (using on @alfredapp) in this short video: https://t.co/XvmRyGSsol #rstats
— Hadley Wickham (@hadleywickham) February 27, 2018
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 namedrstudio-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-scrapbook
1. 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:
- Ask for Text
- Ensure that “require an answer” is selected, unnamed projects are bad
- Set Value of Variable
- This is the
project_name
we will use for creating the new folder and .Rproj file
- 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 theToday's Date
variableDrag the
project_name
variable after the_
Set the Where
field to your chosen location for projects
- Set Value of Variable
- This
full_path
variable will be used to add the.Rproj
file later
- 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
- Name is
- All of the following are true
- 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
- Name is
- All of the following are true
- Copy Finder Items
- Set the
Variable:
field as thefull_path
variable
- Rename Finder Items
Set the initial field as Name Single Item
Set
Name:
Basename only toproject_name
- Open Finder Items
- Set
Open with:
to RStudio.app
It’s advisable to choose a location not in your iCloud as this add a further prompt every time you run the application.↩︎