Skip to content

Commit e5746ae

Browse files
committed
update examples
1 parent 0895b14 commit e5746ae

File tree

6 files changed

+145
-43
lines changed

6 files changed

+145
-43
lines changed

inst/examples/brush/app.R

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
library(plotly)
2+
library(shiny)
3+
4+
# user interface
5+
ui <- fluidPage(
6+
titlePanel("Linked highlighting with plotly and shiny"),
7+
mainPanel(
8+
htmltools::div(style = "display:inline-block", plotlyOutput("x", width = 400, height = 250)),
9+
wellPanel(
10+
style = "display:inline-block; vertical-align:top;",
11+
sliderInput("xbins", "Number of x bins",
12+
min = 1, max = 50, value = 20, width = 250),
13+
sliderInput("ybins", "Number of y bins",
14+
min = 1, max = 50, value = 20, width = 250)
15+
),
16+
br(),
17+
htmltools::div(style = "display:inline-block", plotlyOutput("xy", width = 400, height = 400)),
18+
htmltools::div(style = "display:inline-block", plotlyOutput("y", width = 250, height = 400))
19+
)
20+
)
21+
22+
# marker objects
23+
m <- list(color = toRGB("black"))
24+
m2 <- list(color = toRGB("black", 0.2))
25+
26+
server <- function(input, output, session) {
27+
28+
# convenience function for computing xbin/ybin object given a number of bins
29+
compute_bins <- function(x, n) {
30+
list(
31+
start = min(x),
32+
end = max(x),
33+
size = (max(x) - min(x)) / n
34+
)
35+
}
36+
37+
# the 'x' histogram
38+
output$x <- renderPlotly({
39+
x <- cars$speed
40+
xbins <- compute_bins(x, input$xbins)
41+
p <- plot_ly(x = x, type = "histogram", autobinx = F,
42+
xbins = xbins, marker = m2)
43+
# obtain plotlyjs selection
44+
s <- event_data("plotly_selected")
45+
# if points are selected, subset the data, and highlight
46+
if (length(s$x) > 0) {
47+
p <- add_trace(p, x = s$x, type = "histogram", autobinx = F,
48+
xbins = xbins, marker = m)
49+
}
50+
p %>%
51+
config(displayModeBar = F, showLink = F) %>%
52+
layout(showlegend = F, barmode = "overlay", yaxis = list(title = "count"),
53+
xaxis = list(title = "", showticklabels = F))
54+
})
55+
56+
# basically the same as 'x' histogram
57+
output$y <- renderPlotly({
58+
y <- cars$dist
59+
ybins <- compute_bins(y, input$ybins)
60+
p <- plot_ly(y = y, type = "histogram", autobiny = F,
61+
ybins = ybins, marker = m2)
62+
s <- event_data("plotly_selected")
63+
if (length(s$y) > 0) {
64+
p <- add_trace(p, y = s$y, type = "histogram", autobiny = F,
65+
ybins = ybins, marker = m)
66+
}
67+
p %>%
68+
config(displayModeBar = F, showLink = F) %>%
69+
layout(showlegend = F, barmode = "overlay", xaxis = list(title = "count"),
70+
yaxis = list(title = "", showticklabels = F))
71+
})
72+
73+
output$xy <- renderPlotly({
74+
cars %>%
75+
plot_ly(x = speed, y = dist,
76+
mode = "markers", marker = m) %>%
77+
layout(dragmode = "select")
78+
})
79+
80+
}
81+
82+
shinyApp(ui, server)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Title: Passing plotly selections to shiny via crosstalk
2+
Author: Plotly, Inc.
3+
AuthorUrl: https://plot.ly/r/
4+
License: MIT
5+
DisplayMode: Showcase
6+
Tags: plotly, crosstalk, shiny
7+
Type: Shiny
8+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# git checkout feature/transmit
2+
# R CMD install ./
3+
4+
library(plotly)
5+
library(shiny)
6+
7+
# compute a correlation matrix
8+
correlation <- round(cor(mtcars), 3)
9+
nms <- names(mtcars)
10+
11+
ui <- fluidPage(
12+
mainPanel(
13+
plotlyOutput("heat"),
14+
plotlyOutput("scatterplot")
15+
),
16+
verbatimTextOutput("selection")
17+
)
18+
19+
server <- function(input, output, session) {
20+
output$heat <- renderPlotly({
21+
plot_ly(x = nms, y = nms, z = correlation,
22+
key = correlation, type = "heatmap") %>%
23+
layout(xaxis = list(title = ""),
24+
yaxis = list(title = ""))
25+
})
26+
27+
output$selection <- renderPrint({
28+
s <- event_data("plotly_click")
29+
if (length(s) == 0) {
30+
"Click on a cell in the heatmap to display a scatterplot"
31+
} else {
32+
cat("You selected: \n\n")
33+
as.list(s)
34+
}
35+
})
36+
37+
output$scatterplot <- renderPlotly({
38+
s <- event_data("plotly_click")
39+
if (length(s)) {
40+
vars <- c(s[["x"]], s[["y"]])
41+
d <- setNames(mtcars[vars], c("x", "y"))
42+
yhat <- fitted(lm(y ~ x, data = d))
43+
plot_ly(d, x = x, y = y, mode = "markers") %>%
44+
add_trace(x = x, y = yhat, mode = "lines") %>%
45+
layout(xaxis = list(title = s[["x"]]),
46+
yaxis = list(title = s[["y"]]),
47+
showlegend = FALSE)
48+
} else {
49+
plot_ly()
50+
}
51+
})
52+
53+
}
54+
55+
shinyApp(ui, server, options = list(display.mode = "showcase"))

0 commit comments

Comments
 (0)