Skip to content

Commit

Permalink
plugjs updates
Browse files Browse the repository at this point in the history
  • Loading branch information
helgasoft committed Jun 18, 2021
1 parent 8e6d2df commit 6b7b7ec
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 58 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: echarty
Title: Minimal R/Shiny Interface to JavaScript Library ECharts
Date: 2021-06-03
Version: 0.2.0
Title: Minimal R/Shiny Interface to JavaScript Library 'ECharts'
Date: 2021-06-18
Version: 0.2.1
Authors@R: c(
person("Larry", "Helgason", email = "[email protected]", role = c("aut", "cre", "cph")),
person("John", "Coene", email = "[email protected]", role = c("aut", "cph"))
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# log history of echarty package development

## v. 0.2.1

- upgrade to ECharts v.5.1.2
- ec.plugjs: new parameter 'ask' to allow/suppress prompts
- ec.plugjs: parameter 'source' now accepts 'file://' format

## v. 0.2.0

- removed dependencies for Shiny and crosstalk
Expand Down
66 changes: 37 additions & 29 deletions R/echarty.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@
#' If the grouping is on multiple columns, only the first one is used.
#' @param preset Disable(FALSE) or enable (TRUE, default) presets xAxis,yAxis,serie for 2D, or grid3D,xAxis3D,yAxis3D,zAxis3D for 3D.
#' @param load Name(s) of plugin(s) to load. Could be a character vector or comma-delimited string. default NULL.\cr
#' [ec.plugjs] will be called internally for each entry, popup prompts controlled by parameter \emph{ask}. \cr
#' Built-in plugins: \cr \itemize{
#' \item leaflet - Leaflet maps with customizable tiles, see \href{https://github.com/gnijuohz/echarts-leaflet#readme}{source}\cr
#' \item custom - renderers for [ecr.band] and [ecr.ebars] \cr
#' } Plugins with one-time installation (popup prompt): \cr \itemize{
#' } Plugins with one-time installation: \cr \itemize{
#' \item 3D - 3D charts and WebGL acceleration, see \href{https://github.com/ecomfe/echarts-gl}{source} and \href{https://echarts.apache.org/en/option-gl.html#series}{docs} \cr
#' \item world - world map with country boundaries, see \href{https://github.com/apache/echarts/tree/master/test/data/map/js}{source} \cr
#' \item liquid - liquid fill, see \href{https://github.com/ecomfe/echarts-liquidfill}{source} \cr
#' \item gmodular - graph modularity, see \href{https://github.com/ecomfe/echarts-graph-modularity}{source} \cr
#' \item wordcloud - cloud of words, see \href{https://github.com/ecomfe/echarts-wordcloud}{source} \cr
#' } Install you own plugins with [ec.plugjs].
#' } or install you own third-party plugins.
#' @param width,height A valid CSS unit (like \code{'100\%'},
#' \code{'500px'}, \code{'auto'}) or a number, which will be coerced to a
#' string and have \code{'px'} appended.
Expand Down Expand Up @@ -191,6 +192,7 @@ ec.init <- function( df = NULL, group1 = 'scatter', preset = TRUE, load = NULL,
}

# Plugins implemented as dynamic load on-demand
ask <- if (is.null(opts$ask)) TRUE else opts$ask
if ('3D' %in% load) {
if (preset) {
wt$x$opts$xAxis <- NULL # replace 2D presets with 3D
Expand All @@ -201,33 +203,35 @@ ec.init <- function( df = NULL, group1 = 'scatter', preset = TRUE, load = NULL,
wt$x$opts$yAxis3D <- list(list())
wt$x$opts$zAxis3D <- list(list())
}
wt <- ec.plugjs(wt, 'https://cdn.jsdelivr.net/npm/[email protected]/dist/echarts-gl.min.js')
wt <- ec.plugjs(wt, 'https://cdn.jsdelivr.net/npm/[email protected]/dist/echarts-gl.min.js', ask)
}
if ('world' %in% load)
wt <- ec.plugjs(wt, 'https://cdn.jsdelivr.net/npm/[email protected]/map/js/world.js')
wt <- ec.plugjs(wt, 'https://cdn.jsdelivr.net/npm/[email protected]/map/js/world.js', ask)

if ('liquid' %in% load)
wt <- ec.plugjs(wt, 'https://cdn.jsdelivr.net/npm/[email protected]/dist/echarts-liquidfill.min.js')
wt <- ec.plugjs(wt, 'https://cdn.jsdelivr.net/npm/[email protected]/dist/echarts-liquidfill.min.js', ask)

if ('gmodular' %in% load)
wt <- ec.plugjs(wt, 'https://cdn.jsdelivr.net/npm/[email protected]/dist/echarts-graph-modularity.min.js')
wt <- ec.plugjs(wt, 'https://cdn.jsdelivr.net/npm/[email protected]/dist/echarts-graph-modularity.min.js', ask)

if ('wordcloud' %in% load)
wt <- ec.plugjs(wt, 'https://cdn.jsdelivr.net/npm/[email protected]/dist/echarts-wordcloud.min.js')
wt <- ec.plugjs(wt, 'https://cdn.jsdelivr.net/npm/[email protected]/dist/echarts-wordcloud.min.js', ask)

# load unknown installed plugins
# load unknown plugins
unk <- load[! load %in% c('leaflet','custom','3D','world','liquid','gmodular','wordcloud')]
for(p in unk) wt <- ec.plugjs(wt, p)

if (length(unk)>0) {
for(p in unk) wt <- ec.plugjs(wt, p, ask)
}
return(wt)
}


#' Install Javascript plugin from URL source
#'
#' @param wt A widget to add dependency to, see \code{\link[htmlwidgets]{createWidget}}
#' @param source URL of the uninstalled Javascript plugin, \cr
#' or name of an installed plugin file, suffix .js included. Default is NULL.
#' @param source URL or file:// of an uninstalled Javascript plugin, \cr
#' or name of an installed plugin file with suffix '.js'. Default is NULL.
#' @param ask Whether to ask the user to download source if missing. Default is FALSE
#' @return A widget with JS dependency added if successful, otherwise input wt
#'
#' @details When \emph{source} is URL, the plugin file is installed with a popup prompt to the user.\cr
Expand All @@ -250,33 +254,35 @@ ec.init <- function( df = NULL, group1 = 'scatter', preset = TRUE, load = NULL,
#' @importFrom utils askYesNo download.file
#'
#' @export
ec.plugjs <- function(wt=NULL, source=NULL) {
ec.plugjs <- function(wt=NULL, source=NULL, ask=FALSE) {
if (missing(wt))
stop('ec.plugjs expecting widget', call. = FALSE)
if (is.null(source)) return(wt)
if (!startsWith(source, 'http') && !startsWith(source, 'file://'))
stop('ec.plugjs expecting source as URL or file://', call. = FALSE)
fname <- basename(source)
# if (!endsWith(fname, '.js'))
# stop('ec.plugjs expecting .js suffix', call. = FALSE)
path <- system.file('js', package = 'echarty')
ffull <- paste0(path,'/',fname)
if (!file.exists(ffull)) {
if (!startsWith(source, 'http'))
stop('ec.plugjs expecting URL source', call. = FALSE)
prompt <- paste0('One-time installation of plugin\n',fname,'\n Would you like to proceed ?')
ans <- FALSE
if (interactive())
ans <- askYesNo(prompt)
if (is.na(ans)) ans <- FALSE # was cancelled
if (ask) {
prompt <- paste0('One-time installation of plugin\n',fname,'\n Would you like to proceed ?')
ans <- FALSE
if (interactive())
ans <- askYesNo(prompt)
if (is.na(ans)) ans <- FALSE # was cancelled
} else
ans <- TRUE
if (ans) {
try(withCallingHandlers(
download.file(source, ffull), # method = "libcurl"),
warning = function(w) {
cat('ec.plugjs Error:', sub(".+HTTP status was ", "", w))
ans <- FALSE
}),
silent = TRUE)
error = function(w) { ans <- FALSE },
warning = function(w) { ans <- FALSE }
#cat('ec.plugjs Error:', sub(".+HTTP status was ", "", w, source))
)) #,silent=TRUE)
}
if (!ans) return(wt)
if (!ans) return(wt) # error
}
dep <- htmltools::htmlDependency(
name = fname, version = '1.0.0', src = c(file = path),
Expand Down Expand Up @@ -404,9 +410,11 @@ ec.timegrp <- function(wt, df=NULL, scol=NULL, xcol=NULL, type='line', ...) {
#' @return A container \code{\link[htmltools]{div}} in rmarkdown, otherwise \code{\link[htmltools]{browsable}}
#' @examples
#'
#' p1 <- cars %>% ec.init()
#' p2 <- cars %>% ec.init() %>% ec.theme('dark')
#' ec.layout(list(p1,p2), cols=2 )
#' if (interactive()) {
#' p1 <- cars %>% ec.init()
#' p2 <- cars %>% ec.init() %>% ec.theme('dark')
#' ec.layout(list(p1,p2), cols=2 )
#' }
#'
#' @export
ec.layout <- function (plots, rows = NULL, cols = NULL, width = "xs",
Expand Down
17 changes: 9 additions & 8 deletions R/examples.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@
#'
#' #------ Liquidfill plugin
#' if (interactive()) {
#' p <- ec.init(load=c('liquid'), preset=FALSE)
#' p$x$opts$series[[1]] <- list(
#' type='liquidFill', data=c(0.6, 0.5, 0.4, 0.3), # amplitude=0,
#' waveAnimation=FALSE, animationDuration=0, animationDurationUpdate=0
#' )
#' p
#' p <- ec.init(load=c('liquid'), preset=FALSE)
#' p$x$opts$series[[1]] <- list(
#' type='liquidFill', data=c(0.6, 0.5, 0.4, 0.3), # amplitude=0,
#' waveAnimation=FALSE, animationDuration=0, animationDurationUpdate=0
#' )
#' p
#' }
#'
#'
Expand Down Expand Up @@ -429,8 +429,9 @@
#' q4 <- main; q4$x$opts$series[[1]]$encode <- list(y='qsec', x='mpg');
#' q4$x$connect <- 'group1'
#' # q4$x$disconnect <- 'group1' # ok too
#' ec.layout(list(q1,q2,q3,q4), cols=2, title='group connect')
#'
#' if (interactive()) {
#' ec.layout(list(q1,q2,q3,q4), cols=2, title='group connect')
#' }
#'
#' #------------- Shiny interactive charts demo ---------------
#' if (interactive()) {
Expand Down
4 changes: 2 additions & 2 deletions inst/js/echarts.min.js

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions man/ec.examples.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions man/ec.init.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions man/ec.layout.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions man/ec.plugjs.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6b7b7ec

Please sign in to comment.