From 3f48a22c721a234013cf8fc60b0dc8d7577cdace Mon Sep 17 00:00:00 2001 From: munoztd0 Date: Tue, 22 Aug 2023 17:00:47 +0200 Subject: [PATCH] feat: :sparkles: add possibility to Whether to wrap pure JS functions in \code{htmlwidgets::JS} for echartsToJson --- R/helpers.R | 14 +++++++++++++- man/echartsNJSON.Rd | 4 +++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/R/helpers.R b/R/helpers.R index e8ef4b5b..ea51e943 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -304,6 +304,7 @@ e_aria <- function(e, enabled = TRUE, ...) { #' @inheritParams e_bar #' @param json Whether to return the JSON, otherwise returns a \code{list}. #' @param txt JSON character string, url, or file. +#' @param jswrapper Whether to wrap pure JS functions in \code{htmlwidgets::JS}, Default is FALSE. #' @param ... Additional options to pass to \link[=jsonlite]{toJSON}. #' #' @details \code{txt} should contain the full list of options required to build a chart. @@ -348,10 +349,21 @@ e_inspect <- function(e, json = FALSE, ...) { #' @rdname echartsNJSON #' @export -echarts_from_json <- function(txt) { +echarts_from_json <- function(txt, jswrapper = FALSE) { json <- jsonlite::fromJSON(txt, simplifyVector = FALSE) e_charts() -> e e$x$opts <- json + if (isTRUE(jswrapper)) { + for (i in seq_along(e$x$opts$tooltip)) { + + if (is.character(e$x$opts$tooltip[[i]]) && startsWith(e$x$opts$tooltip[[i]], "function")) { + # Wrap pure JS functions in htmlwidgets::JS + e$x$opts$tooltip[[i]] <- htmlwidgets::JS(e$x$opts$tooltip[[i]]) + } + + } + } + e } diff --git a/man/echartsNJSON.Rd b/man/echartsNJSON.Rd index 4d102ad6..41ab5c8d 100644 --- a/man/echartsNJSON.Rd +++ b/man/echartsNJSON.Rd @@ -7,7 +7,7 @@ \usage{ e_inspect(e, json = FALSE, ...) -echarts_from_json(txt) +echarts_from_json(txt, jswrapper = FALSE) } \arguments{ \item{e}{An \code{echarts4r} object as returned by \code{\link{e_charts}} or @@ -18,6 +18,8 @@ a proxy as returned by \code{\link{echarts4rProxy}}.} \item{...}{Additional options to pass to \link[=jsonlite]{toJSON}.} \item{txt}{JSON character string, url, or file.} + +\item{jswrapper}{Whether to wrap pure JS functions in \code{htmlwidgets::JS}, Default is FALSE.} } \value{ \code{e_inspect} Returns a \code{list} if \code{json} is \code{FALSE} and a