4 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. Interactive feature with shiny are all 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 set 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
4.1.1 Selection as a reactive value
Three reactive values are available:
- the selection of the
data_id
of 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_id
of 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_id
of 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
).
$plot_selected
input$plot_key_selected
input$plot_theme_selected input
4.1.2 Update selected values from Shiny
You can also modify theses values by using the session$sendCustomMessage
method with type girafeid_set
(name of the input id of the reactive output value + _set
).
# delete selections
$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)) session
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)
<- mtcars
dataset $carname <- row.names(dataset)
dataset<- ggplot(
gg_scatter
dataset, aes(x = disp, y = qsec, tooltip = carname, data_id = carname, color= wt) ) +
geom_point_interactive(size=3)
<- dataset$carname[1:5]
preselection girafe(ggobj = gg_scatter,
options = list(
opts_selection(
selected = preselection,
type = "multiple",
only_shiny = FALSE
)
) )
4.2 Examples
The package contains Shiny examples available in the shiny
directory of the package (system.file("shiny", package = "ggiraph")
).
Use function run_girafe_example
to launch the applications in showcase mode. They can be used to better understand how to use girafe graphics with shiny applications.
The following applications are available: cars
, click_scale
, crimes
, DT
, dynamic_ui
, iris
, gender
, maps
and modal
.
4.2.1 Panel selections usage (server side)
run_girafe_example("crimes")
4.2.2 Legend selections usage (server side)
run_girafe_example("click_scale")
4.2.3 onclick actions (client side).
run_girafe_example("DT")
4.2.4 Girafe reactivity showoff
run_girafe_example("gender")
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. He can do this 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)
<- mtcars
dataset $carname <- row.names(dataset)
dataset<- ggplot(dataset, aes(x = disp, y = qsec, tooltip = carname, data_id = carname, color= wt) ) +
gg_scatter geom_point_interactive(size=3)
girafe(ggobj = gg_scatter,
options = list(opts_selection(type = "single", only_shiny = FALSE)) )
Note that we used only_shiny = FALSE
so that selections can be seen in the vignette but in real shiny applications, you don’t have to set this argument to FALSE.