RMarkdown basics
The basics of RMarkdown
đź”— Links
- RStudio Markdown Basics
- tutorial here, homepage here
- RMarkdown overview/references
- Bookdown - rmarkdown syntax
Italics
Can be produced by either _text1_
or *text1*
which will look this text1 or text2.
Bold
Produced using a pair of double asterisks **text3**
or __text4__
which will look like this text3 or text4.
Headers
# Header 1
## Header 2
### Header 3
etc...
Numbered list
1. Item 1
2. Item 2
3. Item 3
- part a
- part b
Unnumbered list
* Item 1
* Item 2
* Item 3
+ part a
+ part b
Script
A pair of tildes (~
) turn text to a subscript (e.g., H~3~PO~4~
renders H3PO4). A pair of carets (^
) produce a superscript (e.g., Cu^2+^
renders Cu2+). To get strike through ~~strike~~
renders to this strike
Images
![](http://example.com/logo.png)
![optional caption text](figures/img.png)
Hyperlinks
://example.com) [linked phrase](http
Horizontal rule
Three or more asterisks or dashes ***
or ---
Tables
| Second Header
First Header ------------- | -------------
| Content Cell
Content Cell | Content Cell Content Cell
Math and symbols
$x = y $
$x = y $ $x < y $
$x < y $ $x \le y $
$x y $ $x^{n}$
\(x^{n}\) $\hat{x}$
\(\hat{x}\) $\tilde{x}$
\(\tilde{x}\) $\frac{a}{b}$
\(\frac{a}{b}\) $\mu M$
\(\mu M\) $\alpha A$
\(\alpha A\) $\beta B$
\(\beta B\) $\pi \Pi$
\(\pi \Pi\) $^\circ C$
\(^\circ C\)
Code chunks & output
Resource link here
Text colour
Below are some useful bits of code that you can use to change font. For basic Rmd syntax see official Reference Guide.
It is also handy to have colour guides/convertors at the ready, checkout the my section on colour in Plot Aesthetics & Colour.
For Rmd
-> PDF
docs
Font colour
The quickest option is this \textcolor{olive}{This text is olive}
. It will look like this “This text is olive” in your PDF document.
If you want a more customisable option you can use the following to first choose your font colour, and then apply it. Define text colour (this won’t appear in your Knitted PDF)
\definecolor{fancyTextColor}{HTML}{FF0000}
Then use the following syntax to change font colour \colorbox{hightlightColor}{This text is red}
It will look like this “This text is red” in your PDF document.
For Rmd
-> html
docs
Font colour and text
the following html code
Roses are <span style="color:red; font-family:Georgia; font-size:2em;">red.</span>
Will give you this output
Roses are red.
For coloured text blocks.
<style>
-color:#e6f0ff; border-radius: 5px; padding: 20px;}
div.blue { background</style>
<div class = "blue">
- This is my first conclusion
- This is my second conclusion
</div>
<style>
-color:#e6f0ff; border-radius: 5px; padding: 20px;}
div.blue { background</style>
- This is my first conclusion
- This is my second conclusion
Custom Fonts in R Markdown PDF Font Formatting - coloring and emphasis
Hyperlinks
Underlink links in PDF
documents When you knit a document to PDF the hyperlinks are not underlined or colour, although they still work just fine. If you prefer to have your hyperlinks underlined as the following to the header section
header-includes:
- \hypersetup{colorlinks=false,
allbordercolors={0 0 0},
pdfborderstyle={/S/U/W 1}}
Another way is to use the following latex command, however this does not knit well to html. [\underline{R Markdown: The Definitive Guide}](https://bookdown.org/yihui/rmarkdown/)
will give the following: R Markdown: The Definitive Guide
Images, Figures and videos
Tips and tricks for working with images and figures in R Markdown documents.
Simple add for image
![image caption](path/to/image.png)
Videos
Embed video
vebmedr
is a package that makes embedding video into Rmarkdown simple.
Install the package from CRAN: install.packages("vembedr")
.
Example
embed_url("https://www.youtube.com/watch?v=uV4UpCq2azs")
Fun stuff
emojis
You can add emojis into Rmarkdown html docs with the following code: :bowling:
which will give you this 🎳 A full list of emoji short cuts available here.
You could also use unicode formatting - see here for more info.
You can also use emojis in plotting, see here.
Icons
Depending on the type of RMarkdown document you are working with (i.e. what are you “knitting” to), will depend on which code works for you. As always there is more than one way to this.
To start with the most commonly used icons are from font awesome
suite - so head here to pick your (free) icons.
HTML docs
<script src="https://unpkg.com/ionicons@5.0.0/dist/ionicons.js"></script>
For HTML docs (including webpages), I have found the following works best <i class="fab fa-r-project"></i>
this will give you a beautiful RStudio icon .
If you want to use Ionicons
then include you will first need to make sure the following line appears within your Rmarkdown doc (don’t worry it wont appear in the knitted product) <script src="https://unpkg.com/ionicons@5.0.0/dist/ionicons.js"></script>
. Then use the following code to insert your icons, for example this <ion-icon name="bug-outline"></ion-icon>
produces a lovely bug for you
This code <span class="badge badge-info">Info</span>
will produce this badge Info in html document, more details found here
Webpages (HTML) - nav bar
You can add icons to the navigation bar on your webpage, just like on this one. In the YAML doc add the following bit of code under the relevant section icon: fa-icon
e.g. for the rstudio icon use fa-r-project
for this icon
Example - partial YAML doc, for more info on RMarkdown webpages/YAML head to RMarkdown Webpages section
navbar:
title: "Code"
left:
- text: "Home"
href: index.html
- text: "RStudio"
icon: fa-file-alt
PDF docs
When knitting to PDF I have found the easiest way using the icon
package.
Install developer version from github: devtools::install_github("ropenscilabs/icon")
Then add the following bit of code in your RMarkdown document.
library(icon)
icon::fa("rocket") # equivalent to icon::fa_rocket()
and you’ll get this 🚀 . Got some extra tips and trick see here.
Diagram
Table
In additional to using basic RMarkdown syntax for tables
| Second Header
First Header ------------- | -------------
| Content Cell
Content Cell | Content Cell Content Cell
Which look like this
First Header | Second Header |
---|---|
Content Cell | Content Cell |
Content Cell | Content Cell |
You can use KableExtra
Themes
Headings
In addition to info in the Rmarkdown page some extra bits.
Add sections to the book using the following at the start of a RMarkdown page
# (PART) Section A {-}
# Page 1
This will section will appear until it recognises another section.
Call out note
:::{.callout-note}
:
Note that there are five types of callouts, including`note`, `tip`, `warning`, `caution`, and `important`.
:::
Gives this: