Skip to content

Commit f2a2b27

Browse files
committed
0.3.2 updates
1 parent 977c616 commit f2a2b27

File tree

11 files changed

+188
-157
lines changed

11 files changed

+188
-157
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: echarty
22
Title: Minimal R/Shiny Interface to JavaScript Library 'ECharts'
3-
Date: 2021-09-15
4-
Version: 0.3.1.3
3+
Date: 2021-10-17
4+
Version: 0.3.2
55
Authors@R: c(
66
person("Larry", "Helgason", email = "larry@helgasoft.com", role = c("aut", "cre", "cph")),
77
person("John", "Coene", email = "jcoenep@gmail.com", role = c("aut", "cph"))

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# history of echarty package development
22

3+
## v. 0.3.1.4
4+
5+
- _ec.init_: _js_ parameter has now 3 levels of execution
6+
- _ec.clmn_: fix order of parameters, add message for missing data
7+
- _eshiny_ demo: button and code added to modify series
8+
39
## v. 0.3.1.3
410

511
- _ec.init_: fix '3D' presets for groups and 'world' presets

R/echarty.R

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
#' Custom widget arguments include: \cr \itemize{
2626
#' \item elementId - Id of the widget, default is NULL(auto-generated)
2727
#' \item ask - the \emph{plugjs} parameter when \emph{load} is present, FALSE by default
28-
#' \item js - single string or a two-strings vector with JavaScript expressions to evaluate. First is evaluated before and second after chart initialization. Chart object 'opts' is exposed for the second script.
28+
#' \item js - single string or a vector with JavaScript expressions to evaluate.\cr
29+
#' First expression is evaluated before chart initialization. \cr
30+
#' Second is evaluated with an exposed object \emph{opts}. \cr
31+
#' Third is evaluated with an exposed object \emph{chart} after \emph{opts} have been set.
2932
#' \item renderer - 'canvas'(default) or 'svg'
3033
#' \item locale - 'EN'(default) or 'ZH' or other, see \href{https://echarts.apache.org/en/api.html#echarts.init}{here}
3134
#' \item useDirtyRect - enable dirty rectangle rendering or not, FALSE by default, see \href{https://echarts.apache.org/en/api.html#echarts.init}{here}
@@ -122,7 +125,7 @@ ec.init <- function( df=NULL, preset=TRUE, group1='scatter', load=NULL,
122125
mapping = list(),
123126
events = list(),
124127
buttons = list(),
125-
eval = js,
128+
jcode = js,
126129
opts = opts,
127130
settings = list(
128131
crosstalk_key = key,
@@ -249,8 +252,8 @@ ec.init <- function( df=NULL, preset=TRUE, group1='scatter', load=NULL,
249252
if (preset) {
250253
wt$x$opts$xAxis <- NULL
251254
wt$x$opts$yAxis <- NULL
252-
wt$x$opts$series <- list(list(type='map', map='world', roam=TRUE))
253-
#wt$x$opts$geo = list(map='world', roam=TRUE) # duplicate of series
255+
#wt$x$opts$series <- list(list(type='map', map='world', roam=TRUE))
256+
wt$x$opts$geo = list(map='world', roam=TRUE) # may duplicate series
254257
# if (!is.null(df)) # cancelled: dont know if first 2 cols are lng,lat
255258
# wt$x$opts$geo$center= c(mean(unlist(df[,1])), mean(unlist(df[,2])))
256259
}
@@ -455,8 +458,8 @@ ec.data <- function(df, format='dataset', header=TRUE) {
455458
#' Helper function to address data column(s) by index or name
456459
#'
457460
#' @param col A column index(number), column name(string) or a \code{\link[base]{sprintf}} format string.
458-
#' @param scale A decimal/integer number to multiply the column value by. Only when \emph{col} is a single index.
459461
#' @param ... Comma separated list of column indexes or names, when \emph{col} is \emph{sprintf}. This allows formatting of multiple columns, as for a tooltip.
462+
#' @param scale A multiplier (decimal/integer) for column values. Only when \emph{col} is a single column identifier, ignored otherwise.
460463
#'
461464
#' @details Column indexes are counted in R and start at 1.\cr
462465
#' \emph{col} as sprintf supports only two placeholders - %d for column indexes and %s for column names.\cr
@@ -476,38 +479,38 @@ ec.data <- function(df, format='dataset', header=TRUE) {
476479
#' p
477480
#'
478481
#' @export
479-
ec.clmn <- function(col=NULL, scale=1, ...) {
482+
ec.clmn <- function(col=NULL, ..., scale=1) {
480483
if (is.null(col)) stop('col is required', call.=FALSE)
481484
if (is.null(scale)) scale=1
485+
scl <- if (scale==1) 'return c;' else paste0('return (parseFloat(c)*',scale,');')
482486
args <- list(...)
483487
if (is.na(suppressWarnings(as.numeric(col)))) { # col is string
484-
if (length(args)==0)
485-
ret <- paste0('return x.data.',col,';')
486-
else { # col a sprintf
488+
if (length(args)==0) # col is solitary name
489+
ret <- paste0('let c=(!x.data) ? `no data` : x.data.',col,'; ',scl)
490+
else { # col a sprintf
487491
spf <- paste("var sprintf = (str, argv) => !argv.length ? str :",
488492
"sprintf(str = str.replace('@', argv.shift()), argv); ")
489493
tmp <- suppressWarnings(as.numeric(args) -1)
490494
if (all(is.na(tmp))) { # multiple non-numeric strings
491495
tmp <- sapply(args, function(s) toString(paste0('x.data.', s)) )
492496
tmp <- paste(tmp, collapse=',')
493-
ret <- paste0(sub('@','%s',spf),'let ss=[',tmp,']; ',
494-
'let c = sprintf(`',col,'`, ss); return c;')
497+
ret <- paste0(sub('@','%s',spf),' if (!x.data) return `no data`; else { let ss=[',tmp,']; ',
498+
'let c = sprintf(`',col,'`, ss); return c; }')
495499
}
496500
else { # multiple numeric, they could be in x, x.data, x.value
497501
tmp <- paste(tmp, collapse=',')
498502
ret <- paste0(sub('@','%d',spf),
499-
"let ss=[",tmp,"]; ",
500-
"ss=ss.map(e => x.value!=null ? x.value[e] : x.data!=null ? x.data[e] : x[e]);",
501-
"let c = sprintf(`",col,"`, ss); return c; ")
503+
"let ss=[",tmp,"]; ",
504+
"ss=ss.map(e => x.value!=null ? x.value[e] : x.data!=null ? x.data[e] : x[e]);",
505+
"let c = sprintf(`",col,"`, ss); return c; ")
502506
}
503507
}
504508
}
505-
else { # col is solitary numeric
506-
if (length(args) > 0) # { cat(length(args));
509+
else { # col is solitary numeric
510+
if (length(args) > 0)
507511
warning('col is numeric, others are ignored', call.=FALSE)
508512
col <- as.numeric(col) - 1 # from R to JavaScript counting
509-
scl <- if (scale==1) 'return c;' else paste0('return (parseFloat(c)*',scale,');')
510-
ret <- paste0('let c = (x.data) ? x.data[',col,'] : x[',col,']; ',scl)
513+
ret <- paste0('let c = x.value!=null ? x.value[',col,'] : x.data!=null ? x.data[',col,'] : x[',col,']; ',scl)
511514
}
512515
htmlwidgets::JS(paste0('function(x) {', ret, '}'))
513516
}
@@ -849,7 +852,7 @@ ecs.proxy <- function(id) {
849852
#' @param cmd Name of command, default is \emph{p_merge}\cr
850853
#' The proxy commands are:\cr
851854
#' \emph{p_update} - add new series and axes\cr
852-
#' \emph{p_merge} - add serie features like marks\cr
855+
#' \emph{p_merge} - modify or add series features like style,marks,etc.\cr
853856
#' \emph{p_replace} - replace entire chart \cr
854857
#' \emph{p_del_serie} - delete a serie by index or name\cr
855858
#' \emph{p_del_marks} - delete marks of a serie\cr

R/examples.R

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@
9595
#' dplyr::mutate(size= exp(mag)/20) # add accented size
9696
#' p <- tmp %>% ec.init(load='leaflet')
9797
#' p$x$opts$series[[1]]$name = 'quakes'
98-
#' p$x$opts$series[[1]]$symbolSize = ec.clmn(6) # size column
98+
#' p$x$opts$series[[1]]$symbolSize = ec.clmn(6, scale=2) # size column
99+
#' p$x$opts$tooltip = list(formatter=ec.clmn('magnitude %d',4))
99100
#' p$x$opts$legend = list(zz='')
100101
#' p
101102
#'
@@ -119,8 +120,9 @@
119120
#' tmp <- data.frame(airport1 = unique(head(flights,10)$airport1),
120121
#' color = c("#387e78","#eeb422","#d9534f",'magenta'))
121122
#' tmp <- head(flights,10) %>% inner_join(tmp) # add color by airport
122-
#' p <- tmp %>% ec.init(load='world')
123-
#' p$x$opts$geo$center= c(mean(flights$start_lon), mean(flights$start_lat))
123+
#' p <- ec.init(load='world')
124+
#' p$x$opts$geo$center <- c(mean(flights$start_lon), mean(flights$start_lat))
125+
#' p$x$opts$geo$zoom <- 7
124126
#' p$x$opts$series <- list(list(
125127
#' type='lines', coordinateSystem='geo',
126128
#' data = lapply(ec.data(tmp, 'names'), function(x)
@@ -136,18 +138,17 @@
136138
#'
137139
#' #------ registerMap JSON
138140
#' json <- jsonlite::read_json("https://echarts.apache.org/examples/data/asset/geo/USA.json")
139-
#' dusa <- USArrests %>% dplyr::mutate(states = row.names(.))
141+
#' dusa <- USArrests %>% mutate(states = row.names(.))
140142
#' p <- ec.init(preset=FALSE)
141-
#' p$x$registerMap <- list(list(mapName = 'USA', geoJSON = json))
143+
#' p$x$registerMap <- list(list(mapName= 'USA', geoJSON= json))
142144
#' # registerMap supports also maps in SVG format, see website gallery
143-
#' p$x$opts <- list(
144-
#' visualMap = list(type='continuous', calculable=TRUE,
145-
#' min=min(dusa$UrbanPop), max=max(dusa$UrbanPop))
146-
#' ,series = list( list(type='map', map='USA', name='UrbanPop', roam=TRUE,
147-
#' data = lapply(ec.data(dusa,'names'), function(x) list(name=x$states, value=x$UrbanPop))
148-
#' ))
149-
#' )
150-
#' p
145+
#' p$x$opts$visualMap <- list(type= 'continuous', calculable= TRUE,
146+
#' min= min(dusa$UrbanPop), max= max(dusa$UrbanPop))
147+
#' p$x$opts$series <- list(list(type= 'map', map= 'USA',
148+
#' roam= TRUE, zoom= 3, left= -100, top= -30,
149+
#' data= lapply(ec.data(dusa,'names'), function(x) list(name=x$states, value=x$UrbanPop))
150+
#' ))
151+
#' p
151152
#'
152153
#'
153154
#' #------ Plugin 3D
@@ -165,16 +166,18 @@
165166
#' if (interactive()) {
166167
#' df <- data.frame(Species = unique(iris$Species),
167168
#' color = c("#387e78","#eeb422","#d9534f"))
168-
#' df <- iris %>% dplyr::inner_join(df) # add 6th column 'color' for Species
169-
#' p <- df %>% ec.init(load = '3D')
169+
#' df <- iris %>% dplyr::inner_join(df) %>% # add 6th column 'color' for Species
170+
#' mutate(size= log(Petal.Width*10)) # add 7th accented size
171+
#' p <- df %>% ec.init(load= '3D')
170172
#' p$x$opts$xAxis3D <- list(name='Petal.Length')
171173
#' p$x$opts$yAxis3D <- list(name='Sepal.Width')
172174
#' p$x$opts$zAxis3D <- list(name='Sepal.Length')
173-
#' p$x$opts$series <- list(list(
174-
#' type='scatter3D', symbolSize=7,
175+
#' p$x$opts$series <- list(list( name='Iris in 3D',
176+
#' type='scatter3D', symbolSize=ec.clmn(7, scale=10),
175177
#' encode=list(x='Petal.Length', y='Sepal.Width', z='Sepal.Length'),
176-
#' itemStyle=list(color = ec.clmn(6) ) # index of column 'color'
178+
#' itemStyle=list(color = ec.clmn(6) ) # index of column 'color'
177179
#' ))
180+
#' p$x$opts$legend <- list(ii='')
178181
#' p
179182
#' }
180183
#'

README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,9 @@ examples**](https://github.com/helgasoft/echarty/blob/main/R/examples.R)
6464
included. Type
6565
**?ec.examples** in the RStudio Console, then copy/paste any code from Help to
6666
see the result.
67-
<!--
68-
Do not hesitate to ask questions in
69-
[Discussions](https://github.com/helgasoft/echarty/discussions) or
70-
report problems in
71-
[Issues](https://github.com/helgasoft/echarty/issues).
72-
-->
7367

7468
Now you can start building [**beautiful
75-
charts**](https://echarts.apache.org/examples/en/index.html) (and
69+
ECharts**](https://echarts.apache.org/examples/en/index.html) (and
7670
[**more**](https://www.makeapie.com)) with R and Shiny!
7771

7872
 <br />

0 commit comments

Comments
 (0)