Skip to main content

Lab session 2

Markdown basics

  1. Create a new file with the .md file extension, e.g.
    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."

  1. Add a YAML header to the top of your file including a title, author name and today’s date.
    Convert your Markdown document into an HTML file without using RStudio’s “Knit” button. There are a multiple ways to do this:
    1. using the R console
    2. using a hotkey in RStudio.

R Markdown

  1. 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 your date field in the YAML header so that it automatically includes the time that your document was last compiled.

  2. 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 the difference between the rolled dice and gives TRUE if this is equal to zero (i.e. if the two dice are the same). Can you think of another way of defining is_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.

  3. By combining results = 'asis' and the cat 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 of roll, respectively.
    Use an if statement to print “I rolled a double” if is_double is TRUE.

  4. 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?

  5. 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 with results = 'asis'. In that chunk, convert Titanic to a tibble or a data.frame and generate a Markdown table of the data.

  6. Convert your entire report to a Word document (without opening Word or LibreOffice, except to view the final result).


  1. In RStudio, use Help > Markdown Quick Reference to remind yourself of Markdown syntax. See also this morning’s lecture slides.
  2. Try ?knitr::knit and package?rmarkdown in the console
    Try Shift + Alt + K in RStudio
  3. Try ?Sys.time. To make it look nicer, see ?format.POSIXct
  4. Try ?'[' to extract values from roll.
  5. Try something of the form if(temp > 20) cat('It is a warm day today')
  6. ?set.seed
  7. ?knitr::kable or ?xtable::xtable (the latter requires installing the xtable package).
  8. Output formats are controlled via the YAML header.

  1. Donald Knuth (1992)