Ziyuan
Ziyuan

Reputation: 4558

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

I asked a question on how to plot dynamically according to user interactions, whose solution works quite well on my machine.

Now I want to make an on-line version and host it with Shiny.

I have tried to put the code into server.R and invoke the iden() function inside reactivePlot(), but the part of identify() does not take effect.

So, any hints on this task?

Upvotes: 58

Views: 3138

Answers (1)

mgriebe
mgriebe

Reputation: 908

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

)

Upvotes: 2

Related Questions