October 8, 2019
If you data changes, or you get new data, you have to change all tables in the paper
Your scientific results are based on data
If your data changes, your paper should also change
You need to use R in your document
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
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
Consecutive lines of text are one paragraph. They are separated by an empty line
The first paragraph. Another paragraph
The first paragraph.
Another paragraph
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
Programs are usually written in a monospaced font
``` this <- is.computer(code) ```
this <- is.computer(code)
This is only shown, but not executed
To do some real work on the document we add {r}
```{r} seq(from=2, to=10) rep("A", 3) ```
[1] 2 3 4 5 6 7 8 9 10
[1] "A" "A" "A"
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)
```{r print_sequences} seq(from=2, to=10) rep("A", 3) ```
Names cannot have
or .
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
eval=FALSE
do not execute the codeecho=FALSE
do not show the codecache=TRUE
execute only once and save the resultYou can also write them directly
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) ```
```{r} seq(from=4, by=2, length=4) ```
which produces
seq(from=4, by=2, length=4)
[1] 4 6 8 10
```{r} weight <- c(60, 72, 57, 90, 95, 72) ```
```{r} weight ```
produces
weight <- c(60, 72, 57, 90, 95, 72)
weight
[1] 60 72 57 90 95 72
```{r} weight[3] weight[-3] ```
produces
weight[3]
[1] 57
weight[-3]
[1] 60 72 90 95 72
There are several tutorial online
A nice on is: