input$plot_selected
input$plot_key_selected
input$plot_theme_selected4 Shiny and ggiraph reactive values
A major advantage of ‘ggiraph’ used in a shiny application is the ability to collect selections. The user defines them by clicking on the elements or by defining an area surrounding the elements to be selected (lasso selection).
It is also possible to deselect all or part of an existing selection.
Hover events can also be used as reactive values. Interactive features with shiny are available in the body of the graphic but also in the legend and the titles. The graphic is transformed into a reactive object and three sets of inputs (events and selections) are available, those of the panel elements, those of the legend elements and those of the theme elements.
4.1 Shiny usage
- add a placeholder for the ‘ggiraph’ graphic in the ui part with
girafeOutput("plot"); - add a render component in the server part with
renderGirafe(girafe(ggobj = ...)); - to use other fonts than the default ones, give a font set to
girafe(font_set = ), see Fonts management.
4.1.1 Selection as a reactive value
Three reactive values are available:
- the selection of the
data_idof the panel, selected elements will be captured in the input reactive valuegirafeid_selected(name of the input id of the reactive output value +_selected). - the selection of the
data_idof the legend elements, selected elements will be captured in the input reactive valuegirafeid_key_selected(name of the input id of the reactive output value +_key_selected). - the selection of the
data_idof the theme elements, selected elements will be captured in the input reactive valuegirafeid_theme_selected(name of the input id of the reactive output value +_theme_selected).
4.1.2 Update selected values from Shiny
You can modify selection values from the server by using the session$sendCustomMessage method with type girafeid_set (name of the input id of the reactive output value + _set).
# delete selections
session$sendCustomMessage(type = 'plot_set', message = character(0))
session$sendCustomMessage(type = 'plot_key_set', message = character(0))
session$sendCustomMessage(type = 'plot_theme_set', message = character(0))For applying arbitrary CSS classes (not selection/hover states), see also CSS class manipulation which provides girafe_class_add(), girafe_class_remove() and girafe_class_toggle().
4.1.3 Define default selection
Sometimes, it can be useful to pre-select some elements. This can be done by using the argument selected of function opts_selection (or opts_selection_key or opts_selection_theme).
library(ggiraph)
library(ggplot2)
dataset <- mtcars
dataset$carname <- row.names(dataset)
gg_scatter <- ggplot(
dataset,
aes(x = disp, y = qsec, tooltip = carname, data_id = carname, color= wt) ) +
geom_point_interactive(size=3)
preselection <- dataset$carname[1:5]
girafe(ggobj = gg_scatter,
options = list(
opts_selection(
selected = preselection,
type = "multiple",
only_shiny = FALSE
)
)
)Try it: five cars are selected at start, drawn in red. Click a point to add it to the selection, click a selected point to remove it. In a Shiny application, the selected cars would be in input$<id>_selected.
4.2 Examples
The package contains Shiny applications, launched in showcase mode (the code is shown next to the application) with run_girafe_example():
run_girafe_example("crimes")| Application | What it shows |
|---|---|
cars |
single selection of a point, shown in the page |
iris |
single selection of a group of points |
crimes |
multiple selection and hover as reactive values, selection reset from the server |
click_scale |
selection of the legend keys (input$plot_key_selected) |
linked_scale |
legend and geometries selected together (linked = TRUE) |
maps |
multiple selection of the countries of a map drawn with geom_sf_interactive() |
modal |
a selection that opens another graphic in a modal dialog |
DT |
onclick actions that filter a DT table on the client side |
css_class |
CSS classes added and removed from the server with girafe_class_add()… |
dynamic_ui |
girafe graphics created with renderUI() |
gender |
all the selection and hover inputs of the panel, the legend and the theme, and their update from the server |
4.3 Working with user selections
The graphics produced by girafe from a shiny application will allow you to retrieve the element selections made by users.
Elements associated with data_id can be selected and the selection (the data_id value) is available in the client and the server side of the application. The selected identifiers will be the values mapped by the aesthetic data_id.
The selection type can take several values: single, multiple or none.
single: the user can only select one element. The click allows its selection if it is not selected, or its de-selection if it is already selected. Clicking on an unselected element automatically de-selects the other selected element.multiple: the user can select several elements, by clicking on the elements or by selecting in the toolbar the “lasso selection” menu which allows you to draw a lasso on the graph and select all the elements contained in the lasso. The toolbar also contains an “anti-lasso selection” menu that allows you to draw a lasso on the graph and de-select all the elements contained in the lasso. The click is of course available for unit selections/de-selections.none: no selection is allowed in the graph produced bygirafe.
All these options can be configured with the following functions:
opts_selection: relative to panel selectionsopts_selection_key: relative to legend selectionsopts_selection_theme: relative to theme elements selections
The following code is enabling single selection in the panel:
library(ggiraph)
library(ggplot2)
dataset <- mtcars
dataset$carname <- row.names(dataset)
gg_scatter <- ggplot(dataset, aes(x = disp, y = qsec, tooltip = carname, data_id = carname, color= wt) ) +
geom_point_interactive(size=3)
girafe(ggobj = gg_scatter,
options = list(opts_selection(type = "single", only_shiny = FALSE)) )Try it: click a point, it is selected (red); click another one, it replaces the first: only one element can be selected.
Note that we used only_shiny = FALSE so that selections can be seen in this book but in real shiny applications, you don’t have to set this argument to FALSE.
4.3.1 Linked selection between legend and geometries
Setting linked = TRUE in opts_selection() links panel and legend selections: clicking a legend key selects the corresponding geometries, and clicking a geometry selects the corresponding legend key.
library(ggiraph)
library(ggplot2)
gg_bar <- ggplot(
data = diamonds,
mapping = aes(x = color, fill = cut, data_id = cut)) +
geom_bar_interactive(
aes(tooltip = sprintf("%s: %.0f", fill, after_stat(count)))
) +
scale_fill_discrete_interactive(
data_id = function(breaks) breaks,
tooltip = function(breaks) breaks
)
girafe(ggobj = gg_bar,
options = list(
opts_hover(css = "transition: opacity 1s;fill:black;", linked = TRUE),
opts_hover_key(css = "transition: opacity 1s;fill:black;"),
opts_hover_inv(css = "transition: opacity 1s;opacity:0.5;"),
opts_selection(type = "multiple", only_shiny = FALSE, linked = TRUE),
opts_selection_key(type = "multiple", only_shiny = FALSE)
))Try it: click a key of the legend, the key and all the bars of its cut are selected (red); click a bar of another cut, it is added to the selection with its key.
This also works with opts_hover(linked = TRUE) for hover effects (see What can be interactive).
4.4 CSS class manipulation
Functions girafe_class_add(), girafe_class_remove() and girafe_class_toggle() let you programmatically add, remove or toggle arbitrary CSS classes on SVG elements from the Shiny server. Elements are targeted by their data_id, key_id or theme_id values.
This is useful when you want to highlight or style specific elements in response to external events (button clicks, other inputs, etc.) without relying on the built-in hover/selection mechanism.
CSS class definitions are your responsibility, ggiraph only adds/removes/toggles the class name on the targeted SVG elements.
# In the UI, define the CSS class:
tags$style(HTML(".highlighted { fill: red !important; }"))
# In the server, manipulate the class:
girafe_class_add(session, "plot", "highlighted", data_id = c("id_1", "id_2"))
girafe_class_remove(session, "plot", "highlighted", data_id = "id_1")
girafe_class_toggle(session, "plot", "highlighted", data_id = "id_2")The first argument is the Shiny session, the second is the outputId used in girafeOutput(), and the third is the CSS class name. Then provide at least one of data_id, key_id or theme_id to target the elements.
A demo application is available:
run_girafe_example("css_class")