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?
Solution 1:
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")
}
)