It requires minimal non-standard R coding - with very little practice you can quickly create a dashboard
+
It requires minimal non-standard R coding - with very little practice you can quickly create a dashboard.
-
The dashboard can usually be emailed to colleagues as a self-contained HTML file - no server required
+
The dashboard can usually be emailed to colleagues as a self-contained HTML file - no server required.
-
You can combine flexdashboard with shiny, ggplotly, and other “html widgets” to add interactivity
+
You can combine flexdashboard with shiny, ggplotly, and other “html widgets” to add interactivity.
Disadvantages of flexdashboard:
-
Less customization as compared to using shiny alone to create a dashboard
+
Less customization as compared to using shiny alone to create a dashboard.
-
Very comprehensive tutorials on using flexdashboard that informed this page can be found in the Resources section. Below we describe the core features and give an example of building a dashboard to explore an outbreak, using the case linelist data.
+
Very comprehensive tutorials on using flexdashboard that informed this page can be found in the Resources section. Below we describe the core features and give an example of building a dashboard to explore an outbreak, using the case linelist data.
42.1 Preparation
@@ -778,14 +851,23 @@
Load packages
Import data
We import the dataset of cases from a simulated Ebola epidemic. If you want to follow along, click to download the “clean” linelist (as .rds file). Import data with the import() function from the rio package (it handles many file types like .xlsx, .csv, .rds - see the Import and export page for details).
-
# import the linelist
-linelist <-import("linelist_cleaned.rds")
+
+
Warning: The `trust` argument of `import()` should be explicit for serialization formats
+as of rio 1.0.3.
+ℹ Missing `trust` will be set to FALSE by default for RDS in 2.0.0.
+ℹ The deprecated feature was likely used in the rio package.
+ Please report the issue at <https://github.com/gesistsa/rio/issues>.
+
+
+
+
# import the linelist
+linelist <-import("linelist_cleaned.rds")
The first 50 rows of the linelist are displayed below.
-
-
+
+
@@ -821,20 +903,20 @@
YAML
At the top of the script is the “YAML” header. This must begin with three dashes --- and must close with three dashes ---. YAML parameters comes in key:value pairs. The indentation and placement of colons in YAML is important - the key:value pairs are separated by colons (not equals signs!).
The YAML should begin with metadata for the document. The order of these primary YAML parameters (not indented) does not matter. For example:
You can use R code in YAML values by putting it like in-line code (preceeded by r within backticks) but also within quotes (see above for Date).
A required YAML parameter is output:, which specifies the type of file to be produced (e.g. html_document, pdf_document, word_document, or powerpoint_presentation). For flexdashboard this parameter value is a bit confusing - it must be set as output:flexdashboard::flex_dashboard. Note the single and double colons, and the underscore. This YAML output parameter is often followed by an additional colon and indented sub-parameters (see orientation: and vertical_layout: parameters below).
As shown above, indentations (2 spaces) are used for sub-parameters. In this case, do not forget to put an additional colon after the primary, like key:value:.
If appropriate, logical values should be given in YAML in lowercase (true, false, null). If a colon is part of your value (e.g. in the title) put the value in quotes. See the examples in sections below.
@@ -853,11 +935,11 @@
Narrative text
Headings
Different heading levels are established with different numbers of hash symbols, as described in the Reports with R Markdown page.
In flexdashboard, a primary heading (#) creates a “page” of the dashboard. Second-level headings (##) create a column or a row depending on your orientation: parameter (see details below). Third-level headings (###) create panels for plots, charts, tables, text, etc.
-
# First-level heading (page)
-
-## Second level heading (row or column)
-
-### Third-level heading (pane for plot, chart, etc.)
+
# First-level heading (page)
+
+## Second level heading (row or column)
+
+### Third-level heading (pane for plot, chart, etc.)
@@ -1062,10 +1144,10 @@
Tables
If you want to show a dynamic table that allows the user to filter, sort, and/or click through “pages” of the data frame, use the package DT and it’s function datatable(), as in the code below.
The example code below, the data frame linelist is printed. You can set rownames = FALSE to conserve horizontal space, and filter = "top" to have filters on top of every column. A list of other specifications can be provided to options =. Below, we set pageLength = so that 5 rows appear and scrollX = so the user can use a scroll bar on the bottom to scroll horizontally. The argument class = 'white-space: nowrap' ensures that each row is only one line (not multiple lines). You can read about other possible arguments and values here or by entering ?datatable
You can also pass a standard ggplot or other plot object to ggplotly() from the plotly package (see the Interactive plots page). This will make your plot interactive, allow the reader to “zoom in”, and show-on-hover the value of every data point (in this scenario the number of cases per week and age group in the curve).
-
age_outbreak <-incidence(linelist, date_onset, "week", groups = age_cat)
-plot(age_outbreak, fill = age_cat, col_pal = muted, title ="") %>%
- plotly::ggplotly()
+
age_outbreak <-incidence(linelist, date_onset, "week", groups = age_cat)
+plot(age_outbreak, fill = age_cat, col_pal = muted, title ="") %>%
+ plotly::ggplotly()
Here is what this looks like in the dashboard (gif). This interactive functionality will still work even if you email the dashboard as a static file (not online on a server).
@@ -1166,14 +1248,14 @@
Settings
Enable shiny in a flexdashboard by adding the YAML parameter runtime: shiny at the same indentation level as output:, as below:
It is also convenient to enable a “side bar” to hold the shiny input widgets that will collect information from the user. As explained above, create a column and indicate the {.sidebar} option to create a side bar on the left side. You can add text and R chunks containing the shinyinput commands within this column.
If your app/dashboard is hosted on a server and may have multiple simultaneous users, name the first R code chunk as global. Include the commands to import/load your data in this chunk. This special named chunk is treated differently, and the data imported within it are only imported once (not continuously) and are available for all users. This improves the start-up speed of the app.
@@ -1476,6 +1558,24 @@
{
+ return filterRegex.test(href) || localhostRegex.test(href) || mailtoRegex.test(href);
+ }
+ // Inspect non-navigation links and adorn them if external
+ var links = window.document.querySelectorAll('a[href]:not(.nav-link):not(.navbar-brand):not(.toc-action):not(.sidebar-link):not(.sidebar-item-toggle):not(.pagination-link):not(.no-external):not([aria-hidden]):not(.dropdown-item):not(.quarto-navigation-tool)');
+ for (var i=0; i