Welcome to learning about R Markdown!
The total length of the videos in this section is approximately 10 minutes, but you will spend time answering short questions while completing this section.
This module was created by Sandy Liu '25.
R Markdown is a way to simplify your workflow. You create a code file that also includes the writing or images you'd like to include when you report your results. When you ``knit" the code file, the resulting pdf or word doc or html is a write-up that combines the results of the code - graphics, tables, single numbers, and even the code itself if you want - with the rest of the report.
If you use markdown to create a report that includes graphics or summary statistics or the results of analysis from your data, then if you make one change to your data set (maybe one more person filled out the survey), you can update then entire report with the click of a button. So, your report, publication, thesis chapter, or a web page will be reproducible and easy to update, and there is no copy-pasting output.
You can also think of this as ethical data science: if you produce a markdown file, then you have been completely transparent about where each number and graphic in your report comes from. Anyone who understands R can see exactly what you did. It's a way to make your entire report reproducible, not just your code.
You also might want to use markdown if you plan to include visible code in your document (so, perhaps for certain stat class homeworks, but not for the QAI Program).
R Markdown is increasingly popular, and it's a skill worth having, both for making your own reports and for understanding others'.
You can use either RStudio or R to use R Markdown. RStudio is easier, so if you haven't tried RStudio yet, now is a good time. Suppose you're going to be using R (not RStudio). In that case, we first need to download a document converter, so before you watch the videos below, download the Pandoc document converter software, which can be done here: https://pandoc.org/installing.html.
Please look at the word and pdf output file sample to get a sense of how output files are supposed to look like before going into the videos. HTML output is not included but is also an option.
R Markdown is a way to combine R code, results from your data analysis (including plots and tables), and written commentary into a single nicely formatted and reproducible document (like a report, publication, thesis chapter or a web page like this one).
The video discusses two ways to create a R Markdown template, but please feel free to use the Rmd template provided here and make edits directly to that file. The file type is .Rmd, and you can open the file in R or R Studio.
Question 2: How do we make bold text?
** the text that needs to be bolded **
^ the text that needs to be bolded ^
## the text that needs to be bolded
* the text that needs to be bolded *
** the text that needs to be bolded **
Question 3: How do we include R code (that needs to be executed) in the R Markdown file?
```your R code```
`your R code`
`r your R code`
```{r} your R code```Â
Option 3 and 4. Option 3 is "inline" R code, which means that you can plug this in the middle of a line of text. It is useful when you are referencing some data when you are making a statement. Option 4 is for an R code paragraph. A code paragraph is useful when you have multiple lines of commands that you want to show.
Question 4: How do we include a plot in the R Markdown file?
Surround your code that generates the plot with ```{r pressure, echo = FALSE/TRUE} your code here```
If you want your code to show up along with the plot, use echo = TRUE. Otherwise, use FALSE.
Sometimes you need to download Latex (a math writing application) in order to output a PDF with markdown.
If your goal is to create a pdf to turn in with your homework, and you run into the Latex problem, the easiest solution is to output to HTML and then export that to PDF.
If you are using markdown for anything other homework, it's likely worthwhile to download Latex so that you can easily output PDFs, especially if you'll be including images that are not generated by the R code itself. When you are exporting an output file, pay special attention if you are inserting an image that you downloaded on your computer. If your output format is HTML, your image might be lost if you try to open your HTML page on another computer. PDF is the safest choice to maintain an image, even it takes several more steps to download LaTex.
Thank you working through this tutorial! :)