manipulateWidget: Add Controls to Interactive Plots

Description Usage Arguments Value Advanced Usage Modify the returned widget Examples

View source: R/manipulate_widget.R

Description

This function permits to add controls to an interactive plot created with packages like dygraphs, highcharter or plotly in order to change the input data or the parameters of the plot.

Technically, the function starts a shiny gadget. The R session is bloqued until the user clicks on "cancel" or "done". If he clicks on "done", then the the function returns the last displayed plot so the user can modify it and/or save it.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
manipulateWidget(
  .expr,
  ...,
  .updateBtn = FALSE,
  .saveBtn = TRUE,
  .exportBtn = TRUE,
  .exportType = c("html2canvas", "webshot"),
  .viewer = c("pane", "window", "browser"),
  .compare = NULL,
  .compareOpts = compareOptions(),
  .translations = mwTranslations(),
  .return = function(widget, envs) {     widget },
  .width = NULL,
  .height = NULL,
  .runApp = TRUE
)

Arguments

.expr

expression to evaluate that returns an interactive plot of class htmlwidget. This expression is re-evaluated each time a control is modified.

...

One or more named control arguments created with functions mwSlider, mwText, etc. The name of each control is the name of the variable the controls modifies in the expression. One can also create a group of inputs by passing a list of such control arguments. for instance mygroup = list(txt = mwText(""), nb = mwNumeric(0)) creates a group of inputs named mygroup with two inputs named "txt" and "nb".

.updateBtn

Should an update button be added to the controls ? If TRUE, then the graphic is updated only when the user clicks on the update button.

.saveBtn

Should an save button be added to the controls ? For saving output as html. Does not work in RStudio Viewer

.exportBtn

Should an export button be added to the controls ? For saving output as png. Does not work in RStudio Viewer

.exportType

.exportBtn, using html2canvas (default) and keeping current zoom, ... or using webshot

.viewer

Controls where the gadget should be displayed. "pane" corresponds to the Rstudio viewer, "window" to a dialog window, and "browser" to an external web browser.

.compare

Sometimes one wants to compare the same chart but with two different sets of parameters. This is the purpose of this argument. It can be a character vector of input names or a named list whose names are the names of the inputs that should vary between the two charts. Each element of the list must be a vector or a list of length equal to the number of charts with the initial values of the corresponding parameter for each chart. It can also be NULL. In this case, the parameter is initialized with the default value for the two charts.

.compareOpts

List of options created compareOptions. These options indicate the number of charts to create and their disposition.

.translations

List of translation strings created with function mwTranslations. Used to translate UI titles and labels.

.return

A function that can be used to modify the output of manipulateWidget. It must take two parameters: the first one is the final widget, the second one is a list of environments containing the input values of each individual widget. The length of this list is one if .compare is null, two or more if it has been defined.

.width

Width of the UI. Used only on Rmarkdown documents with option runtime: shiny.

.height

Height of the UI. Used only on Rmarkdown documents with option runtime: shiny.

.runApp

(advanced usage) If true, a shiny gadget is started. If false, the function returns a MWController object. This object can be used to check with command line instructions the behavior of the application. (See help page of MWController). Notice that this parameter is always false in a non-interactive session (for instance when running tests of a package).

Value

The result of the expression evaluated with the last values of the controls. It should be an object of class htmlWidget.

Advanced Usage

The "normal" use of the function is to provide an expression that always return an htmlwidget. In such case, every time the user changes the value of an input, the current widget is destroyed and a new one is created and rendered.

Some packages provide functions to update a widget that has already been rendered. This is the case for instance for package leaflet with the function leafletProxy. To use such functions, manipulateWidget evaluates the parameter .expr with four extra variables:

You can take a look at the last example to see how to use these two variables to update a leaflet widget.

Modify the returned widget

In some specific situations, a developer may want to use manipulateWidget in a function that waits the user to click on the "Done" button and modifies the widget returned by manipulateWidget. In such situation, parameter .return should be used so that manipulateWidget is the last function called. Indeed, if other code is present after, the custom function will act very weird in a Rmarkdown document with "runtime: shiny".

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Basic example with fake data
if (require(dygraphs)) {
  mydata <- data.frame(period = 1:100, value = rnorm(100))
  manipulateWidget(dygraph(mydata[range[1]:range[2], ], main = title),
                   range = mwSlider(1, 100, c(1, 100)),
                   title = mwText("Fictive time series"))
}

# Let use manipulateWidget to explore the evolution of energy consumption in
# the world
data("worldEnergyUse")

if (require(plotly)) {
  # Function that generates a chart representing the evolution of energy
  # consumption per country. Creating a function is not necessary. We do it
  # for clarity and reuse in the different examples.
  plotEnergyUse <- function(Country, Period, lwd = 2, col = "gray") {
    dataset <- subset(
      worldEnergyUse,
      country == Country & year >= Period[1] & year <= Period[2]
    )
    plot_ly(dataset) %>%
      add_lines(~year, ~energy_used, line = list(width = lwd, color = col)) %>%
      layout(title = paste("Energy used in", Country))
  }

  # Launch the interactive visualisation
  manipulateWidget(
    plotEnergyUse(Country, Period),
    Period = mwSlider(1960, 2014, c(1960, 2014)),
    Country = mwSelect(sort(unique(worldEnergyUse$country)), "United States")
  )

  # Directly start comparison mode
  manipulateWidget(
    plotEnergyUse(Country, Period),
    Period = mwSlider(1960, 2014, c(1960, 2014)),
    Country = mwSelect(sort(unique(worldEnergyUse$country))),
    .compare = list(Country = c("United States", "China")),
    .compareOpts = compareOptions(ncol = 2)
  )

  # Dynamic input parameters
  #-------------------------
  # The arguments of an input can depend on the values of other inputs.
  # In this example, when the user changes the region, the choices of input
  # "Country" are updated with the countries of that region.

  # First we create a list that contains for each region the countries in that
  # retion
  refRegions <- by(worldEnergyUse$country, worldEnergyUse$region,
                   function(x) as.character(sort(unique(x))))

  manipulateWidget(
    plotEnergyUse(Country, Period),
    Period = mwSlider(1960, 2014, c(1960, 2014)),
    Region = mwSelect(sort(unique(worldEnergyUse$region))),
    Country = mwSelect(choices = refRegions[[Region]])
  )

  # Grouping inputs
  #----------------
  # Inputs can be visually grouped with function mwGroup()
  manipulateWidget(
    plotEnergyUse(Country, Period, lwd, col),
    Period = mwSlider(1960, 2014, c(1960, 2014)),
    Country = mwSelect(sort(unique(worldEnergyUse$country)), "United States"),
    `Graphical Parameters` = mwGroup(
      lwd = mwSlider(1,10, 2, label = "Line Width"),
      col = mwSelect(choices = c("gray", "black", "red"))
    )
  )

  # Conditional inputs
  #-------------------
  # Inputs can be displayed or hidden depending on the state of other inputs.
  # In this example, user can choose to display the level of aggregation
  # (region or country). Depending on the choixe, the application displays
  # input Region or input Country.
  plotEnergyUseRegion <- function(Region, Period, lwd = 2, col = "gray") {
    dataset <- subset(
      worldEnergyUse,
      region == Region & year >= Period[1] & year <= Period[2]
    )
    dataset <- aggregate(energy_used ~ year, sum, data = dataset)

    plot_ly(dataset) %>%
      add_lines(~year, ~energy_used, line = list(width = lwd, color = col)) %>%
      layout(title = paste("Energy used in", Region))
  }

  manipulateWidget(
    {
      if (Level == "Region") {
        plotEnergyUseRegion(Region, Period)
      } else {
        plotEnergyUse(Country, Period)
      }
    },
    Period = mwSlider(1960, 2014, c(1960, 2014)),
    Level = mwSelect(c("Region", "Country")),
    Region = mwSelect(sort(unique(worldEnergyUse$region)),
                      .display = Level == "Region"),
    Country = mwSelect(sort(unique(worldEnergyUse$country)),
                       .display = Level == "Country")
  )

}

# Advanced Usage
# --------------
# When .expr is evaluated with tehnical variables:
# .initial: is it the first evaluation?
# .outputId: integer representing the id of the chart
# .output: shiny output id
# .session: shiny session
# They can be used to update an already rendered widget instead of replacing
# it each time an input value is modified.
#
# In this example, we represent on a map, the energy use of countries.
# When the user changes an input, the map is not redrawn. Only the circle
# markers are updated.
if (require(leaflet)) {
  plotMap <- function(Year, MaxRadius = 30, .initial, .session, .output) {
    dataset <- subset(worldEnergyUse, year == Year)
    radius <- sqrt(dataset$energy_used) /
      max(sqrt(worldEnergyUse$energy_used), na.rm = TRUE) * MaxRadius

    if (.initial) { # map has not been rendered yet
      map <- leaflet() %>% addTiles()
    } else { # map already rendered
      map <- leafletProxy(.output, .session) %>% clearMarkers()
    }

    map %>% addCircleMarkers(dataset$long, dataset$lat, radius = radius,
                             color = "gray", weight = 0, fillOpacity = 0.7)
  }

  manipulateWidget(
    plotMap(Year, MaxRadius, .initial, .session, .output),
    Year = mwSlider(1960, 2014, 2014),
    MaxRadius = mwSlider(10, 50, 20)
  )
}

manipulateWidget documentation built on Oct. 5, 2021, 9:10 a.m.