October 8, 2019

How do we write a paper?

Let’s look at a typical paper

It has tables that depend on data

If you data changes, or you get new data, you have to change all tables in the paper

And figures that depend on data

Maybe we need to show a diagram

or a map

or a map with colors

or a map in perspective

Your paper depend on your data

Your scientific results are based on data

If your data changes, your paper should also change

You need to use R in your document

Combining R and Markdown


An alternative to ordinary Word Processors is to use text files with a few rules to mark the role of each element.

Text files can be read with any computer, and will be accessible for ever.

Today the Structured Text format most often used is Markdown


In Rstudio we can combine text written in Markdown and the results of commands in R

You only need to install the rmarkdown package

Please be sure to write the name with an r

Global document options

At the beginning of the file

At the top of our file we have a block that start and ends with ---

These are variables that affect the complete document, like

  • title:
  • author:
  • date:
  • number: (for the student number)
  • output


title: "Exam CMB1 2019"
author: "Student's name"
number: 0405678912
date: "8 October 2019"
output: html_document

Notice that the block is wrapped by --- (three hyphens)

Always write space after :

Always start at the beginning of the line

Always write your student number

Reminder: Paragraphs

Consecutive lines of text are one paragraph. They are separated by an empty line

The first paragraph.

Another paragraph

The first paragraph.

Another paragraph

Block quotations

A block quotation contains text that you want to highlight or point up.
Each line is preceded by a > character and a space.

> This is something important, like your answer to the exam
> Another paragraph

This is something important, like your answer to the exam

Another paragraph

Computer code

Programs are usually written in a monospaced font

this <- is.computer(code)
this <- is.computer(code)

This is only shown, but not executed

R code

To do some real work on the document we add {r}

seq(from=2, to=10)
rep("A", 3)
[1]  2  3  4  5  6  7  8  9 10
[1] "A" "A" "A"

Code Chunks

Chunks are pieces of R code inside an Rmd document

A chunk starts with ```{r} and ends with ```

(each one in a separate line, without spaces)

Chunks can be executed one by one

Chunks can have names

This is useful when your document is long and there is an error
```{r print_sequences}
seq(from=2, to=10)
rep("A", 3)

Names cannot have   or .

Chunk have options

In the RStudio editor, each chunk is marked in grey color

In the top right corner there are three icons

The first icon opens the chunk options

Some useful options

  • eval=FALSE do not execute the code
  • echo=FALSE do not show the code
  • cache=TRUE execute only once and save the result

You can also write them directly

Default options

The command knitr::opts_chunk$set() changes the default chunk options

We see it at the beginning of the document

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)


Last class I wrote:
seq(from=4, by=2, length=4)

which produces

seq(from=4, by=2, length=4)
[1]  4  6  8 10

Variables are shared between chunks

weight <- c(60, 72, 57, 90, 95, 72)


weight <- c(60, 72, 57, 90, 95, 72)
[1] 60 72 57 90 95 72

Chunks can have several outputs



[1] 57
[1] 60 72 90 95 72

More information

How do we write a presentation?