Skip to content

Instantly share code, notes, and snippets.

@cpsievert
Last active July 29, 2018 07:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpsievert/5934f173cafffb8dfb4f23d5488cd185 to your computer and use it in GitHub Desktop.
Save cpsievert/5934f173cafffb8dfb4f23d5488cd185 to your computer and use it in GitHub Desktop.
Simple Linked Brush
library(plotly)
library(shiny)
ui <- fluidPage(
fluidRow(
column(6, plotlyOutput("p1")),
column(6, plotlyOutput("p2"))
)
)
server <- function(input, output, session) {
nms <- row.names(mtcars)
output$p1 <- renderPlotly({
d <- event_data("plotly_selected")
p <- plot_ly(mtcars, x = ~wt, y = ~mpg) %>%
add_markers(key = ~nms, color = I("black"))
if (!is.null(d)) {
m <- mtcars[nms %in% d[["key"]], ]
p <- add_markers(p, data = m, color = I("red"))
}
layout(p, dragmode = "lasso", showlegend = FALSE)
})
output$p2 <- renderPlotly({
d <- event_data("plotly_selected")
p <- plot_ly(mtcars, x = ~wt, y = ~disp) %>%
add_markers(key = ~nms, color = I("black"))
if (!is.null(d)) {
m <- mtcars[nms %in% d[["key"]], ]
p <- add_markers(p, data = m, color = I("red"))
}
layout(p, dragmode = "lasso", showlegend = FALSE)
})
}
shinyApp(ui, server, options = list(display.mode = "showcase"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment