R Markdown

Beatriz Manso
2021-11-5

R Markdown provides a unified interface for writing, collaborating, and communicating data analysis, data science, and results, making it easier to keep track of all your code, results, plots, and writing. This is especially useful for producing documents for audiences that are only interested in the results of your analysis, not the code.

Install R Markdown in R studio

Create an RMarkdown file

File -> New File -> R Markdown…

Then choose the title and output format.

R Markdown Document Format

• YAML Header: Controls some output settings that apply to the final document. • Code Chunk: Includes code to run, and code-related options. • Body Text: For communicating results and findings to the targeted audience. • Code to Generate a Table: Outputs a table with minimal formatting like you would see in the console. • Headers: Specified with ##. • Code to Generate a Plot: Outputs a plot

Markdown syntax

  1. First Level Header (##) Second Level Header (###) Third Level Header

“#” inside a code chunk acts as a normal R comment

# Chunk Options

  1. 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.
  2. echo = FALSE prevents code, but not the results from appearing in the finished file. This is a useful way to embed figures.
  3. message = FALSE prevents messages that are generated by code from appearing in the finished file.
  4. warning = FALSE prevents warnings that are generated by code from appearing in the finished.
  5. fig.cap = “…” adds a caption to graphical results.