Guides for R-Markdown

We utilize R Markdown files, .Rmd, to combine formatted text (formatted using “Markdown” syntax) and code (incl. results), which can be exported to, among others, .html or .pdf files. Click here for more details on using R Markdown.

You can find troubleshooting advice and quick guides for problems and frequently asked questions on R-Markdown (.rmd).


Writing and executing code in R Markdown

In .Rmd files, you can execute R-code in so called chunks (see image below). When you click the Knit or Preview button in R, a document will be generated that includes both content, as well as the output of any embedded R code chunks within the document.

  • To add a chunk, use the shortkey Ctrl + Alt + I (on Windows and Linux). For macOS, use either Cmd + Option + I or Cmd+Alt+I.
  • To run a complete code chunk on Windows and Linux, use Ctrl + Shift + Enter (Windows, Linux). For macOS, use either Cmd + Shift + Enter or Cmd + Option + C.
  • To run parts of your code, you can highlight the respective code and press Ctrl + Enter or Cmd + Enter respectively. To run a single line of code, select the line with your cursor and press the same shortcuts used to run highlighted parts of code.

Chunk Options

Chunk output in your final document can be customized with knitr options, arguments set in the {} of a chunk header. Here are some useful arguments:

  • include = FALSE prevents code and results from appearing in the finished file. R Markdown still runs the code in the chunk, and the results can be used by other chunks.
  • echo = FALSE prevents code, but not the results from appearing in the finished file. This is a useful way to embed figures.
  • message = FALSE prevents messages that are generated by code from appearing in the finished file.
  • warning = FALSE prevents warnings that are generated by code from appearing in the finished.
  • fig.cap = "..." adds a caption to graphical results.

Official RMarkdown Tutorial & Cheatsheet

For formatting and an overview of additional R Markdown functionalities, we recommend the R Markdown Cheat Sheet. Hint: No need to save the link: In RStudio, you can always access it under Help > Cheat Sheets > R Markdown Cheat Sheet.