The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.

Using the downloadableTable Shiny Module

Dr. Connie Brett

2023-11-06

Overview

Purpose

This Shiny Module was created in order to provide a consistent-looking and easy-to-use table including a downloadFileButton that is automatically created, linked and managed.

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.

The downloadableTable Shiny Module is a part of the periscope package and consists of the following functions:

downloadableTableUI

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

The downloadableTableUI looks like:

The downloadableTableUI function takes the unique object ID for the UI object. The next two arguments (downloadtypes and 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 contentHeight argument is optional but important - it sets the viewable height of the table content which can be any css-recognizable size value.

downloadableTables are multi-select by default, however if you need a table to only allow single row selections you can set the singleSelect argument to TRUE.

# Inside ui_body.R or ui_sidebar.R

downloadableTableUI("object_id1", 
                    downloadtypes = c("csv", "tsv"), 
                    hovertext = "Download the data here!",
                    contentHeight = "300px",
                    singleSelect = FALSE)

downloadableTable

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

Data Function Requirements

Reactive Return Value

The server function returns a reactive expression containing the selected rows (data, not references, rownumbers, etc - the actual table row data). This allows the user to capture this to update another table, chart, etc. as desired. It is acceptable to ignore the return value as well if this functionality is not needed.

Customization Options

downloadableTable module can be customized using the same ?DT::datatable arguments. options or format functions. These options can be sent as a named options via the server function, see example below. Notes:

The following is an example of a customized downloadableTable:

It is generated using the following code:

# Inside server_local.R
sketch <- htmltools::withTags(table(
    class = "display",
    thead(
        tr(
            th(rowspan = 2, "Location"),
            th(colspan = 2, "Statistics")
        ),
        tr(
            th("Change"),
            th("Increase")
        )
        
    )
))

selectedrows <- downloadableTable("exampleDT1",
                                  ss_userAction.Log,
                                  "exampletable",
                                  list(csv = load_data3, tsv = load_data3),
                                  load_data3,
                                  colnames = c("Area", "Delta", "Increase"),
                                  filter = "bottom",
                                  callback = htmlwidgets::JS("table.order([1, 'asc']).draw();"),
                                  container = sketch,
                                  formatStyle = list(columns = c("Total.Population.Change"),   
                                                     color = DT::styleInterval(0, c("red", "green"))),
                                  formatStyle = list(columns = c("Natural.Increase"),   
                                                     backgroundColor = DT::styleInterval(
                                                         c(7614, 15914, 34152),
                                                         c("blue", "lightblue", "#FF7F7F", "red"))))

# NOTE: selectedrows is the reactive return value, captured for later use

Sample Application

For a complete running shiny example application using the downloadableTable module you can create and run a periscope sample application using:

library(periscope)

app_dir = tempdir()
create_new_application('mysampleapp', location = app_dir, sampleapp = TRUE)
runApp(paste(app_dir, 'mysampleapp', sep = .Platform$file.sep))


Additional Resources

Vignettes

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.