Instantly share code, notes, and snippets.

@cpsievert /app.R
Last active Jul 29, 2018

Embed
What would you like to do?
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