Tips From The Blog XIX: initialising a new RStudio project

I like to set up a standardised directory structure for RStudio projects. The idea came from here. In brief, the structure is:

Data/
Output/Data/
Output/Plots/
Script/

My typical workflow is therefore to:

So far, so good. However, this process is a bit tedious.

I find it irritating to open up RStudio (which opens up a previous project by default) and then go through these steps. Recently, I had to make several R projects for a paper and I thought there must be a quicker way. Here’s my solution.

The quick way: shell function

I made a shell function to set up an R Project with the correct directory tree. The resulting .Rproj file can just be double-clicked and you are good to go!

I previously wrote about shell aliases and functions. I am using zsh on macOS, but this should all work on bash and possibly shells.

As described before, I had to initialise a file for zsh functions.

In the home directory (~), open .zshrc with your favourite editor e.g.

nano .zshrc

Then add the following to the file:

# Alias definitions
if [ -f ~/.zsh_functions ]; then
    . ~/.zsh_functions
fi

Now make and edit zsh_functions

nano .zsh_functions

Now add the function:

function setup_rproj() {
    if [[ $# -eq 0 ]] ; then
        echo 'Name of new directory required'
        return 0
    fi

    # setup preferred directory structure
    mkdir "$1"
    mkdir "$1"/Data;\
    mkdir "$1"/Output;\
    mkdir "$1"/Output/Data;\
    mkdir "$1"/Output/Plots;\
    mkdir "$1"/Script

    /bin/cat <<EOM >"$1"/"$1".Rproj
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes 
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX
EOM

    # end of function here
}

And save by ctrl + x and y.

How it works

The idea is that the user navigates to where they want to make the R Project, i.e. cd to the location. Then call the function like this:

setup_rproj analysis123

The script will make a folder in the current directory called analysis123, in there it will place the directory structure described above, and then make the .Rproj file.

If you use a different .Rproj file the function needs to be edited accordingly. Finally, if the user calls setup_rproj without specifying a project name, the function will return an error message.

If the directory already exists, nothing will be overwritten.

Part of an occasional series of tech tips.