Lab session 2
R Markdown Practical
David A. Selby
27 September 2016
Markdown basics
- Create a new file with the
.md
file extension, e.g.hello.md
.
Open this file in RStudio or in a plain text editor, such as Notepad.
See if you can recreate the following passage in Markdown syntax.
“I am writing in Markdown. It is intended to be
- easy to read and
- easy to write.
Literate programming1 emphasises the idea that source code
should be human-readable and computer-executable."
- Add a YAML header to the top of your file including a
title
,author
name and today’sdate
.
Convert your Markdown document into an HTML file without using RStudio’s “Knit” button. There are a multiple ways to do this:- using the R console
- using a hotkey in RStudio.
R Markdown
-
Rename or copy your file so it has the extension
.Rmd
, e.g.hello.Rmd
. (Or, in RStudio, use File > New File > R Markdown…) Modify yourdate
field in the YAML header so that it automatically includes the time that your document was last compiled. -
In the board game Monopoly, players roll a pair of dice each turn. A player must ‘go to jail’ if they roll too many doubles in a row. Add an R chunk with the following code, which simulates rolling two dice.
roll <- sample(1:6, size = 2, replace = TRUE) is_double <- diff(roll) == 0
The variable
is_double
checks thediff
erence between theroll
ed dice and givesTRUE
if this is equal to zero (i.e. if the two dice are the same). Can you think of another way of definingis_double
that gives the same result?
Add brackets()
around each of the two lines of code above so that they print their results when called. -
By combining
results = 'asis'
and thecat
command we can generate text dynamically from R code. For example,```{r results = 'asis'} cat("The **maximum** length of petal in Fisher's `iris` dataset is", max(iris$Petal.Length)) ```
will generate the output: “The maximum length of petal in Fisher’s
iris
dataset is 6.9”
Create a new chunk and see if you can automatically generate a sentence of the form: “My first die was a x. My second die was a y”, where x and y are the first and second elements ofroll
, respectively.
Use anif
statement to print “I rolled a double” ifis_double
isTRUE
. -
Notice that every time you compile your document, the dice rolls change. Add
set.seed(n)
(where n is any integer you like) to the start of the chunk from question 4. Re-compile your document several times. What do you notice? -
There is a built-in dataset in R called
Titanic
, which records the survival frequencies of crew and passengers aboard the RMS Titanic in 1912. Create another chunk withresults = 'asis'
. In that chunk, convertTitanic
to atibble
or adata.frame
and generate a Markdown table of the data. -
Convert your entire report to a Word document (without opening Word or LibreOffice, except to view the final result).
Tips
- In RStudio, use Help > Markdown Quick Reference to remind yourself of Markdown syntax. See also this morning’s lecture slides.
- Try
?knitr::knit
andpackage?rmarkdown
in the console
TryShift + Alt + K
in RStudio - Try
?Sys.time
. To make it look nicer, see?format.POSIXct
- Try
?'['
to extract values fromroll
. - Try something of the form
if(temp > 20) cat('It is a warm day today')
?set.seed
?knitr::kable
or?xtable::xtable
(the latter requires installing thextable
package).- Output formats are controlled via the YAML header.
-
Donald Knuth (1992)↩