1.4 Using RStudio effectively
In this section you will learn about how to use RStudio to save and manage your R code. In Chapter 3 we will revisit RStudio and go into greater depth.
- files and working directories
- projects
- what to do if you can’t run RStudio?
1.4.1 Files and working directories
Very quickly you will want to be able to save your R code rather than have type or copy and paste it into the Console Pane every time you wish to execute it. To do this you should either create a new file from within RStudio File > New File > R Script or open an existing file. These will be of the form: filename.R
. You will need to think where is convenient to store your files and suggest you set up, one or more, purpose created directories. There is a menu option within RStudio to Session > Set Working Directory for convenience.
1.4.2 Using projects in RStudio
Although it may sound repetitious, I do urge you to be systematic in how you organise your files. It’s all too easy to slide into chaos once you have multiple versions of a file and several weeks’ worth of exercises and solutions.
One effective solution is to use RStudio to create projects (one for each discrete chunk of work e.g., Week 1 Seminar, Your coursework, etc). In RStudio choose File > New Project. Your choices are to
- make a brand new directory (simple)
- use an existing directory where you already have R code and data
- by cloning a Git repository21
Projects make it easy to divide your work into multiple contexts, each with their own working directory, workspace, history, and source documents. You can launch the context simply by invoking the <filename>.Rproj
file.
For more details see the online RStudio (now rebadged as Posit) Using projects and also the RStudio cheatsheet. For an exceedingly detailed guide see Campbell (2020).
1.4.3 What if you can’t run RStudio?
There are two potential short term fixes
- There are various online R execution environments so if you can access the web, even via smartphone, one possibility is to use a website such as https://www.tutorialspoint.com/execute_r_online.php However, be aware this only supports Base R so it won’t be possible to do anything sophisticated. Also you just have an R interface so none of the facilities of RStudio.
- Very recently RStudio have now started to offer Studio Cloud. You have to be prepared to create an account with them, but it is a promising option and in this case it offers many of the facilities of RStudio. There is a free option. Below is an example screen shot.