|
@@ -0,0 +1,125 @@
|
|
1
|
+# Courtesy timelyportfolio on github:
|
|
2
|
+# https://github.com/rstudio/leaflet/issues/496#issuecomment-650122985
|
|
3
|
+### R functions
|
|
4
|
+# add in methods from https://github.com/rstudio/leaflet/pull/598
|
|
5
|
+setCircleMarkerRadius <- function(map, layerId, radius, data=getMapData(map)){
|
|
6
|
+ options <- list(layerId = layerId, radius = radius)
|
|
7
|
+ # evaluate all options
|
|
8
|
+ options <- evalFormula(options, data = data)
|
|
9
|
+ # make them the same length (by building a data.frame)
|
|
10
|
+ options <- do.call(data.frame, c(options, list(stringsAsFactors=FALSE)))
|
|
11
|
+ leaflet::invokeMethod(map, data, "setRadius", options$layerId, options$radius)
|
|
12
|
+}
|
|
13
|
+
|
|
14
|
+setCircleMarkerStyle <- function(map, layerId
|
|
15
|
+ , radius = NULL
|
|
16
|
+ , stroke = NULL
|
|
17
|
+ , color = NULL
|
|
18
|
+ , weight = NULL
|
|
19
|
+ , opacity = NULL
|
|
20
|
+ , fill = NULL
|
|
21
|
+ , fillColor = NULL
|
|
22
|
+ , fillOpacity = NULL
|
|
23
|
+ , dashArray = NULL
|
|
24
|
+ , options = NULL
|
|
25
|
+ , data = getMapData(map)
|
|
26
|
+){
|
|
27
|
+ if (!is.null(radius)){
|
|
28
|
+ setCircleMarkerRadius(map, layerId = layerId, radius = radius, data = data)
|
|
29
|
+ }
|
|
30
|
+
|
|
31
|
+ options <- c(list(layerId = layerId),
|
|
32
|
+ options,
|
|
33
|
+ filterNULL(list(stroke = stroke, color = color,
|
|
34
|
+ weight = weight, opacity = opacity,
|
|
35
|
+ fill = fill, fillColor = fillColor,
|
|
36
|
+ fillOpacity = fillOpacity, dashArray = dashArray
|
|
37
|
+ )))
|
|
38
|
+
|
|
39
|
+ if (length(options) < 2) { # no style options set
|
|
40
|
+ return()
|
|
41
|
+ }
|
|
42
|
+ # evaluate all options
|
|
43
|
+ options <- evalFormula(options, data = data)
|
|
44
|
+
|
|
45
|
+ # make them the same length (by building a data.frame)
|
|
46
|
+ options <- do.call(data.frame, c(options, list(stringsAsFactors=FALSE)))
|
|
47
|
+ layerId <- options[[1]]
|
|
48
|
+ style <- options[-1] # drop layer column
|
|
49
|
+
|
|
50
|
+ #print(list(style=style))
|
|
51
|
+ leaflet::invokeMethod(map, data, "setStyle", "marker", layerId, style);
|
|
52
|
+}
|
|
53
|
+
|
|
54
|
+setShapeStyle <- function( map, data = getMapData(map), layerId,
|
|
55
|
+ stroke = NULL, color = NULL,
|
|
56
|
+ weight = NULL, opacity = NULL,
|
|
57
|
+ fill = NULL, fillColor = NULL,
|
|
58
|
+ fillOpacity = NULL, dashArray = NULL,
|
|
59
|
+ smoothFactor = NULL, noClip = NULL,
|
|
60
|
+ options = NULL
|
|
61
|
+){
|
|
62
|
+ options <- c(list(layerId = layerId),
|
|
63
|
+ options,
|
|
64
|
+ filterNULL(list(stroke = stroke, color = color,
|
|
65
|
+ weight = weight, opacity = opacity,
|
|
66
|
+ fill = fill, fillColor = fillColor,
|
|
67
|
+ fillOpacity = fillOpacity, dashArray = dashArray,
|
|
68
|
+ smoothFactor = smoothFactor, noClip = noClip
|
|
69
|
+ )))
|
|
70
|
+ # evaluate all options
|
|
71
|
+ options <- evalFormula(options, data = data)
|
|
72
|
+ # make them the same length (by building a data.frame)
|
|
73
|
+ options <- do.call(data.frame, c(options, list(stringsAsFactors=FALSE)))
|
|
74
|
+
|
|
75
|
+ layerId <- options[[1]]
|
|
76
|
+ style <- options[-1] # drop layer column
|
|
77
|
+
|
|
78
|
+ #print(list(style=style))
|
|
79
|
+ leaflet::invokeMethod(map, data, "setStyle", "shape", layerId, style);
|
|
80
|
+}
|
|
81
|
+
|
|
82
|
+### JS methods
|
|
83
|
+leafletjs <- tags$head(
|
|
84
|
+ # add in methods from https://github.com/rstudio/leaflet/pull/598
|
|
85
|
+ tags$script(HTML(
|
|
86
|
+ '
|
|
87
|
+window.LeafletWidget.methods.setStyle = function(category, layerId, style){
|
|
88
|
+ var map = this;
|
|
89
|
+ if (!layerId){
|
|
90
|
+ return;
|
|
91
|
+ } else if (!(typeof(layerId) === "object" && layerId.length)){ // in case a single layerid is given
|
|
92
|
+ layerId = [layerId];
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ //convert columnstore to row store
|
|
96
|
+ style = HTMLWidgets.dataframeToD3(style);
|
|
97
|
+ //console.log(style);
|
|
98
|
+
|
|
99
|
+ layerId.forEach(function(d,i){
|
|
100
|
+ var layer = map.layerManager.getLayer(category, d);
|
|
101
|
+ if (layer){ // or should this raise an error?
|
|
102
|
+ layer.setStyle(style[i]);
|
|
103
|
+ }
|
|
104
|
+ });
|
|
105
|
+};
|
|
106
|
+
|
|
107
|
+window.LeafletWidget.methods.setRadius = function(layerId, radius){
|
|
108
|
+ var map = this;
|
|
109
|
+ if (!layerId){
|
|
110
|
+ return;
|
|
111
|
+ } else if (!(typeof(layerId) === "object" && layerId.length)){ // in case a single layerid is given
|
|
112
|
+ layerId = [layerId];
|
|
113
|
+ radius = [radius];
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ layerId.forEach(function(d,i){
|
|
117
|
+ var layer = map.layerManager.getLayer("marker", d);
|
|
118
|
+ if (layer){ // or should this raise an error?
|
|
119
|
+ layer.setRadius(radius[i]);
|
|
120
|
+ }
|
|
121
|
+ });
|
|
122
|
+};
|
|
123
|
+'
|
|
124
|
+ ))
|
|
125
|
+)
|