Console pane

Start RStudio. You should see a Window similar to the Figure below.
The window has a pane (a part of the window) called ‘Console’.
The console provides a direct connection to the R language interpreter.

Prompt and result

After the prompt symbol > you can type any expression.
Press Enter to see the result (output) of the expression.

Type the following (or similar) examples:

> 4+5
[1] 9
> 2*8
[1] 16
> 2/8
[1] 0.25

A # marks the start of a comment. All text after it is simply ignored.
Type the following line including the a comment:

> 5^2  # 5 raised to the 2-nd power
[1] 25

Decimal separator: comma vs. dot

Fractional parts are always separated with . (dot), never with , (comma).

For example, type to express “one-half”:

> 0.5
[1] 0.5

But type the following to see the error message:

> 0,5
Error: <text>:1:2: unexpected ','
1: 0,
     ^

Order of calculation: parentheses

Use parentheses to be sure that the calculations are done in the right order.
Type the following to compare:

> 12/2*3
[1] 18
> 12/(2*3)
[1] 2

Type the following to see the error for brackets used instead of parentheses:

12/[2*3]
Error: <text>:1:4: unexpected '['
1: 12/[
       ^

Or, when you have an extra closing parenthesis:

12/(2*3))
Error: <text>:1:9: unexpected ')'
1: 12/(2*3))
            ^

While typing, RStudio may automatically insert a closing parenthesis.
If you don’t like this, change option Insert matching parens/quotes in menu ToolsGlobal Options...CodeEditing.

Multiline commands

When the prompt symbol changes to +, it means that the command in the single line is not finished yet.
This might be intentional or mistake:

A command in a single line might be incomplete because a parenthesis is open, but not closed. Try to type:

> 5*(1+1
+ )
[1] 10

It can be also incomplete because there is an arithmetic symbol at the end of the line:

> 1 +
+   2 +
+   3
[1] 6

Simple functions

Here are examples of several useful functions and how to use them. Try to type them in the console:

> sqrt( 10 )     # square root
[1] 3.162278
> log( 10 )      # natural logarithm
[1] 2.302585
> log10( 10 )    # logarithm base 10
[1] 1
> abs( -10 )     # absolute value
[1] 10

Terminology: the function (e.g. log) is applied to its argument(s) (e.g. 10).
The argument of a function is always between parentheses.

Type the following to observe what happens when you forget to provide the argument:

> sqrt()
Error in sqrt(): 0 arguments passed to 'sqrt' which requires 1

Note, that the spaces around the argument are not necessary.
In the course materials we use spaces to increase readability.

> sqrt(10)
[1] 3.162278

Getting help

To show the manual for a function type the function name after ? in the Console.
For example:

?sqrt

The documentation will be shown in the Help pane. Also there you may type the keyword to be searched.

Useful console keystrokes

Try the following keystrokes in the console:



Copyright © 2024 Biomedical Data Sciences (BDS) | LUMC