2 Getting started

2.1 Creating and knitting an R markdown file

An R markdown file is a plain text file. You can create a new text file, give it the “.Rmd” extension, and start typing. But it is better to use RStudio.

Go to the “File” menu bar, then “New File”, then “R Markdown…”. The first time you do this, you may be asked to install some extra packages. Now you will have a pop-up window asking for the Title, author and output format (Fig. 2.1).

Choose the output format

Figure 2.1: Choose the output format

You can fill these in now, or edit the document later. I recommend you start with “HTML” format as is doesn’t require anything else to be installed. When you are ready, click OK, and a new R markdown file will open. It will look something like Figure 2.2

The default R markdown document.

Figure 2.2: The default R markdown document.

You will learn about the elements of this file in section 3.

Exercise

Create an R Markdown document with output format HTML.

2.2 Knitting the R markdown file

You can see the rendered R markdown document by knitting it. Click the blue “Knit” button which is above the document.

The knit button

Figure 2.3: The knit button

The first time you knit the document, you need to save it.

Exercise

Render your R Markdown document and examine the output. If it worked then everything is set up correctly.

Exercise

For the rest of this tutorial, we will analyse plant trait data from Svalbard and make a reproducible report.

To download the data and R Markdown document, you need to run

#install.packages("usethis") # if you don't have it already.
usethis::use_course("biostats-r/svalbardQuartoDemo")

Then follow the instructions. This will open the svalbardQuartoDemo Rstudio project. Open the file svalbard_traits.Rmd and render it. The output is a bit of a mess - we are going to make it better!