Skip to content

Commit

Permalink
tree
Browse files Browse the repository at this point in the history
  • Loading branch information
jpcoene committed Mar 14, 2018
1 parent 56e24da commit 52e32f0
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 12 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Expand Up @@ -11,5 +11,6 @@ Imports:
htmlwidgets,
magrittr,
rlang,
dplyr
dplyr,
data.tree
RoxygenNote: 6.0.1
2 changes: 2 additions & 0 deletions NAMESPACE
Expand Up @@ -12,8 +12,10 @@ export(e_graph_nodes)
export(e_heatmap)
export(e_line)
export(e_parallel)
export(e_pie)
export(e_sankey)
export(e_scatter)
export(e_treemap)
export(e_visual_map)
export(echarts4Output)
export(renderEcharts4)
Expand Down
97 changes: 93 additions & 4 deletions R/add.R
Expand Up @@ -9,16 +9,16 @@
#'
#' @examples
#' USArrests %>%
#' dplyr::mutate(
#' state = row.names(.)
#' ) %>%
#' e_charts(state) %>%
#' e_charts(Assault) %>%
#' e_bar(Murder, name = "Euwww") %>%
#' e_line(Rape)
#'
#' @rdname barline
#' @export
e_bar <- function(e, serie, name = NULL, ...){

if(missing(e))
stop("must pass e", call. = FALSE)

if(missing(serie))
stop("must pass serie", call. = FALSE)
Expand Down Expand Up @@ -541,4 +541,93 @@ e_parallel <- function(e, ..., name = NULL){
e$x$opts$series <- append(e$x$opts$series, serie)
e$x$opts$parallelAxis <- para
e
}

#' Pie
#'
#' Draw pie and donut charts.
#'
#' @inheritParams e_bar
#' @param label Labels of slices.
#'
#' @examples
#' mtcars %>%
#' head() %>%
#' dplyr::mutate(model = row.names(.)) %>%
#' e_charts() %>%
#' e_pie(carb, model)
#'
#' @export
e_pie <- function(e, serie, label, name = NULL, ...){
if(missing(e))
stop("must pass e", call. = FALSE)

if(missing(serie) || missing(label))
stop("must pass serie and label", call. = FALSE)

e$x$opts$xAxis <- NULL # remove
e$x$opts$yAxis <- NULL # remove

if(is.null(name)) # defaults to column name
name <- deparse(substitute(serie))

# build JSON data
data <- .build_pie(e$x$data, dplyr::enquo(serie), dplyr::enquo(label))

serie <- list(
name = name,
type = "pie",
data = data,
...
)

e$x$opts$legend$data <- append(e$x$opts$legend$data, .graph_cat_legend(e$x$data, dplyr::enquo(label)))

e$x$opts$series <- append(e$x$opts$series, list(serie))
e
}

#' Tree
#'
#' Build a treemap.
#'
#' @inheritParams e_bar
#' @param parent,child Edges.
#'
#' @examples
#' df <- data.frame(
#' parent = c("earth","earth","forest","forest","ocean","ocean","ocean","ocean"),
#' child = c("ocean","forest","tree","sasquatch","fish","seaweed","mantis shrimp","sea monster")
#' )
#'
#' df %>%
#' e_charts() %>%
#' e_treemap(parent, child)
#'
#' @export
e_treemap <- function(e, parent, child, ...){
if(missing(e))
stop("must pass e", call. = FALSE)

if(missing(parent) || missing(child))
stop("must pass parent and child", call. = FALSE)

e$x$opts$xAxis <- NULL # remove
e$x$opts$yAxis <- NULL # remove

# build JSON data
data <- .build_tree(
e$x$data,
dplyr::enquo(parent),
dplyr::enquo(child)
)

serie <- list(
type = "tree",
data = list(data),
...
)

e$x$opts$series <- append(e$x$opts$series, list(serie))
e
}
4 changes: 2 additions & 2 deletions R/echarts4.R
Expand Up @@ -45,11 +45,11 @@ e_charts <- function(data, x = NULL, width = NULL, height = NULL, elementId = NU

# create widget
htmlwidgets::createWidget(
name = 'echarts4',
name = 'echarts4r',
x,
width = width,
height = height,
package = 'echarts4',
package = 'echarts4r',
elementId = elementId
)
}
Expand Down
23 changes: 23 additions & 0 deletions R/utils.R
Expand Up @@ -192,3 +192,26 @@

apply(data, 1, as.list)
}

.build_pie <- function(data, serie, label){
data %>%
dplyr::select(
!!serie,
!!label
) -> data

names(data) <- c("value", "name")

apply(data, 1, as.list)
}

.build_tree <- function(data, parent, child){
data %>%
dplyr::select(
!!parent,
!!child
) -> df

tree <- data.tree::FromDataFrameNetwork(df)
data.tree::ToListExplicit(tree, unname = TRUE)
}
17 changes: 17 additions & 0 deletions echarts4r.Rproj
@@ -0,0 +1,17 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
@@ -1,6 +1,6 @@
HTMLWidgets.widget({

name: 'echarts4',
name: 'echarts4r',

type: 'output',

Expand Down
File renamed without changes.
5 changes: 1 addition & 4 deletions man/barline.Rd

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

30 changes: 30 additions & 0 deletions man/e_pie.Rd

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

29 changes: 29 additions & 0 deletions man/e_treemap.Rd

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

0 comments on commit 52e32f0

Please sign in to comment.