Markdown documents are documents that combine text and code. They are saved as simple text documents (with the file extension
.Rmd) and can be edited by any text editor — ideally by a program that interacts with R (like RStudio).
Always specify the current topic (or WPA), your name, and the current date in the header of your file.
In order to use RMarkdown, you first need to install and load the rmarkdown and knitr packages in R.
# Setting echo = FALSE prevents that this code is displayed in the output document. # Setting eval = TRUE ensures that this code chunk is evaluated when knitting the document. ## Install required packages (only once): # install.packages("rmarkdown") # install.packages("knitr") # install.packages("tidyverse", "ds4psy") ## Load required packages (always) at the beginning of an RMarkdown document: library(tidyverse) library(ds4psy)
Markdown document are simple text documents and can be edited by any text editor — ideally by a program that interacts with R (like RStudio).
You can create a new Markdown document in RStudio by clicking
File > New File > R Markdown. At the top of a new file, you will see inputs for a page title, author, date, and the output format. Change the title, author, and date to suit your current document.
There are two basic types of content in a Markdown document:
To write text, you can just type it into the document and use simple formatting commands (e.g., using italics or bold fonts) to emphasize parts of text.
To write and evaluate R code in a Markdown document, you need to enclose the R code into a code chunk.
Here’s how a standard code chunk looks:
# This is a code chunk. Everything in this chunk will be interpreted as R code. # Specifying a unique chunk_name helps to keep track of your various chunks later. # R will evaluate the code in this chunk and print the result: v <- 1:4 sum(v)
##  10
If you use RStudio, the code chunks are marked by a grey background. You can evaluate individual chunks by pressing the buttons on the top right of a chunk window. All code contained in these chunks will be evaluated by R Markdown as R code when you knit the document.
To structure text into sections, the symbol
# is used as a prefix to section titles. Obviously, this differs from the use of the symbol
# in R code, where it is used as a prefix for commenting out lines.
Subsections (and sub-sub-…-sections) can be created by using 2 (3, or more)
Make sure to only introduce new levels of subsections when there are at least 2 headings of a section.
To convert an
.Rmd source file into an output file (into
knitr package is installed and loaded). This can be done by pressing the “Knit” button (at the top of your Editor window of RStudio) or the
Command + Shift + K keyboard shortcut.
If your R code contains errors, the document will fail to knit (and print an error message that specifies the reason or chunk name with problems). Thus, if your document fails to knit, you need to go through your code (in a chunk-by-chunk fashion) to find and fix the error.
Further information on RMarkdown is available at http://rmarkdown.rstudio.com.
[Updated on 2020-05-10 by YourName.]