12 March, 2018

Markdown basics

Setting Up (I)

Step 1: Lets create an R Markdown HTML document

Setting Up (II)

Step 2: Choose HTML (we can change this to PDF or Word later)

Clear the verbiage

Please delete everything below the header…

---
title: "Introduction to RMarkdown"
author: "Dylan Z Childs"
date: "12 March, 2018"
output: html_document
---

…and then fill in the header!

(The header is an example 'YAML' by the way)

Getting Started with text formatting

So what is 'Markdown'. Markdown is a lightweight, readable mark up language to format documents. It can be converted into HTML, Microsoft Word, PDF, ODT, RTF…

Here are a few examples of basic markdown mark up:

  • specify headers with the # symbols
    • e.g. # My great header
  • italicize text single * or _
    • e.g. *word* or _word_
  • bold text with double * or _
    • e.g. **word** or __word__

Let's dive in

Add some heading practice your markdown script.

This is a level 2 heading

This is a level 3 heading



These can be made by using:

  • ## This is a level 2 heading
  • ### This is a level 3 heading

Knit to HTML

So, you've added some Markdown, but how do you turn it into output? If you look up into the top of the script, you will see Knit HTML:

Click it and see what happens…

Bold and Italics

Type this sentence, making the words bold and italicized:

I am a very good user of R and RStudio now.

This was done with:

I am a _very_ good user of **R** and __RStudio__ now.

Embedding links

Highlighting CODE

In R Markdown, using `some words` (e.g. in back ticks) places a grey box around the text and uses a slightly different font. This is very handy.

Can you add a sentence that looks like this?

  • This is how you make a sequence of numbers: x <- 1:50.

Make sure it doesn't look like this:

  • This is how you make a sequence of numbers: x <- 1:50.

Making lists

Making lists involves using either - or __*__ or 1. to mark each element of the list. There must also be a blank line between the preceding paragraph and the start of the list.

Can you figure out how to do this?

  • I Love R
  • I Hate R
  • I Love R

And This?

  1. I Love R
  2. I Hate R
  3. I Love R

Making lists (continued)

Unordered and ordered lists…

- I Love R
- I Love R
- I Love R
1. I Love R
2. I Love R
3. I Love R

(YES there has to be a SPACE in between the '-' and the words)

Making tables (the manual way)

Tables start with pipes: |

They have the column names in between pipes. Here is an example with two columns

|R Code|Unix Code|

Then, we can make a bold(ish) line

|:---:|:---:|

and follow this with detail that goes into the table.

|getwd()|pwd|

|setwd()|cd|

Making tables

See if you can produce this…

R Code- Unix Code
getwd() pwd
setwd() cd
ls() ls
head() head
tail() tail
grep() grep

Like this!

|R Code |Unix Code|
|:-----:|:-------:|
|getwd()|pwd      |
|setwd()|cd       |
|ls()   |ls       |
|head() |head     |
|tail() |tail     |
|grep() |grep     |

This method is used to define a table manually. We can also display data frames as tables. We'll see how to do that later. First we have to know how to insert R code into a document.

Working with R chunks

What is an 'R chunk'?

We can include bits of R code in an R Markdown document. These 'chunks' are run in the order they appear, allowing us to make figures, report statistical results, etc.

```{r}
x <- rnorm(12)
x
```

This will produce the following in a document:

x <- rnorm(12)
x
##  [1] -1.4639487 -1.1643933 -0.5263443 -1.0349474 -0.6429377 -0.6903048
##  [7] -0.7678070 -0.8970685  1.1736446 -0.1425114 -0.1311938 -1.9121733

Designating R code

To embed R code, we have to place it between a special sequence of characters.

These involve sets of three of 'back ticks' (that start and finish the code 'chunk') and some curly braces…

```{r}

```


TIP: Don't type the chunk syntax manually! Use 'Ctrl + Alt + I' to (I)nsert a code chunk.

Try it

Add this detail to your document:

```{r}
x <- 1:10
y <- x^2
x + y
```

This should produce the following when you knit your document:

x <- 1:10
y <- x^2
x + y
##  [1]   2   6  12  20  30  42  56  72  90 110

What's actually happening when we 'knit'?

  1. The knitr package first evaluates all the R code in the R Markdown file ('Rmd') and embeds the resulting code and output in a standard Markdown file ('md').

  2. The pandoc file converter then takes the resulting markdown file and converts it to HTML (or some other file format if you chose to use).

You don't really need to know this…

Controlling evaluation

There are times when we may want to display code but not the output — for example, to provide guidance, but not the answer in teaching. Sometimes we just want the answers but not the code — for example, to embed graphs or results of calculations.

  • An R chunk can be made to either show or not show the code
  • An R chunk can be made to either evaluate or not evaluate the code

These behaviours are are controlled by echo (show) and eval (evaluate), respectively.

code SEEN but NOT EVALUATED

Try and make this happen (set {r, eval = ????} in your chunk)

x <- 1:10
y <- x^2
x + y

Answer

```{r, eval = FALSE}
x <- 1:10
y <- x^2
x + y
```

code NOT SEEN but EVALUATED

Try to run the last calculation but not show the R code to make this happen (set echo = ???)

##  [1]   2   6  12  20  30  42  56  72  90 110

Answer

```{r, echo = FALSE}
x <- 1:10
y <- x^2
x + y
```

Summary of the evaluation tricks

Code SEEN and BUT NOT EVALUATED

```{r, eval = FALSE}

```

Code NOT SEEN and BUT EVALUATED

```{r, echo = FALSE}

```

Bonus! Evaluate the code silently

```{r, include = FALSE}

```

Finally, few useful tricks…

Tables from data frames

We can use the kable function from the knitr package to insert a table made from a data frame:

tb <- head(iris, 4) 
knitr::kable(tb)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa

Controlling appearance of R figures

We control the size/position of figures using the various out.XX and fig.XX chunk arguments, e.g. using out.width='10%' and fig.align='center':

```{r, out.width='50%', fig.align='left', echo=FALSE}
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species)) +
  geom_point(alpha = 0.8)
```

Inserting external figures

Use the include_graphics function from knitr inside an R code chunk:

```{r, out.width='10%', fig.align='center', echo=FALSE}
knitr::include_graphics('./assets/dylanSheep.jpg')
```

Controlling figure output

Again, control the appearance using the various out.XX and fig.XX chunk arguments.

```{r, out.width='10%', fig.align='center', echo=FALSE}
knitr::include_graphics('./assets/dylanSheep.jpg')
```

R Markdown is very powerful…