R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Code chunk options

Your R Markdown is broken up into ‘code’ chunks and text or ‘markdown’ chunks. Making nice looking R Markdown documents means understanding how to format your code chunks and markdown chunks appropriately.

https://yihui.org/knitr/options/

## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Formatting Markdown chunks

A few hints for the markdown chunks:

R Markdown is an easy-to-write plain text format for creating dynamic documents and reports. See Using R Markdown to learn more.

Figures

You can include figures in a text chunk

image: Alt text

Or you can include them in code chunks.

Alt text here

Alt text here

By default, tables just print like they do in the consul when you knit the document

test = data.frame(Year = c(2000:2023), X =c(0:23), Y = c("Apple", "banana", "cheese"),
                  Z = c(exp(1), pi, exp(2)))

print(test)
##    Year  X      Y        Z
## 1  2000  0  Apple 2.718282
## 2  2001  1 banana 3.141593
## 3  2002  2 cheese 7.389056
## 4  2003  3  Apple 2.718282
## 5  2004  4 banana 3.141593
## 6  2005  5 cheese 7.389056
## 7  2006  6  Apple 2.718282
## 8  2007  7 banana 3.141593
## 9  2008  8 cheese 7.389056
## 10 2009  9  Apple 2.718282
## 11 2010 10 banana 3.141593
## 12 2011 11 cheese 7.389056
## 13 2012 12  Apple 2.718282
## 14 2013 13 banana 3.141593
## 15 2014 14 cheese 7.389056
## 16 2015 15  Apple 2.718282
## 17 2016 16 banana 3.141593
## 18 2017 17 cheese 7.389056
## 19 2018 18  Apple 2.718282
## 20 2019 19 banana 3.141593
## 21 2020 20 cheese 7.389056
## 22 2021 21  Apple 2.718282
## 23 2022 22 banana 3.141593
## 24 2023 23 cheese 7.389056

You can do better with the kable function in the knitr package

kable(test, format = "pipe", caption = "A prettier table", digits =2, padding =2)
A prettier table
Year X Y Z
2000 0 Apple 2.72
2001 1 banana 3.14
2002 2 cheese 7.39
2003 3 Apple 2.72
2004 4 banana 3.14
2005 5 cheese 7.39
2006 6 Apple 2.72
2007 7 banana 3.14
2008 8 cheese 7.39
2009 9 Apple 2.72
2010 10 banana 3.14
2011 11 cheese 7.39
2012 12 Apple 2.72
2013 13 banana 3.14
2014 14 cheese 7.39
2015 15 Apple 2.72
2016 16 banana 3.14
2017 17 cheese 7.39
2018 18 Apple 2.72
2019 19 banana 3.14
2020 20 cheese 7.39
2021 21 Apple 2.72
2022 22 banana 3.14
2023 23 cheese 7.39

You can make interactive and sortable tables with the DT package. https://rstudio.github.io/DT/

library(DT)

datatable(test, filter = "top", class = "cell-border stripe")

Further Reading

There are lots and lots of extensions you can add to your markdown documetns to make them really, really spiffy. A few examples: