-
Notifications
You must be signed in to change notification settings - Fork 623
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #416 from ropensci/feature/transmit
Send 'plotly_click' and 'plotly_selected' events to shiny when in shinyMode
- Loading branch information
Showing
21 changed files
with
551 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Package: plotly | ||
Title: Create Interactive Web Graphics via Plotly's JavaScript Graphing Library | ||
Version: 2.4.4 | ||
Version: 2.5.0 | ||
Authors@R: c(person("Carson", "Sievert", role = c("aut", "cre"), | ||
email = "[email protected]"), | ||
person("Chris", "Parmer", role = c("aut", "cph"), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# Many thanks to RStudio for shiny gadgets | ||
# And special thanks to Winston Chang for the inspiration | ||
# https://gist.github.com/wch/c4b857d73493e6550cba | ||
library(shiny) | ||
library(miniUI) | ||
library(plotly) | ||
|
||
#' Shiny gadget for interactive linear model fitting | ||
#' | ||
#' Click on points to add/remove them from consideration | ||
#' | ||
#' @param dat a data.frame | ||
#' @param x a character string specifying the x variable | ||
#' @param y a character string specifying the y variable | ||
|
||
lmGadget <- function(dat, x, y) { | ||
|
||
ui <- miniPage( | ||
gadgetTitleBar("Interactive lm"), | ||
miniContentPanel( | ||
fillRow( | ||
flex = c(NA, 1), | ||
fillCol( | ||
width = "100px", | ||
selectInput("degree", "Polynomial degree", c(1, 2, 3, 4)) | ||
), | ||
plotlyOutput("plot1", height = "100%") | ||
) | ||
) | ||
) | ||
|
||
# mechanism for managing selected points | ||
init <- function() { | ||
selected <- rep(FALSE, nrow(dat)) | ||
function(x) { | ||
selected <<- xor(selected, x) | ||
selected | ||
} | ||
} | ||
selection <- init() | ||
|
||
server <- function(input, output) { | ||
|
||
# obtain a subset of the data that is still under consideration | ||
left <- reactive({ | ||
d <- event_data("plotly_click") | ||
if (!is.null(d)) { | ||
dat <- dat[!selection(row.names(dat) %in% d[["key"]]), ] | ||
} | ||
dat | ||
}) | ||
|
||
# fit a model to subsetted data | ||
refit <- reactive({ | ||
req(input$degree) | ||
formula <- as.formula( | ||
sprintf("%s ~ poly(%s, degree = %s)", y, x, input$degree) | ||
) | ||
lm(formula, left()) | ||
}) | ||
|
||
output$plot1 <- renderPlotly({ | ||
dat2 <- left() | ||
dat2$yhat <- as.numeric(fitted(refit())) | ||
# sort data by 'x' variable so we draw a line (not a path) | ||
dat2 <- dat2[order(dat2[, x]), ] | ||
|
||
plot_ly(x = dat[, x], y = dat[, y], key = row.names(dat), mode = "markers", | ||
marker = list(color = toRGB("grey90"), size = 10)) %>% | ||
add_trace(x = dat2[, x], y = dat2[, y], mode = "markers", | ||
marker = list(color = toRGB("black"), size = 10)) %>% | ||
add_trace(x = dat2[, x], y = dat2$yhat, mode = "lines", | ||
marker = list(color = toRGB("black"))) %>% | ||
layout(showlegend = FALSE, xaxis = list(title = x), yaxis = list(title = y)) | ||
}) | ||
|
||
# Return the most recent fitted model, when we press "done" | ||
observeEvent(input$done, { | ||
stopApp(refit()) | ||
}) | ||
} | ||
|
||
runGadget(ui, server) | ||
} | ||
|
||
m <- lmGadget(mtcars, "wt", "mpg") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# git checkout feature/transmit | ||
# R CMD install ./ | ||
|
||
library(shiny) | ||
library(plotly) | ||
|
||
ui <- fluidPage( | ||
plotlyOutput("plot"), | ||
verbatimTextOutput("click") | ||
) | ||
|
||
server <- function(input, output, session) { | ||
|
||
output$plot <- renderPlotly({ | ||
# specify some map projection/options | ||
g <- list( | ||
scope = 'usa', | ||
projection = list(type = 'albers usa'), | ||
lakecolor = toRGB('white') | ||
) | ||
plot_ly(z = state.area, text = state.name, locations = state.abb, | ||
type = 'choropleth', locationmode = 'USA-states') %>% | ||
layout(geo = g) | ||
}) | ||
|
||
output$click <- renderPrint({ | ||
d <- event_data("plotly_click") | ||
if (is.null(d)) "Click on a state to view event data" else d | ||
}) | ||
|
||
} | ||
|
||
shinyApp(ui, server) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Title: Passing plotly selections to shiny via crosstalk | ||
Author: Plotly, Inc. | ||
AuthorUrl: https://plot.ly/r/ | ||
License: MIT | ||
DisplayMode: Showcase | ||
Tags: plotly, crosstalk, shiny | ||
Type: Shiny | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
library(shiny) | ||
library(plotly) | ||
|
||
ui <- fluidPage( | ||
radioButtons("plotType", "Plot Type:", choices = c("ggplotly", "plotly")), | ||
plotlyOutput("plot"), | ||
verbatimTextOutput("hover"), | ||
verbatimTextOutput("click"), | ||
verbatimTextOutput("brush") | ||
) | ||
|
||
server <- function(input, output, session) { | ||
|
||
output$plot <- renderPlotly({ | ||
if (identical(input$plotType, "ggplotly")) { | ||
p <- ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point() | ||
ggplotly(p) %>% layout(dragmode = "select") | ||
} else { | ||
plot_ly(mtcars, x = mpg, y = wt, mode = "markers") %>% | ||
layout(dragmode = "select") | ||
} | ||
}) | ||
|
||
output$hover <- renderPrint({ | ||
d <- event_data("plotly_hover") | ||
if (is.null(d)) "Hover events appear here (unhover to clear)" else d | ||
}) | ||
|
||
output$click <- renderPrint({ | ||
d <- event_data("plotly_click") | ||
if (is.null(d)) "Click events appear here (double-click to clear)" else d | ||
}) | ||
|
||
output$brush <- renderPrint({ | ||
d <- event_data("plotly_selected") | ||
if (is.null(d)) "Click and drag events (i.e., select/lasso) appear here (double-click to clear)" else d | ||
}) | ||
|
||
} | ||
|
||
shinyApp(ui, server, options = list(display.mode = "showcase")) |
Oops, something went wrong.