2.2 Let's get started!

In the first place, R and RStudio should be installed in your machine. To learn how to install R and RStudio in your computer, please read carefully Appendix A.1.

2.2.1 Running a line of code

There are several ways to run a line of code:

  • Highlight the line of code and click on the button Run (\(\Rightarrow\) Run) displayed on the upper right side of the code editor.

  • Highlight the line of code that you want to run and press Ctrl + Enter (Windows) or Cmd + Enter (MacOS).

  • Place the cursor on the line of code and click on the button Run (\(\Rightarrow\) Run).

  • Press Ctrl + Shift + S (Windows) or Cmd + Shift + S (MacOS) to run the code of an entire script.

  • Type a line of code directly into the console or copy a line of code from the code editor and paste it into the console after the prompt (>). Then, press Enter on the console.

2.2.2 Adding comments on R scripts

It is useful to add comments before chunks of R code to organize the session and to inform others that might be interested in reproducing our session and evaluating our outputs. The hash mark (#) is used to make comments by placing the symbol at the beginning of a line of code. R won't execute the line of code that is preceded by one or more hash mark symbols.


# This is a comment
# Raise 5 to the power of 2

5 ^ 2
## [1] 25

sqrt(25)  # sqrt(x) returns the square root of x
## [1] 5