Skip to content

Commit 8731596

Browse files
committed
improvements to 1.6.0
1 parent 70473c3 commit 8731596

File tree

9 files changed

+69
-73
lines changed

9 files changed

+69
-73
lines changed

R/echarty.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ NULL
4747
#' * load - name(s) of plugin(s) to load. A character vector or comma-delimited string. default NULL.
4848
#' * ask - prompt user before downloading plugins when _load_ is present, FALSE by default
4949
#' * js - single string or a vector with JavaScript expressions to evaluate.\cr
50-
#' First expression is evaluated before chart initialization. \cr
51-
#' Second is evaluated with exposed object _opts_. \cr
52-
#' Third is evaluated with exposed _chart_ object after _opts_ have been set.
50+
#' single: exposed _chart_ object (most common)\cr
51+
#' vector:\cr
52+
#' \verb{ }First expression is evaluated before chart initialization. \cr
53+
#' \verb{ }Second is evaluated with exposed object _opts_. \cr
54+
#' \verb{ }Third is evaluated with exposed _chart_ object after _opts_ set.
5355
#' * renderer - 'canvas'(default) or 'svg'
5456
#' * locale - 'EN'(default) or 'ZH'. Use predefined or custom \href{https://gist.github.com/helgasoft/0618c6537c45bfd9e86d3f9e1da497b8}{like so}.
5557
#' * useDirtyRect - enable dirty rectangle rendering or not, FALSE by default, see \href{https://echarts.apache.org/en/api.html#echarts.init}{here}

R/examples.R

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -429,18 +429,16 @@
429429
#'
430430
#' #------ Sankey and graph plots
431431
#' sankey <- data.frame(
432-
#' node = c("a","b", "c", "d", "e"),
432+
#' name = c("a","b", "c", "d", "e"),
433433
#' source = c("a", "b", "c", "d", "c"),
434434
#' target = c("b", "c", "d", "e", "e"),
435435
#' value = c(5, 6, 2, 8, 13)
436436
#' )
437437
#' data <- ec.data(sankey, 'names')
438-
#'
439438
#' ec.init(preset= FALSE,
440-
#' series= list(list(
441-
#' type= 'sankey',
442-
#' data= lapply(data, function(x) list(name= x$node)),
443-
#' edges= data ))
439+
#' series= list(list( type= 'sankey',
440+
#' data= data,
441+
#' edges= data ))
444442
#' )
445443
#'
446444
#'
@@ -465,20 +463,17 @@
465463
#'
466464
#' #------ group connect
467465
#' main <- mtcars |> ec.init(height= 200, legend= list(show=FALSE),
468-
#' series.param= list(name= "this legend is shared"))
466+
#' tooltip= list(axisPointer= list(axis='x')),
467+
#' series.param= list(name= "this legend is shared"))
469468
#' main$x$group <- 'group1' # same group name for all charts
470-
#'
471-
#' q1 <- main |> ec.upd({ series[[1]]$encode <- list(y='hp', x='mpg')
472-
#' legend <- list(show=TRUE) # show first legend to share
469+
#' main$x$connect <- 'group1'
470+
#' q1 <- main |> ec.upd({ series[[1]]$encode <- list(y='hp'); yAxis$name <- 'hp'
471+
#' legend <- list(show=TRUE) # show first legend to share
473472
#' })
474-
#' q2 <- main |> ec.upd({ series[[1]]$encode <- list(y='wt', x='mpg') })
475-
#' q3 <- main |> ec.upd({ series[[1]]$encode <- list(y='drat', x='mpg') })
476-
#' q4 <- main |> ec.upd({ series[[1]]$encode <- list(y='qsec', x='mpg')})
477-
#' q4$x$connect <- 'group1'
478-
#' # q4$x$disconnect <- 'group1'
479-
#' if (interactive()) { # browsable
480-
#' ec.util(cmd='layout', list(q1,q2,q3,q4), cols=2, title='group connect')
481-
#' }
473+
#' q2 <- main |> ec.upd({ series[[1]]$encode <- list(y='wt'); yAxis$name <- 'wt' })
474+
#' #if (interactive()) { # browsable
475+
#' ec.util(cmd='layout', list(q1,q2), cols=2, title='group connect')
476+
#' #}
482477
#'
483478
#'
484479
#' #------ Events in Shiny

R/util.R

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,8 +1080,9 @@ ec.theme <- function (wt, name, code= NULL)
10801080
#' \verb{ }'opts' - the htmlwidget _options_ as JSON (default)\cr
10811081
#' \verb{ }'full' - the _entire_ htmlwidget as JSON\cr
10821082
#' \verb{ }'data' - info about chart's embedded data (char vector)
1083-
#' @param ... Additional attributes to pass to \link[jsonlite]{toJSON}
1084-
#' @return A JSON string, except when \code{target} is 'data' then
1083+
#' @param ... Additional attributes to pass to \link[jsonlite]{toJSON}\cr
1084+
#' 'file' - optional file name to save to when target='full'\cr
1085+
#' @return A JSON string, except when \code{target} is 'data' - then
10851086
#' a character vector.
10861087
#'
10871088
#' @details Must be invoked or chained as last command.\cr
@@ -1099,8 +1100,16 @@ ec.theme <- function (wt, name, code= NULL)
10991100
ec.inspect <- function(wt, target='opts', ...) {
11001101

11011102
stopifnot("ec.inspect: target only 'opts', 'data' or 'full'"= target %in% c('opts','data','full'))
1102-
if (target=='full')
1103-
return(jsonlite::serializeJSON(wt))
1103+
if (target=='full') {
1104+
jjwt <- jsonlite::serializeJSON(wt)
1105+
opts <- list(...)
1106+
if ('file' %in% names(opts)) {
1107+
fn <- opts$file
1108+
con <- file(fn,'wb'); write(jjwt, con); close(con)
1109+
return(paste('saved in',fn))
1110+
} else
1111+
return(jjwt)
1112+
}
11041113
opts <- wt$x$opts
11051114

11061115
if (target=='data') {
@@ -1154,15 +1163,16 @@ ec.inspect <- function(wt, target='opts', ...) {
11541163
#' @return An _echarty_ widget.
11551164
#'
11561165
#' @details _txt_ could be either a list of options (x$opts) to be set by \href{https://echarts.apache.org/en/api.html#echartsInstance.setOption}{setOption},\cr
1157-
#' OR an entire _htmlwidget_ generated thru \code{ec.inspect(target='full')}.\cr
1166+
#' OR an entire _htmlwidget_ generated thru [ec.inspect] when _target='full'_.\cr
11581167
#' The latter imports all JavaScript functions defined by the user.
11591168
#'
11601169
#' @examples
11611170
#' txt <- '{
11621171
#' "xAxis": { "data": ["Mon", "Tue", "Wed"]}, "yAxis": { },
11631172
#' "series": { "type": "line", "data": [150, 230, 224] } }'
11641173
#' ec.fromJson(txt)
1165-
#'
1174+
#'
1175+
#' # ec.fromJson('https://helgasoft.github.io/echarty/test/pfull.json')
11661176
#' @export
11671177
ec.fromJson <- function(txt, ...) {
11681178
recur <- \(opts) {
@@ -1185,17 +1195,6 @@ ec.fromJson <- function(txt, ...) {
11851195

11861196
if (inherits(txt, c('url','file')))
11871197
return(jsonlite::unserializeJSON(txt))
1188-
if (inherits(txt, 'XXjson')) {
1189-
tmp <- jsonlite::parse_json(txt, FALSE)
1190-
if (!is.null(tmp$attributes$package)) {
1191-
badge <- tmp$attributes$package$value[[1]]
1192-
if (badge=='echarty') {
1193-
obj <- jsonlite::unserializeJSON(txt)
1194-
return(obj)
1195-
}
1196-
stop(paste('ec.fromJson: unknown input',badge))
1197-
}
1198-
}
11991198
if (inherits(txt, 'character')) {
12001199
if (any(startsWith(txt, c('http://','https://'))))
12011200
return(jsonlite::unserializeJSON(url(txt)))

inst/htmlwidgets/echarty.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ HTMLWidgets.widget({
4040
let tmp = null;
4141
if (Array.isArray(x.jcode)) {
4242
tmp = x.jcode[0]; // #1 run before init
43+
try {
44+
eval(tmp);
45+
} catch(err) { console.log('eva1: ' + err.message) }
4346
eva2 = x.jcode[1];
4447
eva3 = x.jcode[2];
4548
} else
46-
tmp = x.jcode;
47-
try {
48-
eval(tmp);
49-
} catch(err) { console.log('eva1: ' + err.message) }
49+
eva3 = x.jcode;
5050
}
5151
}
5252

man/ec.examples.Rd

Lines changed: 13 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/ec.fromJson.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/ec.init.Rd

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/ec.inspect.Rd

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-ec.util.R

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -340,17 +340,16 @@ test_that("ec.data treePC", {
340340

341341
test_that("ec.data treeTK", {
342342
# see example https://helgasoft.github.io/echarty/uc3.html
343-
df <- as.data.frame(Titanic) |>
344-
group_by(Survived,Age,Sex,Class) |>
345-
summarise(value= sum(Freq), .groups= 'drop') |>
346-
rowwise() |>
347-
mutate(pathString= paste('Survive', Survived, Age, Sex, Class, sep='/')) |>
348-
select(pathString, value)
343+
df <- as.data.frame(Titanic) |> rename(value= Freq) |>
344+
mutate(pathString= paste('Survive', Survived, Age, Sex, Class, sep='/'),
345+
itemStyle= case_when(Survived=='Yes' ~ "color='green'", TRUE ~ "color='pink'")) |>
346+
select(pathString, value, itemStyle)
347+
349348
p <- ec.init(preset= FALSE,
350349
title= list(text= 'Titanic: Survival by Class'),
351350
tooltip= list(formatter= ec.clmn('%@ (%@%)', 'value','pct')),
352351
series= list(list(
353-
type= 'tree', symbolSize= ec.clmn(scale=0.08),
352+
type= 'tree', symbolSize= htmlwidgets::JS("x => {return Math.log(x)*10}"),
354353
data= ec.data(df, format='treeTK')
355354
))
356355
)
@@ -403,5 +402,7 @@ test_that("ec.inspect and ec.fromJson", {
403402

404403
v <- ec.fromJson(tmp)
405404
expect_equal(v$x$opts$xAxis$type, 'category')
405+
p <- echarty::ec.fromJson('https://helgasoft.github.io/echarty/test/pfull.json')
406+
expect_true(inherits(p, 'echarty'))
406407
})
407408

0 commit comments

Comments
 (0)