Skip to content

Commit 7af1dcc

Browse files
authored
Merge pull request #105 from trafficonese/hexcolor
Allow hexadecimal colors (with opacities) and contextmenu
2 parents b5c8bb1 + c3026f0 commit 7af1dcc

11 files changed

Lines changed: 307 additions & 60 deletions

File tree

R/glify-lines.R

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ addGlPolylines = function(map,
2626
pane = "overlayPane",
2727
popupOptions = NULL,
2828
labelOptions = NULL,
29+
contextMenu = NULL,
2930
...) {
3031

3132
# check data ##########
@@ -70,16 +71,21 @@ addGlPolylines = function(map,
7071
bounds = as.numeric(sf::st_bbox(data))
7172

7273
# color ########
73-
palette = "viridis"
74-
if ("palette" %in% names(dotopts)) {
75-
palette <- dotopts$palette
76-
dotopts$palette = NULL
74+
if (inherits(color[1], "character") && startsWith(color[1], "#")) {
75+
cols <- color
76+
cols <- if(length(cols) == 1) {list(cols)} else { cols }
77+
} else {
78+
palette = "viridis"
79+
if ("palette" %in% names(dotopts)) {
80+
palette <- dotopts$palette
81+
dotopts$palette = NULL
82+
}
83+
color <- makeColorMatrix(color, data, palette = palette)
84+
if (ncol(color) != 3) stop("only 3 column color matrix supported so far")
85+
color = as.data.frame(color, stringsAsFactors = FALSE)
86+
colnames(color) = c("r", "g", "b")
87+
cols = yyson_json_str(color, digits = 3)
7788
}
78-
color <- makeColorMatrix(color, data, palette = palette)
79-
if (ncol(color) != 3) stop("only 3 column color matrix supported so far")
80-
color = as.data.frame(color, stringsAsFactors = FALSE)
81-
colnames(color) = c("r", "g", "b")
82-
cols = yyson_json_str(color, digits = 3)
8389

8490
# label / popup ########
8591
labels <- leaflet::evalFormula(label, data)
@@ -116,7 +122,7 @@ addGlPolylines = function(map,
116122
if (inherits(geojsonsf_args, "try-error")) geojsonsf_args = NULL
117123
if (identical(geojsonsf_args, "sf")) geojsonsf_args = NULL
118124
}
119-
data = do.call(yyson_geojson_str, c(list(data), dotopts[geojsonsf_args]))
125+
data = do.call(yyson_geojson_str, c(list(data), "json_opts" = list(dotopts[geojsonsf_args])))
120126

121127
# dependencies
122128
map$dependencies = c(map$dependencies, glifyDependencies())
@@ -138,6 +144,7 @@ addGlPolylines = function(map,
138144
, pane
139145
, popupOptions
140146
, labelOptions
147+
, contextMenu
141148
)
142149

143150
leaflet::expandLimits(
@@ -187,17 +194,23 @@ addGlPolylinesSrc = function(map,
187194
fl_data = paste0(dir_data, "/", group, "_data.js")
188195
pre = paste0('var data = data || {}; data["', group, '"] = ')
189196
writeLines(pre, fl_data)
190-
jsonify_args = try(
191-
match.arg(
192-
names(dotopts)
193-
, names(as.list(args(yyjsonr::opts_write_json)))
194-
, several.ok = TRUE
197+
if (length(dotopts) != 0) {
198+
jsonify_args = try(
199+
match.arg(
200+
names(dotopts)
201+
, names(as.list(args(yyjsonr::opts_write_json)))
202+
, several.ok = TRUE
203+
)
204+
, silent = TRUE
195205
)
196-
, silent = TRUE
197-
)
198-
if (inherits(jsonify_args, "try-error")) jsonify_args = NULL
199-
if (identical(jsonify_args, "sf")) jsonify_args = NULL
200-
cat('[', do.call(yyson_geojson_str, c(list(data), dotopts[jsonify_args])), '];',
206+
if (inherits(jsonify_args, "try-error")) jsonify_args = NULL
207+
if (identical(jsonify_args, "sf")) jsonify_args = NULL
208+
209+
dotoptslist <- c("json_opts" = list(dotopts[jsonify_args]))
210+
} else {
211+
dotoptslist <- NULL
212+
}
213+
cat('[', do.call(yyson_geojson_str, c(list(data), dotoptslist)), '];',
201214
file = fl_data, sep = "", append = TRUE)
202215

203216
map$dependencies = c(

R/glify-points.R

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#' @param weight line width/thickness in pixels for \code{addGlPolylines}.
2020
#' @param src whether to pass data to the widget via file attachments.
2121
#' @param pane A string which defines the pane of the layer. The default is \code{"overlayPane"}.
22+
#' @param contextMenu a \code{\link[htmlwidgets]{JS}} function, that is passed to contextmenu.
23+
#' See the example in \code{./inst/examples/contextmenu.R}
2224
#' @param ... Used to pass additional named arguments to \code{\link[yyjsonr]{write_json_str}} or
2325
#' \code{\link[yyjsonr]{write_geojson_str}} & to pass additional arguments to the
2426
#' underlying JavaScript functions. Typical use-cases include setting \code{'digits'} to
@@ -99,6 +101,7 @@ addGlPoints = function(map,
99101
pane = "overlayPane",
100102
popupOptions = NULL,
101103
labelOptions = NULL,
104+
contextMenu = NULL,
102105
...) {
103106

104107
# check data ##########
@@ -140,16 +143,20 @@ addGlPoints = function(map,
140143
bounds = as.numeric(sf::st_bbox(data))
141144

142145
# color ###########
143-
palette = "viridis"
144-
if ("palette" %in% names(dotopts)) {
145-
palette <- dotopts$palette
146-
dotopts$palette = NULL
146+
if (inherits(fillColor[1], "character") && startsWith(fillColor[1], "#")) {
147+
fillColor <- if(length(fillColor) == 1) {list(fillColor)} else { fillColor }
148+
} else {
149+
palette = "viridis"
150+
if ("palette" %in% names(dotopts)) {
151+
palette <- dotopts$palette
152+
dotopts$palette = NULL
153+
}
154+
fillColor <- makeColorMatrix(fillColor, data, palette = palette)
155+
if (ncol(fillColor) != 3) stop("only 3 column fillColor matrix supported so far")
156+
fillColor = as.data.frame(fillColor, stringsAsFactors = FALSE)
157+
colnames(fillColor) = c("r", "g", "b")
158+
fillColor = yyson_json_str(fillColor, digits = 3)
147159
}
148-
fillColor <- makeColorMatrix(fillColor, data, palette = palette)
149-
if (ncol(fillColor) != 3) stop("only 3 column fillColor matrix supported so far")
150-
fillColor = as.data.frame(fillColor, stringsAsFactors = FALSE)
151-
colnames(fillColor) = c("r", "g", "b")
152-
fillColor = yyson_json_str(fillColor, digits = 3)
153160

154161
# label / popup ###########
155162
labels <- leaflet::evalFormula(label, data)
@@ -204,6 +211,7 @@ addGlPoints = function(map,
204211
, pane
205212
, popupOptions
206213
, labelOptions
214+
, contextMenu
207215
)
208216

209217
leaflet::expandLimits(

R/glify-polygons.R

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ addGlPolygons = function(map,
2828
stroke = TRUE,
2929
popupOptions = NULL,
3030
labelOptions = NULL,
31+
contextMenu = NULL,
3132
...) {
3233

3334
# check data ##########
@@ -73,16 +74,21 @@ addGlPolygons = function(map,
7374
bounds = as.numeric(sf::st_bbox(data))
7475

7576
# fillColor ###########
76-
palette = "viridis"
77-
if ("palette" %in% names(dotopts)) {
78-
palette <- dotopts$palette
79-
dotopts$palette = NULL
77+
if (inherits(fillColor[1], "character") && startsWith(fillColor[1], "#")) {
78+
cols <- color
79+
cols <- if(length(cols) == 1) {list(cols)} else { cols }
80+
} else {
81+
palette = "viridis"
82+
if ("palette" %in% names(dotopts)) {
83+
palette <- dotopts$palette
84+
dotopts$palette = NULL
85+
}
86+
fillColor <- makeColorMatrix(fillColor, data, palette = palette)
87+
if (ncol(fillColor) != 3) stop("only 3 column fillColor matrix supported so far")
88+
fillColor = as.data.frame(fillColor, stringsAsFactors = FALSE)
89+
colnames(fillColor) = c("r", "g", "b")
90+
cols = yyson_json_str(fillColor, digits = 3)
8091
}
81-
fillColor <- makeColorMatrix(fillColor, data, palette = palette)
82-
if (ncol(fillColor) != 3) stop("only 3 column fillColor matrix supported so far")
83-
fillColor = as.data.frame(fillColor, stringsAsFactors = FALSE)
84-
colnames(fillColor) = c("r", "g", "b")
85-
cols = yyson_json_str(fillColor, digits = 3)
8692

8793
# label / popup ###########
8894
labels <- leaflet::evalFormula(label, data)
@@ -120,7 +126,7 @@ addGlPolygons = function(map,
120126
if (inherits(geojsonsf_args, "try-error")) geojsonsf_args = NULL
121127
if (identical(geojsonsf_args, "sf")) geojsonsf_args = NULL
122128
}
123-
data = do.call(yyson_geojson_str, c(list(data), dotopts[geojsonsf_args]))
129+
data = do.call(yyson_geojson_str, c(list(data), "json_opts" = list(dotopts[geojsonsf_args])))
124130

125131
# dependencies
126132
map$dependencies = c(map$dependencies, glifyDependencies())
@@ -142,6 +148,7 @@ addGlPolygons = function(map,
142148
, stroke
143149
, popupOptions
144150
, labelOptions
151+
, contextMenu
145152
)
146153

147154
leaflet::expandLimits(
@@ -191,17 +198,22 @@ addGlPolygonsSrc = function(map,
191198
fl_data = paste0(dir_data, "/", group, "_data.js")
192199
pre = paste0('var data = data || {}; data["', group, '"] = ')
193200
writeLines(pre, fl_data)
194-
jsonify_args = try(
195-
match.arg(
196-
names(dotopts)
197-
, names(as.list(args(yyjsonr::opts_write_json)))
198-
, several.ok = TRUE
201+
if (length(dotopts) != 0) {
202+
jsonify_args = try(
203+
match.arg(
204+
names(dotopts)
205+
, names(as.list(args(yyjsonr::opts_write_json)))
206+
, several.ok = TRUE
207+
)
208+
, silent = TRUE
199209
)
200-
, silent = TRUE
201-
)
202-
if (inherits(jsonify_args, "try-error")) jsonify_args = NULL
203-
if (identical(jsonify_args, "sf")) jsonify_args = NULL
204-
cat('[', do.call(yyson_geojson_str, c(list(data), dotopts[jsonify_args])), '];',
210+
if (inherits(jsonify_args, "try-error")) jsonify_args = NULL
211+
if (identical(jsonify_args, "sf")) jsonify_args = NULL
212+
dotoptslist <- c("json_opts" = list(dotopts[jsonify_args]))
213+
} else {
214+
dotoptslist <- NULL
215+
}
216+
cat('[', do.call(yyson_geojson_str, c(list(data), dotoptslist)), '];',
205217
file = fl_data, sep = "", append = TRUE)
206218

207219
map$dependencies = c(

0 commit comments

Comments
 (0)