Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use D3 and Shiny to implement `identify()` in R

Try this gallery item. It uses ggvis to accomplish this goal in shiny. In case the gallery disappears, here is some minimal code that will produce a tooltip, similar to identify(), using ggvis.

require(ggvis)
mtcars$model<-rownames(mtcars)
mtcars %>% ggvis(~wt, ~mpg,key:=~model) %>% 
  layer_points() %>% 
  add_tooltip(function(df) df$model)

And, a more complete, but still minimal example:

require(shiny)
require(ggvis)
mtcars$model<-rownames(mtcars)

shinyApp(
  ui = fluidPage(
    sidebarLayout(
      sidebarPanel(h2("GGVis to Identify Points")),
      mainPanel(ggvisOutput("carsplot"))
    )
  ), 
  server = function(input, output) {
    vis <- reactive({ 
      mtcars %>% ggvis(~wt, ~mpg,key:=~model) %>% 
        layer_points() %>% 
        add_tooltip(function(df) df$model)
    })
    vis %>% bind_shiny("carsplot")
  }

)