# This is an R comment: nothing is gonna happen
ECON 490: Working with R Scripts (2)
Prerequisites
- Connect to a Jupyter Lab session using R.
Learning Outcomes
- Be able to effectively use R script files.
2.1 Opening R Scripts from R Interface
If you choose to work with R, it is very advisable to install RStudio here. RStudio provides a nice interface to work with R code. Whenever we work with R or any other programming language, it is very important that we make our code replicable. For instance, we may be working on a graph and realize that it is not looking the way we intended it to look. Without a list of the commands we previously used, it may take a long time to re-do said graph with the proper corrections.
R provides a way to save code notebooks, also known as R-scripts, where we can keep all the code we ran in a particular instance. To open the Do-file Editor from our R session, we use the shortcut Ctrl(Command)+9
or click at this part of the R Interface
You should now observe a new window in your computer that looks like this:
To run a highlighted part of code, you can use Ctrl(Command)+Enter
.
Note: The Jupyter cells in the following lectures will work as an R script proxy, but whenever you run R non-interactively it is very important to keep track of any changes you make in an R script.
2.2 Writing Comments in our Code
Writing comments for different parts of our code is a very good practice. It allows us to revisit code we wrote in the past and understand what we did. R allows for three different types of comments in our do-files.
- The first type of comment uses the hashtag
#
like below.
Notice that we can put it next to any line of code and it will still recognize such part as a comment.
5 # This command is printing the number 5
- Unline other programs such as Stata, R does not allow multi-line comments. We need to put a hashtag at the beginning of each comment line.
#Multi-line comments only work...
#this way!
Notice that comments are highlighted with the colour green within our R scripts. Whenever we see this color, we should automatically recognize that part of the cell as being a comment made by the author. Similarly, we can see that some parts of a cell are highlighted blue. Whenever we see this colour, we should recognize that part of the cell as being a function or command.
2.3 Delimiters
R works with functions, and the ending of a particular function is what determines the delimitation to the next command instruction. For instance,
#Single line
print(5)
can work identically as
#Multiple line
print(
5
)
2.4 Clearing the R Session
Whenever we begin working with a new R session, it is advisable to clean the memory for any pre-existing objects. In Stata, this is done by the clear
command. In R, we need to provide the list of objects to be removed from memory. The best way to do this is to list everything that exists in memory:
rm(list=ls())
You should include this line at the beginning of any new R script you create.
2.5 Wrap Up
When producing a research project, organization and attention to detail are critical skills to have. That is why you should always save your R scripts in an easy-to-reach folder as soon as you begin your work by clicking the save icon on the top right. It is good practice to also save your R script each and every time you run the file. Getting in the habit of doing this will save you many hours of redoing accidentally lost work when your file closes abruptly.
Note: You can always show your R scripts to either your TA or instructor for feedback. With any practical assignments you complete in R more generally, it is good to submit corresponding R scripts alongside them.