0

I've build a shiny app with an interactive legend using the scale_fill_manual_interactive function from the beautiful ggiraph package. However, I would like to print the information(male or female, depending on selection) from the onclick event to console. Here is the code snippet.

library(ggplot2)
library(ggiraph)

dat <- data.frame(
  name = c( "Guy", "Ginette", "David", "Cedric", "Frederic" ),
  gender = c( "Male", "Female", "Male", "Male", "Male" ),
  height = c(169, 160, 171, 172, 171 ) )

p <- ggplot(dat, aes( x = name, y = height, fill = gender,
                      data_id = name ) ) +
  geom_bar_interactive(stat = "identity")

p3 <- p +
  scale_fill_manual_interactive(
    name = label_interactive("gender", tooltip="Gender levels", data_id="legend.title"),
    values = c(Male = "#0072B2", Female = "#009E73"),
    data_id = function(breaks) { as.character(breaks)},
    tooltip = function(breaks) { as.character(breaks)},
    onclick = function(breaks) { cat( as.character(breaks)) }
  )
x <- girafe(ggobj = p3)
x <- girafe_options(x,
                    opts_hover_key(girafe_css("stroke:red", text="stroke:none;fill:red")))
if (interactive()) print(x)

now it only prints the entire list

Female Male

instead of only the one selected.

Kiran Maniya
  • 8,453
  • 9
  • 58
  • 81

2 Answers2

0

The onclick element is expecting a javascript instructions. It also means that this will live in your browser but not in R, the following is customizing the onclick event so that the browser console get a print instruction when an element is clicked:

library(ggplot2)
library(ggiraph)

dat <- data.frame(
  name = c( "Guy", "Ginette", "David", "Cedric", "Frederic" ),
  gender = c( "Male", "Female", "Male", "Male", "Male" ),
  height = c(169, 160, 171, 172, 171 ) )

p <- ggplot(dat, aes( x = name, y = height, fill = gender,
                      data_id = name ) ) +
  geom_bar_interactive(stat = "identity")

p3 <- p +
  scale_fill_manual_interactive(
    name = label_interactive("gender", tooltip="Gender levels", data_id="legend.title"),
    values = c(Male = "#0072B2", Female = "#009E73"),
    data_id = function(breaks) { as.character(breaks)},
    tooltip = function(breaks) { as.character(breaks)},
    onclick = function(breaks) { sprintf("console.log(\"%s\")", breaks ) }
  )
x <- girafe(ggobj = p3)
x <- girafe_options(x,
                    opts_hover_key(girafe_css("stroke:red", text="stroke:none;fill:red")))
if (interactive()) print(x)

enter image description here

David Gohel
  • 9,180
  • 2
  • 16
  • 34
  • Thank you for the awser and the amazing package. I guess I need to learn JS. – Wesley Rademaker Sep 30 '19 at 17:37
  • It depends :) I was not sure you wanted that in the browser console. Did you want the click to print in R console or browser console? If you want to know which key has been clicked in shiny, there is a reactive value available (`plotid_key_selected`) – David Gohel Sep 30 '19 at 19:07
  • I am sorry for not being clear, I meant in R console, i would like to use the value I selected in shiny to calculate a different parameter. – Wesley Rademaker Sep 30 '19 at 19:58
  • OK, no problem. I will post another answer – David Gohel Sep 30 '19 at 20:07
0

This should answer your question. It will print in the R console the last clicked key. A reactive value is available to get clicked key, its ID is plot_key_selected: the ggiraph ID + _key_selected

library(shiny)
library(ggplot2)
library(ggiraph)

gg <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
    geom_point_interactive( size = 3 ) + theme_minimal() +
    scale_colour_manual_interactive(
        data_id = c("setosa" = "setosa", "versicolor" = "versicolor", "virginica" = "virginica"),
        tooltip = c("setosa" = "setosa", "versicolor" = "versicolor", "virginica" = "virginica"),
        values = c("setosa" = "purple", "versicolor" = "blue", "virginica" = "darkgreen")
        )

ui <- fluidPage(

    sidebarLayout(
        sidebarPanel(
        ),
        mainPanel(
            ggiraph::ggiraphOutput("plot")
        )
    )
)

server <- function(input, output) {
    output$plot <- renderggiraph({
        x <- girafe(code = print(gg), width_svg = 6, height_svg = 8)
        x
    })
    observe({
        print(input$plot_key_selected)
    })
}

shinyApp(ui = ui, server = server)
David Gohel
  • 9,180
  • 2
  • 16
  • 34