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.
You will need to use
girafeOutput in the UI part of the application and
renderGirafe in the SERVER part.
Instead of a
plotOutput, use function
The package contains Shiny examples available in the
shiny directory of the package (
system.file("shiny", package = "ggiraph")).
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:
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
The selection type can take several values:
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 by
All these options can be configured with the following functions:
opts_selection: relative to panel selections
opts_selection_key: relative to legend selections
opts_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)) )
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.
Three reactive values are available:
data_idof the panel, selected elements will be captured in the input reactive value
girafeid_selected(name of the input id of the reactive output value +
data_idof the legend elements, selected elements will be captured in the input reactive value
girafeid_key_selected(name of the input id of the reactive output value +
data_idof the theme elements, selected elements will be captured in the input reactive value
girafeid_theme_selected(name of the input id of the reactive output value +
input$plot_selected input$plot_key_selected input$plot_theme_selected
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 +
Sometimes, it can be useful to pre-select some elements. This can be done by using the argument
selected of function