---
title: 'Practical 2 solutions'
author: "David A. Selby"
date: "`r Sys.time()`"
output:
    html_document:
        number_sections: true
---

# Markdown syntax

I am writing in Markdown. It is intended to be

- easy to read and
- easy to write.

Literate programming^[Donald Knuth (1992)] *emphasises* the idea that source `code` should be human-readable **and** computer-executable.

# Converting to HTML

You can generate the document using

```r
library(rmarkdown)
render('hello.md')
```

or

```r
library(knitr)
knit('hello.md')
```

or using **Control + Shift + K** in RStudio (**Command + Shift + K** on Mac).

# Automatic updates

Add in-line R code to the YAML header including the command `Sys.time()`. (*See the header of this file.*)

# Code chunks

```{r dice_roll}
(roll <- sample(1:6, size = 2, replace = TRUE))
(is_double <- diff(roll) == 0)
is_double2 <- roll[1] == roll[2]
```

# Dynamic reporting

```{r results = 'asis'}
cat('My first die was a ', roll[1], '. My second die was a ', roll[2], '.', sep = '')
if (is_double) cat('I rolled a double.')
```

# Random seeds

Rolling the dice three times without setting the seed gives the following, with different values each roll:

```{r}
# Re-run chunk three times
```{r dice_roll, echo = FALSE}
```{r dice_roll, echo = FALSE}
```{r dice_roll, echo = FALSE}
```

Whereas this will give the same result every time (I have omitted the `is_double` line to save space):

```{r dice_roll2}
set.seed(1)
(roll <- sample(1:6, size = 2, replace = TRUE))
```

```{r}
# Re-run chunk three times
```{r dice_roll2, echo = FALSE}
```{r dice_roll2, echo = FALSE}
```{r dice_roll2, echo = FALSE}
```

# Generating tables

```{r results = 'asis', message = FALSE}
Titanic_df <- as.data.frame(Titanic)
knitr::kable(Titanic_df)
```

# Output to word

Simply put `output: word_document` in the YAML header instead of what is there now. Then re-compile.

