quarto::quarto_render("report.qmd")1 Quarto & RMarkdown
- This is a quarto document with RMarkdown.
- Markdown basics are covered here.
- In RStudio, you create a .qmdtext file, then click the Render button.
- .qmddocuments can also be rendered as such:
2 Layout
2.1 Inner columns
Organisation of content into columns within the body container.
:::: {.columns}
::: {.column style="background-color: aliceblue"}
Content is left.
:::
::: {.column style="background-color:  #f5b7b1"}
Content is right.
:::
::::Content in left column.
Content in right column.
This is an example of nested columns.
:::: {.columns}
::: {.column style="background-color: aliceblue"}
Content in left column.
:::
::: {.column}
Content in right column.
:::: {.columns}
::: {.column style="background-color: #d0ece7"}
Nested left.
:::
::: {.column style="background-color: #f2d7d5"}
Nested right.
:::
::::
:::
::::Content in left column.
Content in right column.
Nested left.
Nested right.
2.2 Outer columns
Extending content outside the body container.
::: {.column-body style="background-color: lightgray; margin-bottom:0.5em;"}
.column-body
:::
::: {.column-body-outset style="background-color: lightgray; margin-bottom:0.5em;"}
.column-body-outset
:::
::: {.column-page-inset style="background-color: lightgray; margin-bottom:0.5em;"}
.column-page-inset
:::
::: {.column-page style="background-color: lightgray; margin-bottom:0.5em;"}
.column-page
:::
::: {.column-screen-inset style="background-color: lightgray; margin-bottom:0.5em;"}
.column-screen-inset
:::
::: {.column-screen style="background-color: lightgray; margin-bottom:0.5em;"}
.column-screen
:::
::: {.column-body-outset-right style="background-color: lightgray; margin-bottom:0.5em;"}
.column-body-outset-right
:::
::: {.column-page-inset-right style="background-color: lightgray; margin-bottom:0.5em;"}
.column-page-inset-right
:::
::: {.column-page-right style="background-color: lightgray; margin-bottom:0.5em;"}
.column-page-right
:::
::: {.column-screen-inset-right style="background-color: lightgray; margin-bottom:0.5em;"}
.column-screen-inset-right
:::
::: {.column-screen-right style="background-color: lightgray; margin-bottom:0.5em;"}
.column-screen-right
:::
::: {.column-body-outset-left style="background-color: lightgray; margin-bottom:0.5em;"}
.column-body-outset-left
:::
::: {.column-page-inset-left style="background-color: lightgray; margin-bottom:0.5em;"}
.column-page-inset-left
:::
::: {.column-page-left style="background-color: lightgray; margin-bottom:0.5em;"}
.column-page-left
:::
::: {.column-screen-inset-left style="background-color: lightgray; margin-bottom:0.5em;"}
.column-screen-inset-left
:::
::: {.column-screen-left style="background-color: lightgray; margin-bottom:0.5em;"}
.column-screen-left
:::
::: {.column-margin style="background-color: lightgray; margin-bottom:0.5em;"}
.column-margin
:::.column-body
.column-body-outset
.column-page-inset
.column-page
.column-screen-inset
.column-screen
.column-body-outset-right
.column-page-inset-right
.column-page-right
.column-screen-inset-right
.column-screen-right
.column-body-outset-left
.column-page-inset-left
.column-page-left
.column-screen-inset-left
.column-screen-left
.column-margin
2.3 Panel layout
::: {.panel-tabset}
#### Sub topic 1
This is some material for topic 1.
#### Sub topic 2
This is some material for topic 2.
:::This is some material for topic 1.
This is some material for topic 2.
3 Text formatting
Headings can be defined as shown below.
## Level 2 heading  
### Level 3 heading  
#### Level 4 heading  
##### Level 5 heading  
###### Level 6 heading3.1 Level 3 heading
3.1.1 Level 4 heading
Level 5 heading
Level 6 heading
Character styles can be set as below.
[Largest text]{.largest}  
[Larger text]{.larger}  
[Large text]{.large}  
Normal text  
[Small text]{.small}  
[Smaller text]{.smaller}  
[Smallest text]{.smallest}  Largest text
Larger text
Large text
Normal text
Small text
Smaller text
Smallest text
A horizontal line can be created using three or more * or -.
***
This is __Bold text__ This is Bold text
This is _Italic text_ This is Italic text
~~Strikethrough~~ text Strikethrough text
This is Subscript H~2~O displayed as H2O
This is Superscript 2^10^ displayed as 210
This is a [link](r-project.org) This is a link
An example of footnote reference 1
> This is a block quote. This
> paragraph has two lines.
>
> 1. This is a list inside a block quote.
> 2. Second item.This is a block quote. This paragraph has two lines.
- This is a list inside a block quote.
- Second item.
[This content lives in the right margin]{.aside}
4 Code formatting
Code can be defined inline where `this` looks like this. R code can be executed inline `r Sys.Date()` producing 2023-06-12. Code can also be defined inside code blocks.
``` This is code ```
This is codeR code is executed inside code blocks like this
```{r}
Sys.Date()
```which shows the code and output.
Sys.Date()[1] "2023-06-12"The code and results can be hidden while sill executing the code.
```{r}
#| eval: true
#| echo: false
#| results: hide
Sys.Date()
```Here is another example of executed R code with input and output.
data(iris)
head(iris[,1:2])| Sepal.Length | Sepal.Width | 
|---|---|
| 5.1 | 3.5 | 
| 4.9 | 3.0 | 
| 4.7 | 3.2 | 
| 4.6 | 3.1 | 
| 5.0 | 3.6 | 
| 5.4 | 3.9 | 
5 Lists
5.1 Bulleted List
Unordered lists are created using asterisks.
- Bullet 1
 
- Bullet 2
- Sub-bullet 2.1
 
- Sub-bullet 2.2
 
 
- Sub-bullet 2.1
- Bullet 3
Ordered lists are created using numbers.
- Point 1
- Point 2
- Point 3
6 Images
6.1 Using Markdown
Using regular markdown.
The dimensions are based on image and/or fill up the entire available space. You can control the dimension as shown below.
{width=30%}  This image above is now 30% of it’s original width.
Figure layout.
::: {#fig1 layout-ncol=2}
{#subfig1 width="40%"}
{#subfig2 width="40%"}
These figures are interesting.
:::More figure options and layouts are described here. Cross referencing described here.
6.2 Using HTML
This image below is 30% size.
<img src="assets/featured.jpg" style="width:30%;"/>

6.3 Using R
R chunks in RMarkdown can be used to control image display size using the argument out.width.
This image below is displayed at a size of 300 pixels.
```{r}
#| out-width: 300px
knitr::include_graphics("assets/featured.jpg")
```This image below is displayed at a size of 75 pixels and a caption added.
```{r}
#| out-width: 75px
#| fig-cap: This is a caption
knitr::include_graphics("assets/featured.jpg")
```For more information on figures, see here. For plots generated through R, see section further below.
7 Math expressions
Some examples of rendering equations.
$e^{i\pi} + 1 = 0$\(e^{i\pi} + 1 = 0\)
$$\frac{E \times X^2 \prod I}{2+7} = 432$$\[\frac{E \times X^2 \prod I}{2+7} = 432\]
$$\sum_{i=1}^n X_i$$\[\sum_{i=1}^n X_i\]
$$\int_0^{2\pi} \sin x~dx$$\[\int_0^{2\pi} \sin x~dx\]
$\left( \sum_{i=1}^{n}{i} \right)^2 = \left( \frac{n(n-1)}{2}\right)^2 = \frac{n^2(n-1)^2}{4}$\(\left( \sum_{i=1}^{n}{i} \right)^2 = \left( \frac{n(n-1)}{2}\right)^2 = \frac{n^2(n-1)^2}{4}\)
$\begin{eqnarray} X & \sim & \mathrm{N}(0,1)\\ Y & \sim & \chi^2_{n-p}\\ R & \equiv & X/Y \sim t_{n-p} \end{eqnarray}$\(\begin{eqnarray} X & \sim & \mathrm{N}(0,1)\\ Y & \sim & \chi^2_{n-p}\\ R & \equiv & X/Y \sim t_{n-p} \end{eqnarray}\)
$\begin{eqnarray} P(|X-\mu| > k) & = & P(|X-\mu|^2 > k^2)\\ & \leq & \frac{\mathbb{E}\left[|X-\mu|^2\right]}{k^2}\\ & \leq & \frac{\mathrm{Var}[X]}{k^2} \end{eqnarray}$\(\begin{eqnarray} P(|X-\mu| > k) & = & P(|X-\mu|^2 > k^2)\\ & \leq & \frac{\mathbb{E}\left[|X-\mu|^2\right]}{k^2}\\ & \leq & \frac{\mathrm{Var}[X]}{k^2} \end{eqnarray}\)
8 Tables
8.1 Manual
For simple cases, tables can be manually created in markdown.
|speed|dist|
|-----|----|
|4    |   2|
|4    |  10|
|7    |   4|| speed | dist | 
|---|---|
| 4 | 2 | 
| 4 | 10 | 
| 7 | 4 | 
8.2 htmlTable
Markdown tables can be enhanced using the R package htmlTable.
library(htmlTable)
iris1 <- iris[c(1:4,51:53,105:108),]
htmlTable(iris1, rgroup=unique(iris1$Species), n.rgroup=rle(as.character(iris1$Species))$lengths)| Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | |
|---|---|---|---|---|---|
| setosa | |||||
| 1 | 5.1 | 3.5 | 1.4 | 0.2 | setosa | 
| 2 | 4.9 | 3 | 1.4 | 0.2 | setosa | 
| 3 | 4.7 | 3.2 | 1.3 | 0.2 | setosa | 
| 4 | 4.6 | 3.1 | 1.5 | 0.2 | setosa | 
| versicolor | |||||
| 51 | 7 | 3.2 | 4.7 | 1.4 | versicolor | 
| 52 | 6.4 | 3.2 | 4.5 | 1.5 | versicolor | 
| 53 | 6.9 | 3.1 | 4.9 | 1.5 | versicolor | 
| virginica | |||||
| 105 | 6.5 | 3 | 5.8 | 2.2 | virginica | 
| 106 | 7.6 | 3 | 6.6 | 2.1 | virginica | 
| 107 | 4.9 | 2.5 | 4.5 | 1.7 | virginica | 
| 108 | 7.3 | 2.9 | 6.3 | 1.8 | virginica | 
8.3 kable
Simple table using kable from R package knitr. This is the default output for quarto.
head(iris)| Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | 
|---|---|---|---|---|
| 5.1 | 3.5 | 1.4 | 0.2 | setosa | 
| 4.9 | 3.0 | 1.4 | 0.2 | setosa | 
| 4.7 | 3.2 | 1.3 | 0.2 | setosa | 
| 4.6 | 3.1 | 1.5 | 0.2 | setosa | 
| 5.0 | 3.6 | 1.4 | 0.2 | setosa | 
| 5.4 | 3.9 | 1.7 | 0.4 | setosa | 
8.3.1 Margin table
```{{r}}
#| fig-caption: This table is in the margin.
#| column: margin
head(cars)
```| speed | dist | 
|---|---|
| 4 | 2 | 
| 4 | 10 | 
| 7 | 4 | 
| 7 | 22 | 
| 8 | 16 | 
| 9 | 10 | 
8.4 kableExtra
More advanced table using kableExtra and formattable.
library(kableExtra)
iris[c(1:4,51:53,105:108),] %>%
  mutate(Sepal.Length=color_bar("lightsteelblue")(Sepal.Length)) %>%
  mutate(Sepal.Width=color_tile("white","orange")(Sepal.Width)) %>%
  mutate(Species=cell_spec(Species,"html",color="white",bold=T,
    background=c("#8dd3c7","#fb8072","#bebada")[factor(.$Species)])) %>%
  kable("html",escape=F) %>%
  kable_styling(bootstrap_options=c("striped","hover","responsive"),
                full_width=F,position="left") %>%
  column_spec(5,width="3cm")| Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | |
|---|---|---|---|---|---|
| 1 | 5.1 | 3.5 | 1.4 | 0.2 | setosa | 
| 2 | 4.9 | 3.0 | 1.4 | 0.2 | setosa | 
| 3 | 4.7 | 3.2 | 1.3 | 0.2 | setosa | 
| 4 | 4.6 | 3.1 | 1.5 | 0.2 | setosa | 
| 51 | 7.0 | 3.2 | 4.7 | 1.4 | versicolor | 
| 52 | 6.4 | 3.2 | 4.5 | 1.5 | versicolor | 
| 53 | 6.9 | 3.1 | 4.9 | 1.5 | versicolor | 
| 105 | 6.5 | 3.0 | 5.8 | 2.2 | virginica | 
| 106 | 7.6 | 3.0 | 6.6 | 2.1 | virginica | 
| 107 | 4.9 | 2.5 | 4.5 | 1.7 | virginica | 
| 108 | 7.3 | 2.9 | 6.3 | 1.8 | virginica | 
8.5 DT
Interactive table using R package DT.
library(DT)
iris %>%
  slice(1:15) %>%
  datatable(options=list(pageLength=7))8.6 reactable
Advanced interactive tables with reactable.
library(reactable)
reactable(iris[sample(1:150,10),],
  columns = list(
    Sepal.Length = colDef(name = "Sepal Length"),
    Sepal.Width = colDef(name = "Sepal Width"),
    Petal.Width = colDef(name = "Petal Width"),
    Petal.Width = colDef(name = "Petal Width")
  ),
  striped = TRUE,
  highlight = TRUE,
  filterable = TRUE
)reactable creation can be simplified as well as enhanced by using reactablefmtr.
9 Static plots
9.1 Base plot
- Plots using base R are widely used and may be good enough for most situations.
- But they lack a consistent coding framework.
{plot(x=iris$Sepal.Length,y=iris$Sepal.Width,
      col=c("coral","steelblue","forestgreen")[iris$Species],
      xlab="Sepal Length",ylab="Sepal Width",pch=19)
legend(x=7,y=4.47,legend=c("setosa","versicolor","virginica"),
       col=c("coral","steelblue","forestgreen"),pch=19)}Refer to @plot-base to see an example of base plot.
9.1.1 Multiple plots
```{r}
#| column: screen-inset-shaded
#| layout-nrow: 1
plot(cars)
plot(iris)
plot(pressure)
```9.1.2 Margin plot
```{{r}}
#| column: margin
plot(cars)
```9.2 ggplot2
R package ggplot2 is one of the most versatile and complete plotting solutions.
10 Interactive plots
10.1 rbokeh
R package rbokeh is an easy and convenient option to get started with interactive plots.
library(rbokeh)
figure(height=400,width=600,xlab="Sepal Length",ylab="Sepal Width") %>%
  ly_points(Sepal.Length,Sepal.Width,data=iris,
  color=Species,glyph=Species,hover=list(Sepal.Length,Sepal.Width))Interactive scatterplot using rbokeh.
10.2 highcharter
R package highcharter is a wrapper around javascript library highcharts.
library(highcharter)
h <- iris %>%
  hchart(.,"scatter",hcaes(x="Sepal.Length",y="Sepal.Width",group="Species")) %>%
  hc_xAxis(title=list(text="Sepal Length"),crosshair=TRUE) %>%
  hc_yAxis(title=list(text="Sepal Width"),crosshair=TRUE) %>%
  hc_chart(zoomType="xy",inverted=FALSE) %>%
  hc_legend(verticalAlign="top",align="right") %>%
  hc_size(height=400)
htmltools::tagList(list(h))Interactive scatterplot using highcharter.
10.3 plotly
R package plotly provides R binding around javascript plotting library plotly.
library(plotly)
p <- iris %>%
  plot_ly(x=~Sepal.Length,y=~Sepal.Width,color=~Species,width=500,height=400) %>%
  add_markers()
pInteractive scatterplot using plotly.
10.4 ggplotly
plotly also has a function called ggplotly which converts a static ggplot2 object into an interactive plot.
library(plotly)
p <- iris %>%
  ggplot(aes(x=Sepal.Length,y=Sepal.Width,col=Species))+
  geom_point()+
  labs(x="Sepal Length",y="Sepal Width")+
  theme_bw(base_size=12)
ggplotly(p,width=500,height=400)Interactive scatterplot using ggplotly.
10.5 ggiraph
ggiraph is also an R package that can be used to convert a static ggplot2 object into an interactive plot.
library(ggiraph)
p <- ggplot(iris,aes(x=Sepal.Length,y=Petal.Length,colour=Species))+
      geom_point_interactive(aes(tooltip=paste0("<b>Petal Length:</b> ",Petal.Length,"\n<b>Sepal Length: </b>",Sepal.Length,"\n<b>Species: </b>",Species)),size=2)+
  theme_bw()
tooltip_css <- "background-color:#e7eef3;font-family:Roboto;padding:10px;border-style:solid;border-width:2px;border-color:#125687;border-radius:5px;"
ggiraph(code=print(p),hover_css="cursor:pointer;stroke:black;fill-opacity:0.3",zoom_max=5,tooltip_extra_css=tooltip_css,tooltip_opacity=0.9)Interactive scatterplot using ggiraph.
10.6 dygraphs
R package dygraphs provides R bindings for javascript library dygraphs for time series data.
library(dygraphs)
lungDeaths <- cbind(ldeaths, mdeaths, fdeaths)
dygraph(lungDeaths,main="Deaths from Lung Disease (UK)") %>%
  dyOptions(colors=c("#66C2A5","#FC8D62","#8DA0CB"))Interactive time series plot using dygraph.
10.7 Network graph
R package networkD3 allows the use of interactive network graphs from the D3.js javascript library.
library(networkD3)
data(MisLinks,MisNodes)
forceNetwork(Links=MisLinks,Nodes=MisNodes,Source="source",
             Target="target",Value="value",NodeID="name",
             Group="group",opacity=0.4)Interactive network plot.
10.8 leaflet
R package leaflet provides R bindings for javascript mapping library; leafletjs.
library(leaflet)
leaflet(height=500,width=700) %>%
  addTiles(urlTemplate='http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png') %>%
  #addProviderTiles(providers$Esri.NatGeoWorldMap) %>%
  addMarkers(lat=57.639327,lng=18.288534,popup="RaukR") %>%
  setView(lat=57.639327,lng=18.288534,zoom=15)Interactive map using leaflet.
10.9 crosstalk
R package crosstalk allows crosstalk enabled plotting libraries to be linked. Through the shared ‘key’ variable, data points can be manipulated simultaneously on two independent plots.
library(crosstalk)
shared_quakes <- SharedData$new(quakes[sample(nrow(quakes), 100),])
lf <- leaflet(shared_quakes,height=300) %>%
        addTiles(urlTemplate='http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png') %>%
        addMarkers()
py <- plot_ly(shared_quakes,x=~depth,y=~mag,size=~stations,height=300) %>%
        add_markers()
htmltools::div(lf,py)Linking independent plots using crosstalk.
11 ObservableJS
- Quarto supports ObservableJS for interactive visualisations in the browser.
Pass data from R to OJS
irism <- iris
colnames(irism) <- gsub("[.]","_",tolower(colnames(irism)))
ojs_define(ojsd = irism)ojsdata = transpose(ojsd)Display as a table
viewof filtered_table = Inputs.table(ojsdata)Define inputs
viewof x = Inputs.select(Object.keys(ojsdata[0]), {value: "sepal_length", multiple: false, label: "X axis"})viewof y = Inputs.select(Object.keys(ojsdata[0]), {value: "sepal_width", multiple: false, label: "Y axis"})Display plot
Plot.plot({
  marks: [
    Plot.dot(ojsdata, {
      x: x,
      y: y,
      fill: "species",
      title: (d) =>
        `${d.species} \n Petal length: ${d.petal_length} \n Sepal length: ${d.sepal_length}`
    })
  ],
  grid: true
})ObservableJS in quarto documentation.
12 Diagrams
```{mermaid}
flowchart LR
  A[Hard edge] --> B(Round edge)
  B --> C{Decision}
  C --> D[Result one]
  C --> E[Result two]
```flowchart LR
  A[Hard edge] --> B(Round edge)
  B --> C{Decision}
  C --> D[Result one]
  C --> E[Result two]Diagram documentation.
13 Icons
To use fontawesome icons as shortcodes, quarto extension fontawesome needs to be installed.
Icons can be placed using shortcodes.
{{< fa lightbulb >}} 
{{< fa exclamation >}} 
{{< fa clipboard-list >}} 
{{< fa comments >}} 
{{< fa desktop >}} 
{{< fa cloud >}} 
{{< fa check >}} 
{{< fa times >}} 
{{< fa skull >}} 
{{< fa skull size=2x >}} 
{{< fa brands github >}} 
Icons can be displayed using the HTML <i> tag. Note that not all icons may work.
Here is a <i class='fa fa-calendar'></i> calendar and a <i class='fa fa-couch'></i> couch.
Here is a calendar and a couch.
Icons can also be placed programatically through R using the R package fontawesome.
`r fontawesome::fa('lightbulb')`
Optional arguments are height and fill.
`r fontawesome::fa('lightbulb',height='30px',fill='steelblue')`
For full list of icons check out FontAwesome.
14 Call-Outs
Call-Out blocks are explained here.
::: {.callout-note}
This is a call-out.
:::
::: {.callout-warning}
This is a call-out.
:::
::: {.callout-important}
This is a call-out.
:::
::: {.callout-tip}
This is a call-out.
:::
::: {.callout-caution}
This is a call-out.
:::
::: {.callout-tip collapse="true"}
## Call-out with collapse
This content is hidden by default.
:::This is a call-out.
This is a call-out.
This is a call-out.
This is a call-out.
This is a call-out.
This content is hidden by default.
15 Alerts
::: {.alert .alert-primary}
**Note:** This is an alert!
:::
::: {.alert .alert-secondary}
**Note:** This is an alert!
:::
::: {.alert .alert-success}
**Note:** This is a success alert!
:::
::: {.alert .alert-danger}
**Note:** This is a danger alert!
:::
::: {.alert .alert-warning}
**Note:** This is a warning alert!
:::
::: {.alert .alert-info}
**Note:** This is an info alert!
:::Note: This is an alert!
Note: This is an alert!
Note: This is a success alert!
Note: This is a danger alert!
Note: This is a warning alert!
Note: This is an info alert!
16 General tips
- Use level 2 heading as the highest level
- Use quarto style chunk options
- warnings and messages has been turned off in chunks globally
- Declare all libraries at the beginning of the document
- Add custom css under YAML if needed css: "my-theme.css"
- Check out the Quarto website





