-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.Rmd
1816 lines (1203 loc) · 42.5 KB
/
index.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Interactive Rasters with R"
author: "inSileco Team"
date: "2020/12/15"
output:
xaringan::moon_reader:
css: [default, rd.css, rd-font.css, "hygge"]
lib_dir: assets
seal: false
nature:
highlightStyle: dracula
countIncrementalSlides: false
beforeInit: "macros.js"
---
```{r setup, include = FALSE, purl = FALSE}
knitr::opts_chunk$set(
comment = "#>",
collapse = TRUE,
warning = FALSE,
message = FALSE,
dev = "png",
fig.width = 5,
fig.height = 4.5,
fig.align = 'center',
width = 120
)
mypar <- list(fg = "#37abc8", bg = "transparent", las = 1)
par(mypar)
# library(icon)
rfa <- function(...) icon::fontawesome(...)
```
class: title-slide, middle
## .font200[`r icon::fontawesome("r-project")`asters]
<br><br>
.instructors[
.font180[Interactive raster visualization with `r rfa("r-project")`]
<br><br><br>
.authors140[David Beauchesne & Kevin Cazelles]
<br><br>
`r format(Sys.time(), '%B %d, %Y')`
<br>
.font200[
[`r rfa("github")`](https://github.com/inSilecoInc/interactiveRaster)
[`r rfa("database")`](https://github.com/inSilecoInc/interactiveRaster/blob/main/data_and_script.zip)
]
]
<br>
<img src="img/logoW.png" width="140px"></img>
![deploy workshop](https://github.com/inSilecoInc/interactiveRaster/workflows/deploy%20workshop/badge.svg)
.instructors[Content under [`r rfa("creative-commons")` `r rfa("creative-commons-by")`](https://creativecommons.org/licenses/by/4.0/) unless otherwise specified]
---
class: inverse, center, middle
# Learning objectives
![:custom_hr]()
## .font160[`r rfa("map")` + `r rfa("r-project")`]
---
# Learning objectives
<br>
1. Understand the benefits of interactive data visualization with `r rfa("r-project")`
--
2. Learn how to create and share interactive raster maps
--
3. Learn how to build interactive dashboards
--
4. Learn how to build simple shiny apps
--
Focus on `raster` package for simplicity, but all tools presented can work with `stars`
--
**Objective of the day:** Everyone should be able to create and share interactive maps to visualize rasters and embed them in simple dashboards and shiny applications
---
class: inverse, center, middle
# `r rfa("info-circle")` Rationalize
![:custom_hr]()
## Why use `r rfa("r-project")` for interactive data visualization? (~10min)
---
# Interactive visualization
## Interactive data visualization
> Interactive data visualization refers to the use of modern data analysis software that enables users to directly manipulate and explore graphical representations of data.
--
## Why use interactive visualization?
- Identify trends
- Observe relationships in data
- Data storytelling
- Simplify complex data
- Share and explore data
- Get more out of static visualizations!
---
# Interactive visualization
## Javascript
> [JavaScript](https://www.javascript.com/) is a text-based programming language used both on the client-side and server-side that allows you to make web pages interactive.
--
> JavaScript is the modern interactive web
--
- Close to 1.5 million libraries
--
- Requires in depth knowledge of web-based programming, and, if you are a `r rfa("r-project")` user, learning another programming language
---
# Why `r rfa("r-project")`?
<br>
1. `r rfa("r-project")` packages are actively developped for interactive visualization. See the [htmlwidgets for R](https://www.htmlwidgets.org/) web page for examples
--
2. No need to learn a new language
--
3. Efficiently explore outputs from `r rfa("r-project")` analyses
--
4. Share interactive outputs of `r rfa("r-project")` analyses with collaborators in a variety of formats
--
5. For people still using [ArcGIS](www.arcgis.com) or [QGIS](www.qgis.org) solely for visual data exploration: interactive mapping in `r rfa("r-project")` can now - *partly* - address this
---
# Why `r rfa("r-project")`?
## HTML widgets
In `r rfa("r-project")`, HTML widgets are one of two things:
1. The [`htmlwidgets`](https://cran.r-project.org/package=htmlwidgets) package provides a framework for creating R bindings to JavaScript visualization libraries that can be rendered in a variety of formats (*e.g.* RStudio, R Markdown documents, Shiny applications, or standalone web pages).
--
2. A collection of R packages that allow a user to build interactive visualization (*i.e.* tables, charts, maps, networks, etc) through specific JavaScript libraries
---
# Why `r rfa("r-project")`?
## `r rfa("r-project")` packages for interactive mapping
### **Interactive visualization in general**
.pull-left[
- [`plotly`](https://cran.r-project.org/package=plotly) - uses [Plotly.js](https://plotly.com/javascript/)
- [`highcharter`](https://cran.r-project.org/package=highcharter) - uses [Highcharts.js](https://jkunst.com/highcharter/)
]
--
.pull-right[
*Work well with vectorized spatial objects (`sf` and `sp`) in particular. Not so much with rasters yet.*
]
--
### **Interactive mapping in particular**
.pull-left[
- [`leaflet`](https://cran.r-project.org/package=leaflet) `r rfa("check")` - uses [Leaflet.js](https://leafletjs.com/)
]
--
.pull-right[
*Works well with vectors (`sf` and `sp`) and rasters (`raster` and `stars`)*
]
--
### **`r rfa("r-project")` packages expanding `leaflet` capabilities**
- [`mapview`](https://cran.r-project.org/package=mapview) `r rfa("check")`
- [`tmap`](https://cran.r-project.org/package=tmap) `r rfa("check")`
---
class: inverse, center, middle
# Interactive raster mapping
![:custom_hr]()
## .font160[`r rfa("map")` + `r rfa("r-project")`] (1h)
---
# <a href="https://leafletjs.com/"><img src="https://leafletjs.com/docs/images/logo.png" alt="" width="25%"></a>
## Leaflet library
> Leaflet is the leading open-source JavaScript library for mobile-friendly interactive maps. Weighing just about 39 KB of JS, it has all the mapping features most developers ever need.
--
<h3>Examples<sup>*</sup></h3>
- [The New York Times](https://www.nytimes.com/projects/elections/2013/nyc-primary/mayor/map.html)
- [The Washington Post](https://www.washingtonpost.com/sf/local/2013/11/09/washington-a-world-apart/)
- [GitHub](https://github.blog/2013-06-13-there-s-a-map-for-that/)
- [Open Street Map](https://www.openstreetmap.org/#map=11/46.8543/-71.3414)
.font70[
<sup>*</sup>Examples provided in R Studio's [Leaflet for R](https://rstudio.github.io/leaflet/) introduction
]
---
# <a href="https://leafletjs.com/"><img src="https://leafletjs.com/docs/images/logo.png" alt="" width="25%"></a>
## `r rfa("r-project")` package
[`leaflet`](https://cran.r-project.org/package=leaflet) .font90[06/2015 (1.0.0) // 01/2021 (2.0.4.1)]
- The `leaflet` package integrates and controls Leaflet maps through `r rfa("r-project")`
--
### **Features** - .font70[full list [here](https://rstudio.github.io/leaflet/)]
.font90[
- Interactive panning/zooming
- Compose maps using:
- Map tiles
- Markers
- Polygons
- Lines
- Rasters
- Popups
- Embed maps in knitr/R Markdown documents and Shiny apps
]
---
# Package [`leaflet`](https://cran.r-project.org/package=leaflet)
## Quick map
.pull-left2[
Leaflet map centered on the St. Lawrence
```{r, eval = FALSE, purl = FALSE}
library(leaflet)
lf <- leaflet() %>%
setView(lng = -63,
lat = 48,
zoom = 5)
addTiles(group = 'Default')
lf
```
.font70[See [`setView()`](https://www.rdocumentation.org/packages/leaflet/versions/2.0.3/topics/setView) documentation for more options to set map extent.]
]
.pull-right2[
```{r load_leaflet, fig.width = 6, fig.height = 6, echo = FALSE}
library(leaflet)
lf <- leaflet() %>%
addTiles(group = 'Default') %>%
setView(lng = -63,
lat = 48,
zoom = 5)
lf
```
]
---
# Package [`leaflet`](https://cran.r-project.org/package=leaflet)
## A quick word on pipes
The pipe operator `%>%` is a chained method that lets you pass an intermediate result to the next function. Thus, these two code chunks provide the same output:
.pull-left[
```{r use_pipe}
# Example with pipe operator
lf <- leaflet() %>%
setView(lng = -63,
lat = 48,
zoom = 5) %>%
addTiles(group = 'Default')
```
]
.pull-right[
```{r nopipe}
# Example without pipe operator
lf <- setView(map = leaflet(),
lng = -63,
lat = 48,
zoom = 5)
lf <- addTiles(map = lf,
group = 'Default')
```
]
.font70[`%>%` is similar to the `+` operator used by `ggplot2` and `tmap`]
.font70[See [here](https://www.datacamp.com/community/tutorials/pipe-r-tutorial) for a full description of the history and use of pipes. Note that in the upcoming release
A [simple native pipe syntax](https://cran.r-project.org/doc/manuals/r-devel/NEWS.html), `|>`, will be included in the upcoming stable release.
]
---
# Package [`leaflet`](https://cran.r-project.org/package=leaflet)
## Base maps
.pull-left2[
`addTiles()` uses [OpenStreetMap](https://www.openstreetmap.org/) as default base map.
Use `addProviderTiles()` for other options.
```{r, eval = FALSE, purl = FALSE}
leaflet() %>%
setView(lng = -63,
lat = 48,
zoom = 5) %>%
addProviderTiles('Esri.OceanBasemap',
group = 'Ocean')
```
.font90[Full provider list [here](http://leaflet-extras.github.io/leaflet-providers/preview/index.html)]
]
.pull-right2[
```{r base_maps, fig.width = 6, fig.height = 6, echo = FALSE}
leaflet() %>%
setView(lng = -63,
lat = 48,
zoom = 5) %>%
addProviderTiles('Esri.OceanBasemap',
group = 'Ocean')
```
]
---
# Package [`leaflet`](https://cran.r-project.org/package=leaflet)
## Multiple base maps
.pull-left2[
Use [`addLayersControl()`](https://www.rdocumentation.org/packages/leaflet/versions/2.0.3/topics/addLayersControl) to toggle base map selection.
.font90[
```{r, eval = FALSE, purl = FALSE}
lf <-
lf %>%
addProviderTiles('Esri.OceanBasemap',
group = 'Ocean') %>%
addProviderTiles("OpenTopoMap",
group = "Topo") %>%
# Add layer selection
addLayersControl(
baseGroups = c('Default','Ocean',
'Topo'),
position = 'topleft')
lf
```
Note that we are adding features to the preexisting `lf` object
]
]
.pull-right2[
```{r multiple_base, fig.width = 6, fig.height = 6, echo = FALSE}
lf <- lf %>%
addProviderTiles('Esri.OceanBasemap',
group = 'Ocean') %>%
addProviderTiles("OpenTopoMap",
group = "Topo") %>%
# Add layer selection
addLayersControl(
baseGroups = c('Default','Ocean','Topo'),
position = 'topleft')
lf
```
]
---
# Package [`leaflet`](https://cran.r-project.org/package=leaflet)
## Add rasters
First, get some raster layers from the [`eDrivers`](https://github.com/eDrivers/eDrivers) `r rfa("r-project")` package, which provides data on environmental stressors for the St. Lawrence System.
Let's get data for hypoxia, demersal destructive fisheries (trawl and dredge), and acidification.
```{r eDrivers, eval = TRUE}
# Install eDrivers
# devtools::install_github('eDrivers/eDrivers')
# Load data from eDrivers
library(eDrivers)
fetchDrivers(drivers = c('Hypoxia','FisheriesDD','Acidification'),
output = 'data')
# Raster objects from eDrivers class objects
library(raster)
hyp <- raster('data/Hypoxia.tif')
fish <- raster('data/FisheriesDD.tif')
acid <- raster('data/Acidification.tif')
```
.font70[Use `fetchList()` to see all data available in `eDrivers`]
---
# Package [`leaflet`](https://cran.r-project.org/package=leaflet)
## Add rasters
.pull-left2[
Use [`addRasterImage()`](https://www.rdocumentation.org/packages/leaflet/versions/2.0.3/topics/addRasterImage) to add rasters from `raster` package.
```{r, eval = FALSE, purl = FALSE}
lf <- lf %>%
addRasterImage(hyp,group = 'Hyp') %>%
addRasterImage(fish,group = 'Fish') %>%
addRasterImage(acid,group = 'Acid') %>%
# Reset layer selection
addLayersControl(
baseGroups = c('Default','Ocean',
'Topo'),
overlayGroups = c('Hyp','Fish',
'Acid'),
position = 'topleft')
lf
```
]
.pull-right2[
```{r leaflet_raster, fig.width = 6, fig.height = 6, echo = FALSE, eval = TRUE}
# Add layers to leaflet map
lf <- lf %>%
addRasterImage(hyp,group = 'Hyp') %>%
addRasterImage(fish,group = 'Fish') %>%
addRasterImage(acid,group = 'Acid') %>%
# Reset layer selection
addLayersControl(
baseGroups = c('Default','Ocean','Topo'),
overlayGroups = c('Hyp','Fish','Acid'),
position = 'topleft')
lf
```
]
---
# Package [`leaflet`](https://cran.r-project.org/package=leaflet)
## Add rasters
**A couple warnings:**
- `leaflet` uses the [EPSG:3857](https://spatialreference.org/ref/sr-org/7483/) projection and will convert the layers you add automatically. You may want to reproject your rasters manually with argument `project = FALSE` in `addRasterImage()` to save computing time.
--
- Raster size is limited by default. Use argument `maxBytes` in `addRasterImage()` to change this. (Hint: try to add `bathy.tif` to a leaflet map)
---
# Package [`leaflet`](https://cran.r-project.org/package=leaflet)
- Leaflet maps can be exported using `saveWidget()` from the `htmlwidget` package
```{r export_leaflet, eval = FALSE}
dir.create('output')
htmlwidgets::saveWidget(lf, file="output/lf.html")
```
---
# Package [`leaflet`](https://cran.r-project.org/package=leaflet)
```{r, echo = FALSE, purl = FALSE}
countdown::countdown(minutes = 20, seconds = 0)
```
### .font90[`r rfa("star")` Select two raster layers to map and change their projection to EPSG:3857]
.font80[
- Use [https://epsg.io/](https://epsg.io/) to get the `proj` string
]
### .font90[`r rfa("star")` Generate a leaflet map with:]
.font80[
- Two tile providers (hint: visit [this](http://leaflet-extras.github.io/leaflet-providers/preview/index.html) website)
- The two newly projected raster layers
- Set view to the Bedford Institute of Oceanography
- Get coordinates from [Google Maps](https://www.google.ca/maps) or [OpenStreetMap](https://www.openstreetmap.org/)
]
### .font90[`r rfa("star")` `r rfa("star")` Use a custom color palette to render the rasters (hint: look up [`colorNumeric()`](https://www.rdocumentation.org/packages/leaflet/versions/2.0.3/topics/colorNumeric))]
### .font90[`r rfa("star")` `r rfa("star")` Render a single raster on map startup (hint: look up [`hideGroup()`](https://rdrr.io/cran/leaflet/man/showGroup.html))]
---
# Solution
.font90[
```{r leafletEx, eval = FALSE, include = TRUE, purl = FALSE}
# Project rasters
prj <- '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs'
hyp2 <- projectRaster(hyp, crs = CRS(prj))
acid2 <- projectRaster(acid, crs = CRS(prj))
# Colors
cols <- c("#c7cbce", "#687677", "#222d3d", "#25364a", "#172434",
"#ad6a11", "#e6a331", "#e4be29", "#f2ea8b")
# Leaflet map
leaflet() %>%
setView(lng = -63.6118, lat = 44.6828, zoom = 5) %>%
addProviderTiles('Esri.OceanBasemap', group = 'Ocean') %>%
addProviderTiles("OpenTopoMap", group = "Topo") %>%
addRasterImage(hyp2, group = 'Hyp', project = FALSE,
colors = colorNumeric(cols, values(hyp2), na.color = "transparent")) %>%
addRasterImage(acid2, group = 'Acid', project = FALSE,
colors = colorNumeric(cols, values(acid2), na.color = "transparent")) %>%
# Reset layer selection
addLayersControl(baseGroups = c('Ocean', 'Topo'),
overlayGroups = c('Hyp','Acid'),
position = 'topleft') %>%
# Hide 2 layers
hideGroup(c("Fish","Acid"))
```
]
---
# Package [`mapview`](https://cran.r-project.org/package=mapview)
## `r rfa("r-project")` package
[`mapview`](https://cran.r-project.org/package=mapview) .font90[12/2015 (1.0.0) // 08/2020 (2.9.0)]
> `mapview` provides functions to very quickly and conveniently create interactive visualisations of spatial data. It’s main goal is to fill the gap of quick (not presentation grade) interactive plotting to examine and visually investigate both aspects of spatial data, the geometries and their attributes.
---
# Package [`mapview`](https://cran.r-project.org/package=mapview)
```{r mapview, eval = TRUE, fig.width = 12, fig.height = 6}
library(mapview)
mv <- mapview(hyp) + fish + acid
mv
```
---
# Package [`mapview`](https://cran.r-project.org/package=mapview)
```{r mapview_export, eval = TRUE, fig.width = 12, fig.height = 6}
dir.create('output')
mapshot(mv, url = 'output/map.html')
```
The interactive map can then be shared with the `html` file and `map_files/` folder
---
# Package [`mapview`](https://cran.r-project.org/package=mapview)
```{r, echo = FALSE, purl = FALSE}
countdown::countdown(minutes = 10, seconds = 0)
```
<br/>
### `r rfa("star")` Recreate the map you generated before with `leaflet` using `mapview`
--
### `r rfa("star")` Export it as a `html` file in the `output/` folder
--
### `r rfa("star")` Open the `.html` file in your web browser
--
### `r rfa("star")` `r rfa("star")` `r rfa("star")` `r rfa("star")` Send it to your best friend!
---
# Solution
```{r mapviewEx, eval = FALSE, include = TRUE, purl = FALSE}
# Color ramp palette
pal <- colorRampPalette(cols)
# Mapview
mv <- mapview(hyp2, col.regions = pal) +
mapview(acid2, col.regions = pal)
# Export
dir.create('output')
mapshot(mv, url = 'output/mapviewEx.html')
# Send email
# Not actually going to do this! But! If you want to send emails through R, check this blog post:
# https://blog.mailtrap.io/r-send-email/
```
---
# Package [`tmap`](https://cran.r-project.org/package=tmap)
## `r rfa("r-project")` package
[`tmap`](https://cran.r-project.org/package=tmap) .font90[12/2015 (1.0.0) // 08/2020 (2.9.0)]
- `tmap` creates flexible thematic maps.
--
- We have looked at the static mapping visualization it offers
--
- Now we look at the interactive capabilities
---
# Package [`tmap`](https://cran.r-project.org/package=tmap)
.pull-left[
.font90[
```{r, fig.width = 5.5, fig.height = 5, echo = TRUE, eval = TRUE, purl = FALSE}
library(tmap)
tmap_mode("plot")
static <- tm_shape(fish) +
tm_raster(title = "Fisheries")
static
```
]
]
.pull-right[
.font90[
```{r, fig.width = 5.5, fig.height = 5, echo = TRUE, eval = TRUE, purl = FALSE}
library(tmap)
tmap_mode("view")
inter <- tm_shape(fish) +
tm_raster(title = "Fisheries")
inter
```
]
]
---
# Package [`tmap`](https://cran.r-project.org/package=tmap)
.pull-left[
.font90[
```{r tmap_static, fig.width = 5.5, fig.height = 5, echo = TRUE, eval = FALSE}
library(tmap)
tmap_mode("plot")
static <- tm_shape(fish) +
tm_raster(title = "Fisheries")
static
# Export as png
tmap_save(static,
filename = "output/Fisheries.png")
```
]
]
.pull-right[
.font90[
```{r tmap_interactive, fig.width = 5.5, fig.height = 5, echo = TRUE, eval = FALSE}
library(tmap)
tmap_mode("view")
inter <- tm_shape(fish) +
tm_raster(title = "Fisheries")
inter
# Export as html
tmap_save(inter,
filename = "output/Fisheries.html")
```
]
]
---
# Package [`tmap`](https://cran.r-project.org/package=tmap)
## Multiple layers
.pull-left2[
Use `tm_facets()` to include multiple raster layers
```{r, eval = FALSE, purl = FALSE}
rstack <- stack(hyp, fish, acid)
tmap_mode("view")
tm_shape(rstack) +
tm_raster(title = c("Hypoxia",
"Fisheries",
"Acidification")) +
tm_facets(as.layers = TRUE,
free.scales.raster = TRUE)
```
]
.pull-right2[
```{r tmap_facets, fig.width = 6, fig.height = 6, echo = FALSE, eval = TRUE}
rstack <- stack(hyp, fish, acid)
tmap_mode("view")
tm_shape(rstack) +
tm_raster(title = c("Hypoxia",
"Fisheries",
"Acidification")) +
tm_facets(as.layers = TRUE,
free.scales.raster = TRUE)
```
]
---
# Package [`tmap`](https://cran.r-project.org/package=tmap)
```{r, echo = FALSE, purl = FALSE}
countdown::countdown(minutes = 10, seconds = 0)
```
<br/>
### `r rfa("star")` Recreate the static map you generated before using `tmap` as an interactive map but first **resample** the raster to have a maximum of 20 cells per °.
### `r rfa("star")` Export it as a `.html` file in the `output/` folder
### `r rfa("star")` View the '.html' file in your web browser
---
# Solution
.font90[
```{r tmapEx, eval = FALSE, include = TRUE, purl = FALSE}
# manipulation
library(stars)
ras <- read_stars("data/bathy.tif")
stl <- sf::st_read("data/st_laurence.geojson")
ras2 <- st_warp(ras, cellsize = .05, crs = st_crs(ras))
ras_v <- ras2[stl]
names(ras_v) <- "Elevation"
# view mode
tmap_mode("view")
# colors
pal <- colorRampPalette(c("#c7cbce", "#687677", "#222d3d", "#25364a", "#172434", "#ad6a11", "#e6a331", "#e4be29", "#f2ea8b"))
# raster
elv <- tm_shape(ras_v) +
tm_raster("Elevation", breaks = seq(-800, 200, 100), palette = pal(11), midpoint = NA) +
tm_layout(main.title = "St-Lawrence river & Gulf", main.title.color = "#ad6a11") +
tm_xlab("longitude", size = 0.5) + tm_ylab("latitude", size = 0.5) +
tm_graticules(lwd = .5, col = "#aaaaaa")
shp <- tm_shape(stl) + tm_borders(col = "black", lwd = 2)
oth <- tm_compass(type = "8star", position = c("left", "bottom")) +
tm_scale_bar(breaks = c(0, 100, 200), text.size = .8) +
tm_logo(c("https://www.r-project.org/logo/Rlogo.png"), position = c("right", "top"), height = 3)
tmapEx <- elv + shp + oth
# Export
tmap_save(tmapEx, filename = "output/tmapEx.html")
```
]
---
class: inverse, center, middle
# Interactive data dashboards
![:custom_hr]()
## .font160[~30 min]
---
# Interactive data dashboards
- Data dashboards are information management tools used to visually display, track, analyze and share related data visualizations and that meet a particular objective. They can be viewed as interactive reports.
--
- Multiple types of dashboards, for example:
- Operational dashboards
- Strategic dashboards
- Analytical dashboards
--
- Can be automatically updated as new data becomes available
---
# Interactive data dashboards in `r rfa("r-project")`
## `r rfa("r-project")` package
[`flexdashboard`](https://cran.r-project.org/package=flexdashboard) .font90[05/2016 (0.2.0) // 06/2020 (0.5.2)]
- Uses [R Markdown](https://rmarkdown.rstudio.com/) to create a grid dashboard that adapts the size of the dashboard components to the containing web page.
--
- Custom layouts based on rows and columns in a R Markdown document
--
- Can contain a wide variety of components:
- htmlwidgets
- base
- lattice
- grid
- tabular data
- text
---
# Interactive data dashboards in `r rfa("r-project")`
<iframe src="https://beta.rstudioconnect.com/jjallaire/htmlwidgets-highcharter/htmlwidgets-highcharter.html#sales-by-category" style="width:100%; height:65vh;"></iframe>
---
# A (very!) quick intro to R Markdown<sup>*</sup>
.font70[<sup>*</sup>See [R Markdown](https://rmarkdown.rstudio.com/) documentation for more details and [here](https://insileco.github.io/ResearchDown/) for a more complete presentation]
## R Markdown universe
R Markdown produces dynamic document in a variety of format.
### Opens new possibilities with R:
* make CV, poster ([`posterdown`](https://CRAN.R-project.org/package=posterdown)) & online presentation ([`xaringan`](https://CRAN.R-project.org/package=xaringan))
* format scientific paper ([`rticles`](https://CRAN.R-project.org/package=rticles))
* help to document your research through notebook ([`bookdown`](https://CRAN.R-project.org/package=bookdown))
* build an academic blog or website ([`blogdown`](https://CRAN.R-project.org/package=blogdown))
---
# A (very!) quick intro to R Markdown<sup>*</sup>
.font70[<sup>*</sup>See [R Markdown](https://rmarkdown.rstudio.com/) documentation for more details and [here](https://insileco.github.io/ResearchDown/) for a more complete presentation]
## But what's R Markdown?
- ["An authoring framework for data science."](https://rmarkdown.rstudio.com/lesson-1.html) (✔️)
- [A document format (.Rmd).](https://bookdown.org/yihui/rmarkdown/) (✔️)
- [An R package named rmarkdown.](https://rmarkdown.rstudio.com/docs/) (✔️)
- ["A tool for integrating text, code, and results."](https://r4ds.had.co.nz/communicate-intro.html) (✔️)
- ["A file format for making dynamic documents with R."](https://rmarkdown.rstudio.com/articles_intro.html) (✔️)
- ["A computational document."](http://radar.oreilly.com/2011/07/wolframs-computational-documen.html) (✔️)
- [Wizardry.](https://raw.githubusercontent.com/allisonhorst/stats-illustrations/master/rstats-artwork/rmarkdown_wizards.png) (🧙)
---
# A (very!) quick intro to R Markdown<sup>*</sup>
.font70[<sup>*</sup>See [R Markdown](https://rmarkdown.rstudio.com/) documentation for more details and [here](https://insileco.github.io/ResearchDown/) for a more complete presentation]
## How does R Markdown work?
.pull-left[
```{r echo=FALSE, out.width="100%", purl = FALSE}
knitr::include_graphics("img/Rmdformat.png")
```
]
.pull-right[
- 🤷
- I press knit, a document appears, and I believe that anything happening in between could be actual magic.
- `knitr` executes the code and converts `.Rmd` to `.md`; Pandoc renders the `.md` file to the output format you want.
]
---
# Package [`flexdashboard`](https://cran.r-project.org/package=flexdashboard)
```{r, eval = FALSE}
library(rmarkdown)
library(flexdashboard)
draft("dashboard.Rmd",
template = "flex_dashboard",
package = "flexdashboard")
```
Creates a `.Rmd` document with a dashboard layout
---
# Package [`flexdashboard`](https://cran.r-project.org/package=flexdashboard)