R Markdown provides an authoring framework for data science.
You can use a single R Markdown file to both:
R Markdown documents are fully reproducible and support dozens of static and dynamic output formats.
install.packages("rmarkdown")
A plain text file that has the extension .Rmd
Notice that the file contains three types of content:
---
title:...
author:...
date:...
output:...
---
A plain text file that has the extension .Rmd
Notice that the file contains three types of content:
When you open the file in the RStudio IDE, it becomes a notebook interface for R.
You can run each code chunk by clicking the run button
library(rmarkdown)
rmarkdown::render("Lecture_4.1_Rmarkdown.Rmd", output_format = "word_document")
Picture 1
Picture 2
Picture 3
Picture 4
{r include = FALSE}
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.
{r echo = FALSE}
echo = FALSE prevents code, but not the results from appearing in the finished file. This is a useful way to embed figures.
{r message = FALSE }
message = FALSE prevents messages that are generated by code from appearing in the finished file.
{r warning = FALSE}
warning = FALSE prevents warnings that are generated by code from appearing in the finished.
{r fig.cap = "..."}
fig.cap = “…” adds a caption to graphical results.
{r, out.width = "200px", fig.cap = "Example"}
image(volcano, col = viridis(200))
image(volcano, col = viridis(200))
Example
To set global options that apply to every chunk in your file, call knitr::opts_chunk$set() in a code chunk.
For instance:
{r}
knitr::opts_chunk$set(echo = TRUE)
Dashboards are a useful way to communicate large amounts of information visually and quickly.
We will create one with the flexdashboard::flex_dashboard output format.
Flexdashboard makes it easy to organize your content into a visual layout:
Picture 4