RMarkdown

R Markdown can help generate:

  • HTML documents
  • Notebooks in which you’ve run code chunks individually
  • PDFs that you can print out to follow along physically with the course
  • This entire R course website

It enables you to:

  • save and execute code and display its output
  • create high quality reports that could include LaTeX equations

What is great about R Markdown documents is that they are fully reproducible and support many static and dynamic output formats, to name a few: PDF, HTML, MS Word, Beamer… You can incorporate narrative text and code of your data analysis to produce an elegantly formatted story telling journey.

It is a variant of Markdown that has embedded R code chunks (denoted by three back ticks), to be used with knitr to make it easy to create reproducible web-based reports.

R Markdown is a plain text file that has the extension .Rmd

To use R Markdown you will need to install the package from CRAN and load it with:

install.packages("rmarkdown", repos = "http://cran.us.r-project.org")
suppressPackageStartupMessages(library(rmarkdown))

👉 Go to the following GitHub repo to download the material: https://github.com/TanjaKec/RMarkdown_Intro


Starting with RMarkdown

Task 1: Open the file RMarkdown_Intro.Rmd

  • Change the title of the Markdown Document from My First Markdown Document to RMarkdown Introduction.

  • Click the “Knit” button to see the compiled version of your sample code.

Congratulations! You’ve just Knitted your first Rmd document!!!! 👍😃


Basic Text editing

Task 2: Let’s format this document further by

  • Changing the author of the document to your own name

  • Rewriting the first sentence of the document to say "This is my first R Markdown document.

  • Recompiling the document so you can see your changes


Adding a link

You can turn a word into a link by surrounding it in hard brackets: [ ] and then placing the link behind it in parentheses: ( ), like this:

[RStudio](www.rstudio.com)

Task 3: Make GitHub in the following paragraph link to https://github.com/SisterAnalyst


Text formatting

To embed formatting instructions into your document using Markdown, you would surround text by:

  • one asterisk to make it italic: italic

  • two asterisks to make it bold: bold and

  • backticks to make it monospaced: monospaced.

To make an ordered list you need to place each item on a new line after a number followed by a period followed by a space: 1. order list 2. second item

💡! Note that you need to place a blank line between the list and any paragraphs that come before it.

Task 4:

  • Make the following paragraph in your Rmd document look like this:

When analysing data… The variables can be one of two broad types:

  1. Attribute variable: has its outcomes described in terms of its characteristics or attributes;

  2. Measured variable: has the resulting outcome expressed in numerical terms.

  • Make the word Knit in the following paragraph italic.

Embedding the R code To embed an R code chunk you would use three back ticks:

```{r}

chunk of code

```

Task 5: Replace the cars data set with the gapminder data set. Don’t forget to load gapminder package using library(gapminder).


Prevent printing of the R code

You can also embed plots by setting echo = FALSE to the code chunk to prevent printing of the R code that generates the plot:

```{r, echo=FALSE}

chunk of code

```

Task 6: Replace the base boxplot of mpg vs. cyl by a ggplot’s boxplot to examine a relationship between continent and lifeExp (remember to use some of the dplyr functions too!).

suppressPackageStartupMessages(library(dplyr))
library(ggplot2)
# ggplot boxplot
ggplot(gapminder, aes(x = continent, y = lifeExp)) +
  geom_boxplot(outlier.colour = "hotpink") +
  geom_jitter(position = position_jitter(width = 0.1, height = 0), 
              alpha = .2) +
  labs (title= "Life Exp. vs. Continent", 
        x = "Continent", y = "Life Exp.") +
  theme(legend.position = "none", 
        panel.border = element_rect(fill = NA, 
                                    colour = "black",
                                    size = .75),
        plot.title=element_text(hjust=0.5))    

Adding LaTex equations

Finally, if you wish to add mathematical equations to your Markdown document you can easily embed LaTeX math equations into your report.

To display an equation in its own line it needs to be surrounded by the double dollar symbol

$$ y = a + bx $$,

or to embed an equation in line within the text you would use only one dollar symbol: $y = a + bx$.

Task 7: Display the equation in the Including Mathematical Equations paragraph into its own line.

Congratulations! You have got the basics to start creating your own fabulous dynamic documents… !!!! 👍😃


You would definitely find the following useful:


© 2020 Sister Analyst