Previous chapter
DashboardsFlexdashboard
Next chapter

flexdashboard

Flexdashboards are based on RMarkdown (.Rmd) files and thus require some initial knowledge of RMarkdown.

Note: If you are not already familiar with RMarkdown please read through the materials below:

Please also refer to https://rmarkdown.rstudio.com/flexdashboard for the flexdashboard documentation and further examples.

To create a flexdashboard within RStudio make sure, that the package flexdashboard is installed. Next, we we create a new flexdashboard template.

Exercise: Create First Dashboard

Now let’s create our first Flexdashboard using a template.

In RStudio: File -> New file -> R Markdown -> From Template

You can now create three plots that go in each of the panes using built-in R datasets library (help="datasets").

Next Steps

  1. Try to first run the exercise above based on the specifications ON YOUR OWN.
  2. There is no clear solution for this exercise. Try to implement the steps as you prefer and consult the documentation if running into problems.

Exercise: Changing Dashboard

Download and inspect the file flexdashboard_01.Rmd.

  1. How is it different than Shiny apps we have been building so far, how is it similar?
  2. Make a change to the layout of the dashboard, see http://rmarkdown.rstudio.com/flexdashboard/using.html#layout for help
  3. Change the theme of the dashboard, see http://rmarkdown.rstudio.com/flexdashboard/using.html#appearance for help

Next Steps

  1. Try to first run the exercise above based on the specifications ON YOUR OWN.
  2. There is no clear solution for this exercise. Try to implement the steps as you prefer and consult the documentation if running into problems.

Shiny Documents

To turn a static flex dashboard into a dynamic one you just follow the steps outlined below:

  • Add runtime: shiny to the YAML header.
  • Add inputs in code chunks.
  • Add reactive expressions.
  • Add renderXyz functions in code chunks.
  • No need for output$x <- assignment, or for xyzOutput functions.

Exercise: Shiny Documents

  1. Now, let’s turn our static flexdashboard from the previous exercise (Exercise: Changing Dashboard) into a dynamic Shiny app.
  2. Next, add another UI widget, a radioButton, that allows the user to select whether the plot used to visualize the distribution of weight should be histogram or a violin plot

Next Steps

  1. Try to first run the exercise above based on the specifications ON YOUR OWN.
  2. Only as a last resort check out the solution below.

Solution: Shiny Documents

Sample solution download here

Shiny Pre-rendered

The traditional way to add Shiny components to an R Markdown document is through the use of runtime: shiny. This method provides a very straightforward development experience (you can use Shiny UI and server functions anywhere you like within the document). However, because it requires a full document render for each end user browser session it can perform poorly for documents that don’t render quickly.

The most important drawbacks of traditional Shiny documents are:

  • Start-up time: knits document every time someone visits it
  • Resizing can trigger re-knit
  • Auto-reconnection doesn’t work (i.e. client browsers cannot automatically reconnect afer being disconnected due to network problems)

Shiny Pre-rendered Sample

## ---
## title: "Hello Prerendered Shiny"
## output: html_document
## ---
##  ``` 
## {r, context="render", echo=FALSE}
##   sliderInput("bins", "Number of bins:", min = 1, max = 50, value = 30)
##   plotOutput("distPlot")
##  ``` 
## {r, context="server"}
##   output$distPlot <- renderPlot({
##   x <- faithful[, 2] # Old Faithful Geyser data
##   bins <- seq(min(x), max(x), length.out = input$bins + 1)
##   hist(x, breaks = bins, col = "darkgray", border = "white")
## })
##  ```

The most noticable differences to a traditional Shiny document are:

  1. output$x <- assignment and xyzOutput functions are required
  2. context="render" as additional knitr option in the UI chunk
  3. context="server" as additional knitr option in the Server chunk

Contexts for Shiny Prerendered

  • "render": Runs in rendering phase (like ui)
  • "server": Runs in serving phase (like server)

  • Additional contexts:
  • "setup": Runs in both phases (like global.R)
  • "data": Runs in rendering phase (any variables are saved to a file, and available to serving phase, useful for data preprocessing)
  • "server-start": Runs once in serving phase, when the Shiny document is first run and is not re-executed for each new user of the document, appropriate for
  • establishing shared connections to remote servers (e.g. databases, Spark contexts, etc.)
  • creating reactive values/expressions to be shared across sessions (e.g. with reactivePoll, reactiveFileReader)

Exercise: Shiny Prerendered

Start with the app from the previous exercise Exercise: Shiny Documents.

  • Note: You will need to use output$x <- assignment and xyzOutput functions

Next Steps

  1. Try to first run the exercise above based on the specifications ON YOUR OWN.
  2. Only as a last resort check out the solution below.

Solution: Shiny Documents

Sample solution download here