Skip to content

Commit 4d4c944

Browse files
authored
Merge pull request #68 from cpsievert/interactives
small tweaks; bit.ly links; links to interactives
2 parents 231268c + 4c39109 commit 4d4c944

File tree

1,037 files changed

+205669
-1259
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,037 files changed

+205669
-1259
lines changed

DESCRIPTION

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ Title: This is not really an R package, but this is here to list the dependencie
33
Version: 0.1
44
Authors@R: person("Carson", "Sievert", email = "[email protected]",
55
role = c("aut", "cre"))
6-
Depends: R (>= 3.1.0)
76
URL: https://github.com/cpsievert/plotly_book
8-
Imports:
7+
Depends:
8+
R (>= 3.1.0),
99
tidyverse,
1010
shiny,
1111
plotly,
@@ -35,13 +35,15 @@ Imports:
3535
leaflet,
3636
colorspace,
3737
idbr,
38-
emo
38+
emo,
39+
slopegraph
3940
SystemRequirements: pandoc (>= 1.12.3) - http://johnmacfarlane.net/pandoc
4041
Remotes:
41-
ropensci/plotly,
4242
ramnathv/htmlwidgets,
4343
react-R/reactR,
4444
yihui/knitr#1650,
4545
hadley/ggstat,
4646
hadley/emo,
47-
hrbrmstr/albersusa
47+
hrbrmstr/albersusa,
48+
hrbrmstr/ggalt,
49+
leeper/slopegraph

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
gitbook:
22
mkdir -p docs/images && cp images/* docs/images
3+
mkdir -p docs/interactives && cp -r interactives/* docs/interactives
34
Rscript --quiet _render.R "bookdown::gitbook"
45
cp _redirects docs/
56

_output.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ bookdown::gitbook:
1313
after_body:
1414
- includes/rstudio-cloud.html
1515
- includes/ga.html
16+
- includes/data-url.html
1617

1718
bookdown::html_chapters:
1819
css: [css/style.css, css/toc.css]

_redirects

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
https://plotly-book.cpsievert.me/* https://plotly-r.com/:splat 301!
22

3-
# redirect some links from from the 1st edition
3+
# redirect some links from the 1st edition
4+
45
https://plotly-r.com/plot-ly-for-collaboration.html https://plotly-r.com
56

67
https://plotly-r.com/extending-ggplotly.html https://plotly-r.com/improving-ggplotly.html
@@ -16,3 +17,6 @@ https://plotly-r.com/linking-views-without-shiny.html https://plotly-r.com/clien
1617
https://plotly-r.com/shiny-plotly-inputs.html https://plotly-r.com/linking-views-with-shiny.html#shiny-plotly-inputs
1718

1819
https://plotly-r.com/merging-plotly-objects.html https://plotly-r.com/arranging-views.html#arranging-plotly-objects
20+
21+
22+
https://plotly-r.com/interactives/shiny-drag-brush.html https://testing-apps.shinyapps.io/shiny-drag-brush

animation.Rmd

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ gg <- ggplot(gapminder, aes(gdpPercap, lifeExp, color = continent)) +
1212
ggplotly(gg)
1313
```
1414

15-
```{r animation-ggplotly, echo = FALSE, fig.cap = "(ref:animation-ggplotly)"}
15+
```{r animation-ggplotly, echo = FALSE, fig.cap = "(ref:animation-ggplotly)", out.extra = 'data-url="/interactives/animation-ggplotly.html"'}
1616
knitr::include_graphics("images/animation-ggplotly.png")
1717
```
1818

@@ -35,7 +35,7 @@ base %>%
3535
)
3636
```
3737

38-
```{r animation-opts, echo = FALSE, fig.cap = "(ref:animation-opts)"}
38+
```{r animation-opts, echo = FALSE, fig.cap = "(ref:animation-opts)", out.extra = 'data-url="/interactives/animation-opts.html"'}
3939
knitr::include_graphics("images/animation-opts.png")
4040
```
4141

@@ -53,20 +53,20 @@ base %>%
5353
animation_opts(frame = 1000, transition = 0, redraw = FALSE)
5454
```
5555

56-
```{r animation-factors, echo = FALSE, fig.cap = "(ref:animation-factors)"}
56+
```{r animation-factors, echo = FALSE, fig.cap = "(ref:animation-factors)", out.extra = 'data-url="/interactives/animation-factors.html"'}
5757
knitr::include_graphics("images/animation-factors.png")
5858
```
5959

6060
Both the `frame` and `ids` attributes operate on the trace level -- meaning that we can target specific layers of the graph to be animated. One obvious use case for this is to provide a background which displays every possible frame (which is not animated) and overlay the animated frames onto that background. Figure \@ref(fig:animation-targets) shows the same information as Figure \@ref(fig:animation-opts), but layers animated frames on top of a background of all the frames. As a result, it is easier to put a specific year into a global context.
6161

6262
```r
6363
base %>%
64-
add_markers(color = ~continent, alpha = 0.2, showlegend = F) %>%
64+
add_markers(color = ~continent, alpha = 0.2, alpha_stroke = 0.2, showlegend = F) %>%
6565
add_markers(color = ~continent, frame = ~year, ids = ~country) %>%
6666
animation_opts(1000, redraw = FALSE)
6767
```
6868

69-
```{r animation-targets, echo = FALSE, fig.cap = "(ref:animation-targets)"}
69+
```{r animation-targets, echo = FALSE, fig.cap = "(ref:animation-targets)", out.extra = 'data-url="/interactives/animation-targets.html"'}
7070
knitr::include_graphics("images/animation-targets.png")
7171
```
7272

arranging.Rmd

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ p2 <- plot_ly(economics, x = ~date, y = ~uempmed) %>%
2020
subplot(p1, p2)
2121
```
2222

23-
24-
```{r subplot-simple, echo=FALSE, fig.cap="(ref:subplot-simple)"}
23+
```{r subplot-simple, echo=FALSE, fig.cap="(ref:subplot-simple)", out.extra = 'data-url="/interactives/subplot-simple.html"'}
2524
knitr::include_graphics("images/subplot-simple.svg")
2625
```
2726

@@ -36,7 +35,7 @@ plots <- lapply(vars, function(var) {
3635
subplot(plots, nrows = length(plots), shareX = TRUE, titleX = FALSE)
3736
```
3837

39-
```{r economics, echo=FALSE, fig.cap = "(ref:economics)"}
38+
```{r economics, echo=FALSE, fig.cap = "(ref:economics)", out.extra = 'data-url="/interactives/economics.html"'}
4039
knitr::include_graphics("images/economics.svg")
4140
```
4241

@@ -65,7 +64,7 @@ s <- subplot(
6564
layout(s, showlegend = FALSE)
6665
```
6766

68-
```{r joint, echo = FALSE, fig.cap = "(ref:joint)"}
67+
```{r joint, echo = FALSE, fig.cap = "(ref:joint)", out.extra = 'data-url="/interactives/joint.html"'}
6968
knitr::include_graphics("images/joint.svg")
7069
```
7170

@@ -85,7 +84,7 @@ subplot(
8584
)
8685
```
8786

88-
```{r recursive, echo = FALSE, fig.cap = "(ref:recursive)"}
87+
```{r recursive, echo = FALSE, fig.cap = "(ref:recursive)", out.extra = 'data-url="/interactives/recursive.html"'}
8988
knitr::include_graphics("images/recursive.svg")
9089
```
9190

@@ -117,7 +116,7 @@ subplot(barcharts, margin = 0.01) %>%
117116
colorbar(y = 0.5)
118117
```
119118

120-
```{r map-subplot, echo = FALSE, fig.cap = "(ref:map-subplot)"}
119+
```{r map-subplot, echo = FALSE, fig.cap = "(ref:map-subplot)", out.extra = 'data-url="/interactives/map-subplot.html"'}
121120
knitr::include_graphics("images/map-subplot.svg")
122121
```
123122

@@ -155,7 +154,7 @@ class(pm)
155154
ggplotly(pm)
156155
```
157156

158-
```{r ggpairs, echo = FALSE, fig.cap = "(ref:ggpairs)"}
157+
```{r ggpairs, echo = FALSE, fig.cap = "(ref:ggpairs)", out.extra = 'data-url="/interactives/ggpairs.html"'}
159158
knitr::include_graphics("images/ggpairs.svg")
160159
```
161160

@@ -204,7 +203,7 @@ economics_long %>%
204203
subplot(nrows = NROW(.), shareX = TRUE)
205204
```
206205

207-
```{r subplot-trellis, echo=FALSE, fig.cap = "(ref:subplot-trellis)"}
206+
```{r subplot-trellis, echo=FALSE, fig.cap = "(ref:subplot-trellis)", out.extra = 'data-url="/interactives/subplot-trellis.html"'}
208207
knitr::include_graphics("images/subplot-trellis.svg")
209208
```
210209

@@ -241,8 +240,6 @@ Although **flexdashboard** is a really excellent way to arrange web-based conten
241240

242241
Figure \@ref(fig:flexdashboard-ggplotly) provides an example of embedding `ggplotly()` inside **flexdashboard** [@flexdashboard]. Since **flexdashboard** is an **rmarkdown** template, it automatically comes with many of things that make **rmarkdown** great: ability to produce standalone HTML, integration with other languages, and thoughtful integration with RStudio products like Connect. There are many other things to like about **flexdashboard**, including lots of easy-to-use theming options, multiple pages, storyboards, and even **shiny** integration. Explaining how the **flexdashboard** package actually works is beyond the scope of this book, but you can visit the website for documentation and more examples <https://rmarkdown.rstudio.com/flexdashboard/>.
243242

244-
245-
246243
```{r flexdashboard-ggplotly, echo = FALSE, fig.link="https://plotly-r.com/flexdashboard.html", fig.cap = "(ref:flexdashboard-ggplotly)"}
247244
knitr::include_graphics("images/flexdashboard.png")
248245
```
@@ -266,7 +263,7 @@ fluidPage(
266263
)
267264
```
268265

269-
```{r fluid, echo = FALSE, fig.cap = "(ref:fluid)"}
266+
```{r fluid, echo = FALSE, fig.cap = "(ref:fluid)", out.extra = 'data-url="/interactives/fluid.html"'}
270267
knitr::include_graphics("images/fluid.png")
271268
```
272269

@@ -296,7 +293,7 @@ browsable(div(
296293
))
297294
```
298295

299-
```{r flexbox, echo = FALSE, fig.cap = "(ref:flexbox)"}
296+
```{r flexbox, echo = FALSE, fig.cap = "(ref:flexbox)", out.extra = 'data-url="/interactives/flexbox.html"'}
300297
knitr::include_graphics("images/flexbox.png")
301298
```
302299

code/3Devents/app.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
library(plotly)
2+
3+
shiny::shinyAppDir(
4+
system.file(package = "plotly", "examples", "shiny", "event_data_3D")
5+
)

code/discrete-event-data/app.R

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
library(shiny)
2+
library(dplyr)
3+
library(plotly)
4+
5+
ui <- fluidPage(
6+
plotlyOutput("bars"),
7+
verbatimTextOutput("click")
8+
)
9+
10+
classes <- sort(unique(mpg$class))
11+
12+
server <- function(input, output, session) {
13+
14+
output$bars <- renderPlotly({
15+
ggplot(mpg, aes(class, fill = drv, customdata = drv)) + geom_bar()
16+
})
17+
18+
output$click <- renderPrint({
19+
d <- event_data("plotly_click")
20+
if (is.null(d)) return("Click a bar")
21+
mpg %>%
22+
filter(drv %in% d$customdata) %>%
23+
filter(class %in% classes[d$x])
24+
})
25+
26+
}
27+
28+
shinyApp(ui, server)

code/event-priority/app.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
library(shiny)
2+
library(plotly)
3+
4+
ui <- fluidPage(
5+
plotlyOutput("p"),
6+
textOutput("time1"),
7+
textOutput("time2")
8+
)
9+
10+
server <- function(input, output, session) {
11+
12+
output$p <- renderPlotly({
13+
plot_ly(x = 1:2, y = 1:2, size = I(c(100, 150))) %>%
14+
add_markers()
15+
})
16+
17+
output$time1 <- renderText({
18+
event_data("plotly_click")
19+
paste("Input priority: ", Sys.time())
20+
})
21+
22+
output$time2 <- renderText({
23+
event_data("plotly_click", priority = "event")
24+
paste("Event priority: ", Sys.time())
25+
})
26+
27+
}
28+
29+
shinyApp(ui, server)

code/interactive-lm/app.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
library(plotly)
2+
3+
shiny::shinyAppDir(
4+
system.file(package = "plotly", "examples", "shiny", "drag_markers")
5+
)

0 commit comments

Comments
 (0)