1.2 RStudio projects
RStudio projects (.Rproj) are a handy solution to keep all the files related to a specific workflow, including
- R scripts (
.R), - workspace (
.RData), - history (
.Rhistory), - datasets,
- etc.,
together in one working directory. RStudio projects, once opened, are automatically associated with the directory the .Rproj file is located in, thus rendering unnecessary the use of setwd().
Starting a new project
From inside RStudio, projects are easily initialized and associated with either
- a brand new working directory,
- an existing directory or
- a local copy of a version control repository (of which more later on)
by hitting the ‘New Project…’ button from the drop-down menu at the top-right corner.
Working with projects
Once created, RStudio automatically switches to the new project (not without asking if you’d like to safe your latest changes, of course). If you desire to work with an already existing project instead, you may simply open the referring .Rproj file either via the drop-down menu in the top-right corner, or just double-click it within your system’s file explorer. Note that, among others, the following actions are taken when a project opens inside RStudio (taken directly from the RStudio Support Team:
- a new R session is started;
- the
.Rprofilein the project’s main directory (if any) is sourced; - the recent workspace stored in
.RDatain the project’s main directory is loaded (if indicated by the project options); - the
.Rhistoryfile in the project’s main directory is loaded into the RStudio ‘History’ pane and available for console up/down arrow command history; - previously edited source documents are restored into editor tabs.
Similarly, when you quit a project,
- the
.RDataand.Rhistoryare being saved (if indicated by the project options); - the currently active R scripts are being saved;
- the current R session is terminated.
Task: Create your own RStudio project
Now it’s your turn: Initialize a new RStudio project in the advanced-programming-in-r subfolder from Section 1.1, i.e. the one with Git version control enabled! For that purpose,
- choose an existing directory rather than a new one and
- navigate to the
advanced-programming-in-rsubfolder that you have copied from GitHub earlier usinggit clone.
After having accomplished this,
git addall the stuff that has accumulated in the local project directory so far (mostly.Rprojrelated files)- followed by
git commit(including a meaningful description of your recent actions) - and, finally,
git push.
It is totally up to you whether to use the built-in RStudio interface to be found in the top-right ‘Git’ pane or run git from the command line. Due to its ease of use, I highly encourage you to choose option #1, though.
To verify that everything worked as expected, head over to the web-based representation of your code repository on GitHub. In addition, don’t forget that you may use git status at any stage to get further information on the status quo of the Git processing chain. There’s also a comprehensive tutorial on version control with Git (and SVN) on the RStudio support website if you wish to gather further insights into the topic.