Stylistic Conventions
R code that is evaluated
When R expressions are typed at the R console prompt >
and
evaluated they appear like this:
> x <- matrix(1:6, nrow = 2, ncol = 3,
+ byrow = TRUE) # This is a comment
> ## This is also a comment
> x
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
The >
symbol is the command prompt. Notice that when
R evaluates an expression that continues onto the next line(s), a
+
sign appears to the left. This means that this line is a
continuation of the previous line, and the R expression is not
complete. The output appears without the prompt symbol.
In this book however, the above expressions will appear like this:
#> [,1] [,2] [,3]
#> [1,] 1 2 3
#> [2,] 4 5 6
The R prompt (>
) and continuation (+
) symbols have been removed,
and output is preceded by these two characters (#>
). Again, R
output is preceded by the (#>
) characters. Actual comments will be
preceded by one or more comment (#
) characters without >
.
These stylistic conventions (#>
characters preceding the output
lines) is for convenience only. This enables readers to copy and test
code from PDF or ebook versions of this book. Removing extraneous
symbols also improves readability of R expressions.
R code in a script file (not evaluated)
The same R code above can also be saved as a R script for later
evaluation. A R script is a collection of R expressions and saved as
an ASCII text file with a .R
extension. The R script can be edited
using a text editor or RStudio. R script snippets are displayed
without evaluation like this:
If the R script is evaluated it will appears as in above examples.