Skip to content

Commit

Permalink
add reorder argument to address #126
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnCoene committed Feb 12, 2020
1 parent af5b1a5 commit 5d7e820
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 25 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Every function that adds a serie to chart (e.g.: `e_bar`) have been turned into
- Added `e_resize` to dynamically resize the chart.
- Added `e_map_register_p` works like a proxy but does not require a chart.
- Fixed a few issues with `e_mark*` family, labels and multiple marks correctly work.
- Initialisation function now take `reorder` argument to specify whether to reorder numeric values.

# echarts4r 0.2.3

Expand Down
15 changes: 8 additions & 7 deletions R/echarts4r.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ echarts_build <- function(e) {
#' @param renderer Renderer, takes \code{canvas} (default) or \code{svg}.
#' @param timeline Set to \code{TRUE} to build a timeline, see timeline section.
#' @param ... Any other argument.
#' @param reorder Set the \code{FALSE} to not reorder numeric x axis values.
#'
#' @section Timeline:
#' The timeline feature currently supports the following chart types.
Expand Down Expand Up @@ -76,7 +77,7 @@ echarts_build <- function(e) {
#' @name init
#' @export
e_charts <- function(data, x, width = NULL, height = NULL, elementId = NULL, dispose = TRUE,
draw = TRUE, renderer = "canvas", timeline = FALSE, ...) {
draw = TRUE, renderer = "canvas", timeline = FALSE, ..., reorder = TRUE) {

xmap <- NULL

Expand Down Expand Up @@ -105,7 +106,7 @@ e_charts <- function(data, x, width = NULL, height = NULL, elementId = NULL, dis
row.names(data) <- NULL

if(!is.null(xmap) && !isTRUE(timeline))
data <- .arrange_data_x(data, xmap)
data <- .arrange_data_x(data, xmap, reorder = reorder)

x$data <- map_grps_(data, timeline)
}
Expand All @@ -125,7 +126,7 @@ e_charts <- function(data, x, width = NULL, height = NULL, elementId = NULL, dis
stop("must pass grouped data when timeline = TRUE", call. = FALSE)

if(!is.null(xmap))
x$data <- .arrange_data_by_group(x$data, xmap)
x$data <- .arrange_data_by_group(x$data, xmap, reorder = reorder)

tl <- list(
baseOption = list(
Expand Down Expand Up @@ -195,7 +196,7 @@ e_charts <- function(data, x, width = NULL, height = NULL, elementId = NULL, dis
#' @rdname init
#' @export
e_charts_ <- function(data, x = NULL, width = NULL, height = NULL, elementId = NULL, dispose = TRUE,
draw = TRUE, renderer = "canvas", timeline = FALSE, ...) {
draw = TRUE, renderer = "canvas", timeline = FALSE, ..., reorder = TRUE) {

xmap <- x

Expand Down Expand Up @@ -356,7 +357,7 @@ renderEcharts4r <- function(expr, env = parent.frame(), quoted = FALSE) {

#' @rdname echarts4r-shiny
#' @export
echarts4rProxy <- function(id, data, x, timeline = FALSE, session = shiny::getDefaultReactiveDomain()){
echarts4rProxy <- function(id, data, x, timeline = FALSE, session = shiny::getDefaultReactiveDomain(), reorder = TRUE){

if(missing(data)){
proxy <- list(id = id, session = session)
Expand Down Expand Up @@ -390,7 +391,7 @@ echarts4rProxy <- function(id, data, x, timeline = FALSE, session = shiny::getDe
row.names(data) <- NULL

if(!is.null(xmap))
data <- .arrange_data_x(data, xmap)
data <- .arrange_data_x(data, xmap, reorder = reorder)

x$data <- map_grps_(data, timeline)
}
Expand All @@ -410,7 +411,7 @@ echarts4rProxy <- function(id, data, x, timeline = FALSE, session = shiny::getDe
stop("must pass grouped data when timeline = TRUE", call. = FALSE)

if(!is.null(xmap))
x$data <- .arrange_data_by_group(x$data, xmap)
x$data <- .arrange_data_by_group(x$data, xmap, reorder = reorder)

tl <- list(
baseOption = list(
Expand Down
14 changes: 8 additions & 6 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ globalVariables(c("x", "e", ".", "acc", "epoch", "loss", "size", "val_acc", "val
if (!is.null(x)) x else y
}

.arrange_data_x <- function(data, x){
.arrange_data_x <- function(data, x, reorder = TRUE){
vect <- data[[x]]

if(inherits(vect, "numeric") || inherits(vect, "integer"))
data <- data %>% dplyr::arrange_(x)
if(reorder)
if(inherits(vect, "numeric") || inherits(vect, "integer"))
data <- data %>% dplyr::arrange_(x)

return(data)
}

.arrange_data_by_group <- function(data, x){
.arrange_data_by_group <- function(data, x, reorder = TRUE){

vect <- data[[1]][[x]]

for(i in 1:length(data)){
if(inherits(vect, "numeric") || inherits(vect, "integer"))
data[[i]] <- data[[i]] %>% dplyr::arrange_(x)
if(reorder)
if(inherits(vect, "numeric") || inherits(vect, "integer"))
data[[i]] <- data[[i]] %>% dplyr::arrange_(x)
}

return(data)
Expand Down
12 changes: 10 additions & 2 deletions docs/news/index.html

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

10 changes: 8 additions & 2 deletions docs/reference/echarts4r-shiny.html

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

13 changes: 10 additions & 3 deletions docs/reference/init.html

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

8 changes: 6 additions & 2 deletions man/echarts4r-shiny.Rd

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

11 changes: 8 additions & 3 deletions man/init.Rd

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

0 comments on commit 5d7e820

Please sign in to comment.