Lab session 2
R Markdown Practical
David A. Selby
27 September 2016
Markdown basics
- Create a new file with the
.mdfile 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,authorname 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 yourdatefield 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) == 0The variable
is_doublechecks thedifference between therolled dice and givesTRUEif this is equal to zero (i.e. if the two dice are the same). Can you think of another way of definingis_doublethat 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 thecatcommand 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
irisdataset 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 anifstatement to print “I rolled a double” ifis_doubleisTRUE. -
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, convertTitanicto atibbleor adata.frameand 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::knitandpackage?rmarkdownin the console
TryShift + Alt + Kin 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::kableor?xtable::xtable(the latter requires installing thextablepackage).- Output formats are controlled via the YAML header.
-
Donald Knuth (1992)↩