Using the downloadablePlot Shiny Module

Dr. Connie Brett

2022-01-03

Overview

Purpose

This Shiny Module was created in order to provide an easy-to-use downloadFileButton for a plot that is automatically created, linked and managed. This module is compatible with ggplot2, grob and lattice produced graphics as well as any superclass of these.

Features


Usage

Shiny Module Overview

Shiny modules consist of a pair of functions that modularize, or package, a small piece of reusable functionality. The UI function is called directly by the user to place the UI in the correct location (as with other shiny UI objects). The module server function that is called only once to set it up using the module name as a function inside the server function (i.e. user-local session scope. The function first arguments is string represents the module id (the same id used in module UI function). Additional arguments can be supplied by the user based on the specific shiny module that is called. There can be additional helper functions that are a part of a shiny module.

downloadablePlotUI

The downloadablePlotUI function is called from the ui.R (or equivalent) file in the location where the plot should be placed. This is similar to other UI element placement in shiny.

The downloadablePlotUI looks like:

The downloadablePlotUI function takes the unique object ID for the UI object. The next two arguments (downloadtypes and download_hovertext) are passed to the downloadFileButton and set the file types the button will allow the user to request and the downloadFileButton’s tooltip text. The next two options (width, height) set the plot size in the UI and can be any css-recognizable size value. These options are followed with alignment and overlap options. The last three options give you the standard ways of interacting with shiny plots - using the functionality available with the clickOpts, hoverOpts and brushOpts functions. When you create an object using any of these functions shiny will send information about the user’s interactions with the plot to the server and make it available to you via an input$ variable.

# Inside ui_body.R or ui_sidebar.R

downloadablePlotUI("object_id1", 
                   downloadtypes = c("png", "csv"), 
                   download_hovertext = "Download the plot and data here!",
                   height = "500px", 
                   btn_halign = "left")

downloadablePlot

The downloadablePlot function is also called directly. The call consists of the following:

Download Function Requirements

# Inside server_local.R

downloadablePlot("object_id1",
                 logger = ss_userAction.Log,
                 filenameroot = "mydownload1",
                 aspectratio = 1.33,
                 downloadfxns = list(png = myplotfxn, tsv = mydatafxn),
                 visibleplot = myplotfxn)


Additional Resources

Vignettes