Skip to content

Commit

Permalink
use yyjonsr instead of jsonify/geojsonsf, use correct options, fix te…
Browse files Browse the repository at this point in the history
…sts for popup = FALSE
  • Loading branch information
trafficonese committed Jun 11, 2024
1 parent c3c3a97 commit b509362
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 227 deletions.
4 changes: 1 addition & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@ Encoding: UTF-8
LazyData: false
RoxygenNote: 7.3.1
Imports:
geojsonsf,
htmltools,
jsonify,
leaflet,
sf,
yyjsonr,
grDevices
Suggests:
colourvalues,
shiny,
yyjsonr,
testthat (>= 2.1.0)
5 changes: 1 addition & 4 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ leafgl 0.2.1.9005 (2023-09-08)

features and improvements

* Switched from `jsonify` and `geojsonsf` to `yyjsonr`
* New method *clearGlGroup* removes a group from leaflet and the Leaflet.Glify instances.
* The JavaScript methods of the `removeGl**` functions was rewritten to correctly remove an element identified by `layerId`
* `clearGlLayers` now correctly removes all Leaflet.Glify instances
Expand All @@ -24,10 +25,6 @@ documentation etc
miscellaneous

* unified / simplified the dependency functions/calls
* add option `leafgl_json_parser` to change JSON-parser via options. Possible values are:
- `jsonify` (default)
- `yyjsonr`
- Custom JSON converter like `jsonlite::toJSON` for example.


leafgl 0.2.1
Expand Down
35 changes: 4 additions & 31 deletions R/glify-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,37 +60,10 @@ glifyAttachmentSrc <- function(fl, group, type) {


# helpers
json_funccall <- function() {
json_parser <- getOption("leafgl_json_parser", "jsonify") # Default to jsonify
if (is.function(json_parser)) {
json_parser
} else if (json_parser == "yyjsonr") {
yyjsonr::write_json_str
} else {
jsonify::to_json
}
}
convert_to_json <- function(data, ...) {
json_parser <- getOption("leafgl_json_parser", "jsonify") # Default to jsonify
if (is.function(json_parser)) {
json_data <- json_parser(data, ...)
} else if (json_parser == "yyjsonr") {
json_data <- yyjsonr::write_json_str(data, ...)
class(json_data) <- "json"
} else {
json_data <- jsonify::to_json(data, ...)
}
return(json_data)
}
geojson_funccall <- function() {
json_parser <- getOption("leafgl_geojson_parser", "geojsonsf") # Default to geojsonsf
if (is.function(json_parser)) {
json_parser
} else if (json_parser == "yyjsonr") {
yyson_geojson_str
} else {
geojsonsf::sf_geojson
}
yyson_json_str <- function(x, ...) {
dt <- yyjsonr::write_json_str(x, ...)
class(dt) <- "json"
dt
}
yyson_geojson_str <- function(x, ...) {
dt <- yyjsonr::write_geojson_str(x, ...)
Expand Down
20 changes: 10 additions & 10 deletions R/glify-lines.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ addGlPolylines = function(map,
if (ncol(color) != 3) stop("only 3 column color matrix supported so far")
color = as.data.frame(color, stringsAsFactors = FALSE)
colnames(color) = c("r", "g", "b")
cols = convert_to_json(color, digits = 3)
cols = yyson_json_str(color, digits = 3)

# label / popup ########
labels <- leaflet::evalFormula(label, data)
Expand All @@ -96,7 +96,7 @@ addGlPolylines = function(map,
htmldeps
)
}
popup = convert_to_json(makePopup(popup, data))
popup = yyson_json_str(makePopup(popup, data))
geom = sf::st_geometry(data)
data = sf::st_sf(id = 1:length(geom), geometry = geom)
}
Expand All @@ -108,15 +108,15 @@ addGlPolylines = function(map,
geojsonsf_args = try(
match.arg(
names(dotopts)
, names(as.list(args(geojson_funccall)))
, names(as.list(args(yyjsonr::opts_write_json)))
, several.ok = TRUE
)
, silent = TRUE
)
if (inherits(geojsonsf_args, "try-error")) geojsonsf_args = NULL
if (identical(geojsonsf_args, "sf")) geojsonsf_args = NULL
}
data = do.call(geojson_funccall(), c(list(data), dotopts[geojsonsf_args]))
data = do.call(yyson_geojson_str, c(list(data), dotopts[geojsonsf_args]))

# dependencies
map$dependencies = c(map$dependencies, glifyDependencies())
Expand Down Expand Up @@ -190,14 +190,14 @@ addGlPolylinesSrc = function(map,
jsonify_args = try(
match.arg(
names(dotopts)
, names(as.list(args(geojson_funccall)))
, names(as.list(args(yyjsonr::opts_write_json)))
, several.ok = TRUE
)
, silent = TRUE
)
if (inherits(jsonify_args, "try-error")) jsonify_args = NULL
if (identical(jsonify_args, "sf")) jsonify_args = NULL
cat('[', do.call(geojson_funccall(), c(list(data), dotopts[jsonify_args])), '];',
cat('[', do.call(yyson_geojson_str, c(list(data), dotopts[jsonify_args])), '];',
file = fl_data, sep = "", append = TRUE)

map$dependencies = c(
Expand All @@ -220,7 +220,7 @@ addGlPolylinesSrc = function(map,
fl_color = paste0(dir_color, "/", group, "_color.js")
pre = paste0('var col = col || {}; col["', group, '"] = ')
writeLines(pre, fl_color)
cat('[', convert_to_json(color, digits = 3), '];',
cat('[', yyson_json_str(color, digits = 3), '];',
file = fl_color, append = TRUE)

map$dependencies = c(map$dependencies, glifyAttachmentSrc(fl_color, group, "col"))
Expand All @@ -232,7 +232,7 @@ addGlPolylinesSrc = function(map,
fl_label = paste0(dir_labels, "/", group, "_label.js")
pre = paste0('var labs = labs || {}; labs["', group, '"] = ')
writeLines(pre, fl_label)
cat('[', convert_to_json(leaflet::evalFormula(label, data_orig)), '];',
cat('[', yyson_json_str(leaflet::evalFormula(label, data_orig)), '];',
file = fl_label, append = TRUE)

map$dependencies = c(map$dependencies, glifyAttachmentSrc(fl_label, group, "lab"))
Expand All @@ -251,7 +251,7 @@ addGlPolylinesSrc = function(map,
fl_popup = paste0(dir_popup, "/", group, "_popup.js")
pre = paste0('var pops = pops || {}; pops["', group, '"] = ')
writeLines(pre, fl_popup)
cat('[', convert_to_json(makePopup(popup, data_orig)), '];',
cat('[', yyson_json_str(makePopup(popup, data_orig)), '];',
file = fl_popup, append = TRUE)

map$dependencies = c(map$dependencies, glifyAttachmentSrc(fl_popup, group, "pop"))
Expand All @@ -263,7 +263,7 @@ addGlPolylinesSrc = function(map,
fl_weight = paste0(dir_weight, "/", group, "_weight.js")
pre = paste0('var wgt = wgt || {}; wgt["', group, '"] = ')
writeLines(pre, fl_weight)
cat('[', convert_to_json(weight), '];',
cat('[', yyson_json_str(weight), '];',
file = fl_weight, append = TRUE)

map$dependencies = c(
Expand Down
43 changes: 22 additions & 21 deletions R/glify-points.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
#' @param weight line width/thickness in pixels for \code{addGlPolylines}.
#' @param src whether to pass data to the widget via file attachments.
#' @param pane A string which defines the pane of the layer. The default is \code{"overlayPane"}.
#' @param ... Used to pass additional named arguments to \code{\link[jsonify]{to_json}}
#' & to pass additional arguments to the underlying JavaScript functions. Typical
#' use-cases include setting \code{'digits'} to round the point coordinates or to pass
#' a different \code{'fragmentShaderSource'} to control the shape of the points. Use
#' @param ... Used to pass additional named arguments to \code{\link[yyjsonr]{write_json_str}} or
#' \code{\link[yyjsonr]{write_geojson_str}} & to pass additional arguments to the
#' underlying JavaScript functions. Typical use-cases include setting \code{'digits'} to
#' round the point coordinates or to pass a different \code{'fragmentShaderSource'} to
#' control the shape of the points. Use
#' \itemize{
#' \item{\code{'point'} (default) to render circles with a thin black outline}
#' \item{\code{'simpleCircle'} for circles without outline}
Expand Down Expand Up @@ -146,7 +147,7 @@ addGlPoints = function(map,
if (ncol(fillColor) != 3) stop("only 3 column fillColor matrix supported so far")
fillColor = as.data.frame(fillColor, stringsAsFactors = FALSE)
colnames(fillColor) = c("r", "g", "b")
fillColor = convert_to_json(fillColor, digits = 3)
fillColor = yyson_json_str(fillColor, digits = 3)

# label / popup ###########
labels <- leaflet::evalFormula(label, data)
Expand All @@ -158,7 +159,7 @@ addGlPoints = function(map,
htmldeps
)
}
popup = convert_to_json(makePopup(popup, data))
popup = yyson_json_str(makePopup(popup, data))
} else {
popup = NULL
}
Expand All @@ -171,14 +172,14 @@ addGlPoints = function(map,
jsonify_args = try(
match.arg(
names(dotopts)
, names(as.list(args(json_funccall)))
, names(as.list(args(yyjsonr::opts_write_json)))
, several.ok = TRUE
)
, silent = TRUE
)
}
if (inherits(jsonify_args, "try-error")) jsonify_args = NULL
data = do.call(json_funccall(), c(list(crds), dotopts[jsonify_args]))
data = do.call(yyson_json_str, c(list(crds), dotopts[jsonify_args]))
class(data) <- "json"

# dependencies
Expand Down Expand Up @@ -252,14 +253,14 @@ addGlPointsSrc = function(map,
jsonify_args = try(
match.arg(
names(dotopts)
, names(as.list(args(json_funccall)))
, names(as.list(args(yyjsonr::opts_write_json)))
, several.ok = TRUE
)
, silent = TRUE
)
if (inherits(jsonify_args, "try-error")) jsonify_args = NULL
if (identical(jsonify_args, "x")) jsonify_args = NULL
cat('[', do.call(json_funccall(), c(list(crds), dotopts[jsonify_args])), '];',
cat('[', do.call(yyson_json_str, c(list(crds), dotopts[jsonify_args])), '];',
file = fl_data, sep = "", append = TRUE)

map$dependencies = c(
Expand All @@ -283,7 +284,7 @@ addGlPointsSrc = function(map,
fl_color = paste0(dir_color, "/", group, "_color.js")
pre = paste0('var col = col || {}; col["', group, '"] = ')
writeLines(pre, fl_color)
cat('[', convert_to_json(fillColor, digits = 3), '];',
cat('[', yyson_json_str(fillColor, digits = 3), '];',
file = fl_color, append = TRUE)

map$dependencies = c(map$dependencies, glifyAttachmentSrc(fl_color, group, "col"))
Expand All @@ -295,7 +296,7 @@ addGlPointsSrc = function(map,
fl_label = paste0(dir_labels, "/", group, "_label.js")
pre = paste0('var labs = labs || {}; labs["', group, '"] = ')
writeLines(pre, fl_label)
cat('[', convert_to_json(leaflet::evalFormula(label, data)), '];',
cat('[', yyson_json_str(leaflet::evalFormula(label, data)), '];',
file = fl_label, append = TRUE)

map$dependencies = c(map$dependencies, glifyAttachmentSrc(fl_label, group, "lab"))
Expand All @@ -314,7 +315,7 @@ addGlPointsSrc = function(map,
fl_popup = paste0(dir_popup, "/", group, "_popup.js")
pre = paste0('var pops = pops || {}; pops["', group, '"] = ')
writeLines(pre, fl_popup)
cat('[', convert_to_json(makePopup(popup, data)), '];',
cat('[', yyson_json_str(makePopup(popup, data)), '];',
file = fl_popup, append = TRUE)

map$dependencies = c(map$dependencies, glifyAttachmentSrc(fl_popup, group, "pop"))
Expand All @@ -326,7 +327,7 @@ addGlPointsSrc = function(map,
fl_radius = paste0(dir_radius, "/", layerId, "_radius.js")
pre = paste0('var rad = rad || {}; rad["', layerId, '"] = ')
writeLines(pre, fl_radius)
cat('[', convert_to_json(radius), '];',
cat('[', yyson_json_str(radius), '];',
file = fl_radius, append = TRUE)

map$dependencies = c(map$dependencies, glifyAttachmentSrc(fl_radius, group, "rad"))
Expand Down Expand Up @@ -391,11 +392,11 @@ addGlPointsSrc = function(map,
# fl_data2 = paste0(dir_data, "/", grp2, "_data.json")
# pre1 = paste0('var data = data || {}; data["', grp1, '"] = ')
# writeLines(pre1, fl_data1)
# cat('[', jsonify::to_json(crds[1:100, ], ...), '];',
# cat('[', yyson_json_str(crds[1:100, ], ...), '];',
# file = fl_data1, sep = "", append = TRUE)
# pre2 = paste0('var data = data || {}; data["', grp2, '"] = ')
# writeLines(pre2, fl_data2)
# cat('[', jsonify::to_json(crds[101:nrow(crds), ], ...), '];',
# cat('[', yyson_json_str(crds[101:nrow(crds), ], ...), '];',
# file = fl_data2, sep = "", append = TRUE)
#
# # color
Expand All @@ -406,15 +407,15 @@ addGlPointsSrc = function(map,
# fl_color = paste0(dir_color, "/", group, "_color.json")
# pre = paste0('var col = col || {}; col["', group, '"] = ')
# writeLines(pre, fl_color)
# cat('[', jsonify::to_json(color), '];',
# cat('[', yyson_json_str(color), '];',
# file = fl_color, append = TRUE)
#
# # popup
# if (!is.null(popup)) {
# fl_popup = paste0(dir_popup, "/", group, "_popup.json")
# pre = paste0('var popup = popup || {}; popup["', group, '"] = ')
# writeLines(pre, fl_popup)
# cat('[', jsonify::to_json(data[[popup]]), '];',
# cat('[', yyson_json_str(data[[popup]]), '];',
# file = fl_popup, append = TRUE)
# } else {
# popup = NULL
Expand Down Expand Up @@ -468,22 +469,22 @@ addGlPointsSrc = function(map,
# crds = sf::st_coordinates(data)[, c(2, 1)]
#
# fl_data = paste0(dir_data, "/", group, "_data.json")
# cat(jsonify::to_json(crds, digits = 7), file = fl_data, append = FALSE)
# cat(yyson_json_str(crds, digits = 7), file = fl_data, append = FALSE)
# data_var = paste0(group, "dt")
#
# # color
# if (ncol(color) != 3) stop("only 3 column color matrix supported so far")
# color = as.data.frame(color, stringsAsFactors = FALSE)
# colnames(color) = c("r", "g", "b")
#
# jsn = jsonify::to_json(color)
# jsn = yyson_json_str(color)
# fl_color = paste0(dir_color, "/", group, "_color.json")
# color_var = paste0(group, "cl")
# cat(jsn, file = fl_color, append = FALSE)
#
# # popup
# if (!is.null(popup)) {
# pop = jsonify::to_json(data[[popup]])
# pop = yyson_json_str(data[[popup]])
# fl_popup = paste0(dir_popup, "/", group, "_popup.json")
# popup_var = paste0(group, "pop")
# cat(pop, file = fl_popup, append = FALSE)
Expand Down
Loading

0 comments on commit b509362

Please sign in to comment.