```{r setup, include=FALSE}
# code chunk specifies whether the R code, warnings, and output
# will be included in the output files.
if (!require("tidyverse")) {
install.packages("tidyverse")
library(tidyverse)
}
if (!require("knitr")) {
install.packages("knitr")
library(knitr)
}
if (!require("cowplot")) {
install.packages("cowplot")
library(cowplot)
}
if (!require("latex2exp")) {
install.packages("latex2exp")
library(latex2exp)
}
if (!require("plotly")) {
install.packages("plotly")
library(plotly)
}
if (!require("gapminder")) {
install.packages("gapminder")
library(gapminder)
}
if (!require("png")) {
install.packages("png") # Install png package
library("png")
}
if (!require("RCurl")) {
install.packages("RCurl") # Install RCurl package
library("RCurl")
}
if (!require("colourpicker")) {
install.packages("colourpicker")
library("colourpicker")
}
if (!require("gganimate")) {
install.packages("gganimate")
library("gganimate")
}
if (!require("gifski")) {
install.packages("gifski")
library("gifski")
}
if (!require("magick")) {
install.packages("magick")
library("magick")
}
if (!require("grDevices")) {
install.packages("grDevices")
library("grDevices")
}
if (!require("jpeg")) {
install.packages("jpeg")
library("jpeg")
}
# knitr::opts_knit$set(root.dir = "C:/Users/75CPENG/OneDrive - West Chester University of PA/Documents")
# knitr::opts_knit$set(root.dir = "C:\\STA490\\w05")
knitr::opts_chunk$set(echo = TRUE,
warning = FALSE,
result = TRUE,
message = FALSE)
```
# {.tabset}
## What is RMD
R Markdown is a file format for making dynamic documents with R. An R Markdown document is written in markdown (an easy-to-write plain text format) and contains chunks of embedded R code, like the document below.
RMarkdown makes use of Markdown syntax. Markdown is a very simple ‘markup’ language that provides methods for creating documents with headers, images, links, etc. from plain text files while keeping the original plain text file easy to read.
R Markdown files are the source code for rich, reproducible documents. We can transform an R Markdown file in two ways.
* **knit** - The rmarkdown package will call the `knitr` package. `knitr` will run each chunk of R code in the document and append the results of the code to the document next to the code chunk.
* **convert** - The rmarkdown package will use the pandoc program to transform the file into a new format such as HTML, PDF, or Microsoft Word file. RMarkdown will preserve the text, code results, and formatting contained in the original.Rmd file.
The term `render` refers to the above two-step process of knitting and converting an R Markdown file.
### Create an RMarkdown file
To create a new RMarkdown file (`.Rmd`), select `File -> New File -> R Markdown`in RStudio, then choose one of the file types we want to create.
The newly created.Rmd file comes with an RMarkdown template with basic instructions.
### The YAML Header
At the top of any RMarkdown script is a YAML (`Y`et `A`nother `M`arkup `L`anguage) header section enclosed by `---`. By default, this includes a title, author, date and the file type you want to output to.
Many other options are available for different functions and formatting, see here for .html options and here for .pdf options. Rules in the header section will alter the whole document.
The following is the simplest YMAL template.
```{}
---
title: "Introduction to RMarkdown"
author: "Cheng Peng"
date: "12/25/2021"
output:
html_document: default
html_notebook: default
editor_options:
chunk_output_type: inline
---
```
Note that YAML is automatically generated that reflects the choice of the file type and related file specifications. Therefore, we don't need to know YAML unless we want to modify it manually.
### Markdown Syntax
* **Formatting Text**
+ *Italic* - single asterisk -> italic.
+ **bold** - double asterisks -> bold face
+ `code` - code in text
* **`Header`**
+ `# Header 1`: single `#` - level 1 header
+ `## Header 2`: double `##`- level 2 header
+ `### Header 3`: triple `###` - level 3 header
* **List**
+ `* Unordered list` - single asterisk `+` **space** -> unordered list
+ `1. Ordered list` - numbered list
* **Hyperlink**
+ `< url >` - Example:
{width=70%}
```{width=70%}
3. Using `knitr` Function ```{} ## Chunk options: fig.align='center', echo=FALSE, fig.cap="White Mountain", out.width = '50%' knitr::include_graphics("https://github.com/pengdsci/sta553/blob/main/image/WhiteMountain.jpg?raw=true") ``` ```{r fig.align='center', echo=FALSE, fig.cap="White Mountain", out.width = '50%'} knitr::include_graphics("https://github.com/pengdsci/sta553/blob/main/image/WhiteMountain.jpg?raw=true") ``` ## Inserting Tables A quality table is also an important visualization tool. We introduce three methods for inserting nice-looking tables into the RMD document. * **Extracting R Output Matrix Using `kable()` Function** For example, we fit a linear regression model using iris data and extract the statistics of model fit to define a table. ```{r} data(iris) iris.model = lm(Sepal.Length~., data = iris) # fit a linear model mod.stats = coef(summary(iris.model)) # inferential stats of coefficients kable(mod.stats) # ``` * **Markdown Table** We can manually create a Markdown table. See the following example. ```{} | Plant | Temp. | Growth | |:------|:-----:|-------:| | A | 20 | 0.65 | | B | 20 | 0.95 | | C | 20 | 0.15 | ``` The resulting Markdown has the following | Plant | Temp. | Growth | |:------|:-----:|-------:| | A | 20 | 0.65 | | B | 20 | 0.95 | | C | 20 | 0.15 | The Markdown table syntax is explained in the following: * `:----:`: Centre * `:-----`: Left * `-----:`: Right * `------`: Auto \ \ * **HTML Table** ```{}Company | Contact | Country |
---|---|---|
Alfreds Futterkiste | Maria Anders | Germany |
Centro comercial Moctezuma | Francisco Chang | Mexico |
Company | Contact | Country |
---|---|---|
Alfreds Futterkiste | Maria Anders | Germany |
Centro comercial Moctezuma | Francisco Chang | Mexico |