Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Initial Instructions and RMarkdown Basics

Initial Instructions
1. Download and decompress the “R_with_tidyverse_CRAG_course_students.zip” file into any directory
you like.
2. Launch Rstudio
3. Go to View > Panes > Pane Layout.
The pane layout in Rstudio is a question of personal preferences. However, in this course we will use a
common layout to simplify explanations (see Figure 1):
• Top left pane: “Source”
• Bottom left pane: “Console”
• Top right pane: “Environment, etc”. Make sure the “Environment” and “History” checkboxes are
selected.
• Bottom right pane: “Files, Plots, etc”. Make sure the “Files”, “Plots”, “Packages”, “Help”
checkboxes are selected.
4. Right-click on the small arrow behind the cogwheel (row of icons, top left panel). Select “Chunk Output
in Console”.
5. Go to File > Create Project > Existing Directory and then browse to select the folder you have just
decompressed.
6. Click “Create Project. This creates a Course_files.Rproj file as well as two hidden files (.Rhistory and
.Rproj.user) which will store any objects created during your current work session, as well as Rstudio
screen layout, selected options, etc. Next time you resume working in this project, all that data will be
loaded.
7. Go to File > New File > RMarkdown > Document (alternatively, you could choose File > New File >
R Notebook)
8. Fill the “Title” (R Course, for example) field, and choose “HTML” (or PDF) as default output format.
Then, click OK. A new R Markdown document will appear on the top left pane. This is where you will
be writing your code and comments from now on.
9. Delete all lines from ‘## R Markdown’ on (this is just a basic explanation on Rmarkdown documents).

RMarkdown Basics
• Note: Be aware that a next-generation version of R Markdown exists: Quarto. It includes dozens
of new features and capabilities while at the same time being able to render most existing Rmd files
without modifications (https://quarto.org/docs/get-started/hello/rstudio.html).
• Whole document can be compiled (to html or pdf) by clicking on the Knit button (row of icons, top
left panel).
• New line: empty line
• *italic*: italic

1
• **bold**: bold
• Headers:
# Header 1
## Header 2
### Header 3
• Unordered list: *, +, or - creates a bullet point. You can also add sub-levels, to create sub-lists by
indenting the next list item evenly by two or four spaces. Typing
* Item 1
* Item 2
* Item 2a

* Item 2a1
will produce
• Item 1
• Item 2
– Item 2a
∗ Item 2a1
• Ordered list:
1. Item 1
2. Item 2
3. Item 3
+ Item 3a
+ Item 3b

Mathematical formulas, non-latin alphabets, etc


Lateχ code can be used to include mathematical formulas, non-latin alphabets and hundres of other symbols.

α, β, γ, Γ, π, Π, ϕ, φ, µ, Φ
∼ ∈,
=, / ⊠, ≈, ≤, ♭, ♯, ♣
 
n! n
=
k!(n − k)! k
In fact, with some knowledge of Lateχ (which can be achieved pretty quickly), you can produced publication
quality documents comprising complex tables and figures, bibliographic chapters, hyperlinked text, etc. A
complete doctoral thesis or a research paper can be produced using a combination of RMarkdown and Lateχ
commands. Figures will be produced as the R code chunks (see below) are being executed, for example to try
new parameters or changing the appearance of plots. However, basics on Lateχ coding is outside the scope of
this course.

2
R Code Chunks
R code will be evaluated and printed
```{r, wrapper=TRUE}
1 + 1

## [1] 2
2*3

## [1] 6
```
You can add comments inside code chunks:
```{r, results='hide'}
2ˆ3 #We are computing two to the power of three.
#This is just a comment and it will not be treated as code.

```
A single code chunk can be evaluated by clicking on the green arrow facing right (chunk header).

A single line of code inside a chunk can be executed by:


• Placing the cursor anywhere on that line and pressing ctrl + enter: Runs the current line and jumps
to the next one.
• Selecting the line and pressing ctrl + enter: Runs the selected part without jumping further.
• Using the ‘Run’ button (row of icons, top left panel).

Chunk output can be customized with knitr options, arguments set in the {} of a chunk header. Some
commonly used options:
• include = FALSE prevents code and results from appearing in the finished file. R Markdown still runs
the code in the chunk, and the results can be used by other chunks.
• echo = FALSE prevents code, but not the results from appearing in the finished file. This is a useful
way to embed figures.
• message = FALSE prevents messages that are generated by code from appearing in the finished file.
• warning = FALSE prevents warnings that are generated by code from appearing in the finished file.
• fig.cap = "..." adds a caption to graphical results.

(The ‘wrapper=TRUE’ used in a previous code chunk forces the chunk header to appear in the finished file
and is used here just to display how code chunks are used.)
An example of embedding external images in the output document:

```{r, out.width = "400px", fig.cap="Rstudio screen layout", fig.align="center"}


knitr::include_graphics("layout.png")

```

3
Figure 1: Rstudio screen layout
4
(though, just to display the figure, but not the code, we’d use instead the following header: r, echo=FALSE,
out.width = "300px", fig.cap="Rstudio screen layout", fig.align="center").

Similarly, we can also embed R-generated plots:


```{r, out.width = "300px", fig.cap="A very basic R plot", fig.align="center"}
plot(cars)

100 120
80
dist

60
40
20
0

5 10 15 20 25

speed

Figure 2: A very basic R plot

```

Finally, rmarkdown documents can become really large, comprising a lot of sections and chunks, and many
lines of code. To access a particular part of the document, the index found at the bottom of the rmarkdown
pane is a much better method than scrolling up and down the document. Also, the Find/Replace button
(row of icons, top left pane) can be used as you would in any Word document.

You might also like