---
title: "R: Visualize Dashboards with Quarto"
---

```{css, echo=FALSE}
.setoff {
  background-color: rgb(245,245,245);
  border: 1px none rgb(100,100,100);
  border-radius: .45em;
  padding-top: .5em;
  padding-left: .5em;
  padding-bottom: -.5em;
}
```

## Setup

Accomplish R dashboards via Quarto. The Quarto website displays a [gallery](https://quarto.org/docs/gallery/index.html#dashboards) of possibilities. The website also provides more detailed dashboard [instructions](https://quarto.org/docs/dashboards/). Here, we cover the basics.

To construct a dashboard with R, do so by creating a type of document called Quarto. A distinguishing characteristic of a Quarto document is that ordinary text is separated from computer code, such as R code. When the document is created, that is, rendered, the computer code is run, and the output displayed in the document interwoven with the provided text. The following examples show how to construct this R code within the document in the context of creating dashboards.

```{r}
#| echo: false
#| message: false
library(lessR) 
style(suggest=FALSE, quiet=TRUE)   # turn off suggestions and text output 
style(lab_cex=1.6, axis_cex=1.3)   # enlarge axis labels and value labels
```

## Basic Dashboard

Template: `R_dashboard.qmd`

This dashboard consists of four figures, each generated as a data visualization from a corresponding chunk of R code.

![Standard R dashboard.](images/dashboardOut.png){#fig-dash width="600"}

Here is the Quarto (.qmd) file opened in RStudio with `Source` editing mode. When rendered, this text file generates the dashboard in @fig-dash.

![Quarto file in `Source` editing mode that generates the dashboard when rendered.](images/dashboardEG.png){#fig-input width="550"}

::: column-margin
Create other document types with other formats, including `html`, `docx`, `pptx`, and `pdf`.
:::

To create a dashboard, create a Quarto document with the `.qmd` file type. Begin your document with what is called `YAML`, basic document formatting instructions enclosed in a block of code within beginning and ending lines of three hyphes `---`. Specify the `format` as `dashboard`.

Next, to generate `lessR` visualizations, include the following three lines in the first chunk of R code.

::: setoff
suppressPackageStartupMessages(library(lessR))\
style(suggest=FALSE, quiet=TRUE)   # no suggestions and text output\
style(lab_cex=1.6, axis_cex=1.3)          # enlarge axis and value labels\
:::

Each of the four figures in the dashboard was created by running a single line of R code. To distinguish text from computer code, embed one or more lines of code as illustrated in @fig-input. The three single backlashes ```` ``` ```` opening and ending lines of each code chunk can be quickly generated by the CTRL+Alt+I or CMD+Option+I key combination, or, enter the information manually.

::: {.column-margin}
The lines of code in this example apply to R, but code from other languages, such as Python, can also be embedded.
:::

By default, construct your dashboard row-by-row. To create a dashboard row in your Quarto document, enter the word `Row` as a second-level header indicated by `##`followed by the specified height of the row.

To create a free-form dashboard, enter the instructions directly in `Source` edit mode, or do the following in `Visual` edit mode. Then, follow these four steps to create a row of information in your dashboard.

1.  Enter the word: Row

2.  Go to the Paragraph Style menu on the menu bar, right before the bullet point menu, usually with the word `Normal` displayed. Select `Header 2`. (Markdown code indicates a second-level header by two `#` signs preceding it.)

3.  Go to the `Format` menu and select `Edit Attributes...` at the bottom.

4.  Under the entry for `Other`, add `height=50%` or whatever percentage of the overall height of the window you wish for this row of the dashboard to occupy.

::: {.callout-tip title="Add content to the dashboard"}
Now that the row is declared, populate the row under the `Row` header by specifying one or more items to display on your dashboard.
:::

For example, if you place two items, such as two R code chunks for two data visualizations, under the row header you created, then that `Row` will contain two displays. If you have two rows, and two items under each row, then the dashboard will contain four separate displays as in @fig-dash.

## MarkDown Box Dashboard

Template: `R_MarkDownBox.qmd`

This dashboard consists of two data visualizations from the corresponding R code chunks and two free-form text boxes called MarkDown boxes. The purpose is to provide commentary and results regarding the displayed visualizations.

![R dark themed dashboard with Mark Down Boxes.](images/MarkDownBox.png){#fig-mdb width="600"}

Notice that R code can be embedded within the text, what is called inline code, by placing a code statement within <code>\`r    \`</code>. To compute a statistic such as the mean or the sum, need to incorporate `na.rm=TRUE` in the function call, which means to remove missing data, `NA`s, before computing the statistic. Otherwise, one missing data value causes the computation of the statistic to return `NA`, that is, missing.

![Quarto file in `Source` editing mode that generates the markdown dashboard when rendered.](images/cardinXY.png){#fig-cardIn width="550"}

To create a free-form markdown box dashboard, enter the instructions from the example within RStudio directly in `Source` edit mode with any needed modifications, or do the following in `Visual` edit mode.

1.  From the insert menu, select `Div…`.

2.  In the `Classes` field, enter `.card`.

3.  As an option, in the `Other` field, add a title with `title=xxx` where *xxx* is your title.

4.  In the gray box that appears, enter the content you wish to display, one or more sentences.

Place as many markdown boxes as you wish in the row, or interspersed with other content as well. The height of the markdown boxes and other content is determined by the height of the row set in the `Row` header. As always, move to `Source` mode to view the created markdown directly.
