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.

Shiny usage

You will need to use girafeOutput in the UI part of the application and renderGirafe in the SERVER part.

The client ui.R

Instead of a plotOutput, use function girafeOutput.

girafeOutput("plot")

The server server.R

Use function renderGirafe.

output$plot <- renderGirafe({
    girafe(ggobj = gg_blahblah )
  })

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.

Panel selections usage (server side)

Legend selections usage (server side)

run_girafe_example("click_scale")

onclick actions (client side).

Girafe reactivity showoff

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 by girafe.

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.

Access the selected values

Three reactive values are available:

  • the selection of the data_id of the panel, selected elements will be captured in the input reactive value girafeid_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 value girafeid_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 value girafeid_theme_selected (name of the input id of the reactive output value + _theme_selected).
input$plot_selected
input$plot_key_selected
input$plot_theme_selected

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
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))

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).

preselection <- dataset$carname[1:5]
girafe(ggobj = gg_scatter, 
  options = list(
    opts_selection(selected = preselection,
                   type = "multiple", only_shiny = FALSE,
                   css = "fill:red;stroke:gray;")))