5 Executing code

  • R is case sensitive: ‘a’ and ‘A’ are two different things.
  • For naming variables, it’s preferable to use only alphanumeric characters without accents. I.e., [A:Z,a:z,0:9,’_’].
  • Variable names should start with a letter.
  • Commands are separated by ‘;’ or a new line
  • The default prompt in the console is a >. If a command is incomplete at the end of a line, R will change the > for a + (by default).
  • Vertical arrow keys can be used to scroll back and forth the command history and recall (and edit) previous issued commands.
  • The [tab] key can be used to autocomplete commands, name of variables, etc.
  • Autocomplete also works other characters such as parentheses, curly braces, etc.
  • control+a takes the cursor to the beginning of a line.
  • control+e takes the cursor to the end of a line.

5.1 Running code from the console

One can type in commands directly on the console, for example, type each of these lines:

Running code from the console

Figure 5.1: Running code from the console

  • <- assigns the instructions on its right to the variable on its left. Most of the times <- is interchangeable with =.
  • a:b creates a series of numbers from a to b.
  • rnorm(n) returns n random numbers normally distributed.
  • sin(n) returns the sine of the argument. The output has the same length of the argument. E.g., the output of our example is a list of 50 numbers.
  • pi returns the value of the constant \(\pi\)

5.2 Running code from a document

For simple computations, using the console is convenient. But, when running an analysis the number of lines may grow rapidly yielding this method rather cumbersome. For those instances, it’s better to create a new document (Menu ‘File’ > ‘New Document’ or
command+n) and execute commands from there.

5.2.1 Inserting comments

Comments are an important piece of documentation. Either for others to understand your script, or for future you who could forget what you were doing back in the day…

Comments are created by preceding them with a # anywhere in the text.

## [1] "subject_1" "subject_2" "subject_3" "subject_4" "subject_5"
  • paste0() concatenate strings with no separation between them (here, numbers are casted into characters).
  • To comment out part of a script, select it with the mouse and press control+

5.2.2 Document structure

  1. Start a document with a comment about the contents and purpose of the script, the author and creation date.
  2. Load all the libraries you will need
  3. Functions you create
  4. Any other piece of code that does not change and you may need (defining constants, options, etc.)
  5. The actual script code

Steps 1–4 may be run only once when you start an R session.

5.2.3 Partial vs. total execution of a script

To execute the full content of a file named myScript, one can issue the command

source('myScript')

But, more frequently, we’re interested in running parts of the script. In that case, one should select with the mouse the desired lines of the script and press the keys command+return.

  • Be careful when re-running parts of a script as the contents of variables may have changed.
  • Always read the output in the console and look for error and warning messages.
  • Remember: Just because R outputs a result doesn’t mean that it is correct. Be always critical.