JohnCoene / echarts4r

🐳 ECharts 5 for R
http://echarts4r.john-coene.com/
Other
595 stars 81 forks source link

feat: logic to split grouped tidytable #584

Closed teofiln closed 11 months ago

teofiln commented 11 months ago

This PR adds minimal code to support stacked bar chart with a grouped_tt class (grouped tidytable).

Currently, echarts4r handles grouped tibbles nicely (class grouped_df). The code below will make a stacked barplot..

x <- c("a", "a", "b", "b")
g <- c("g1", "g2", "g1", "g2")
y <- c(1, 1, 1, 1)

d <- data.frame(x, g, y)
grp_d <- d |> dplyr::group_by(g)

grp_d |>
    e_chart(x) |>
    e_bar(y, stack = "g")

image

However, if one does not user dplyr but tidytable, the resulting grouped data frame is not of class grouped_df, but grouped_tt. The code results with this

x <- c("a", "a", "b", "b")
g <- c("g1", "g2", "g1", "g2")
y <- c(1, 1, 1, 1)

d <- data.frame(x, g, y)
grp_d <- d |> tidytable::group_by(g)

grp_d |>
    e_chart(x) |>
    e_bar(y, stack = "g")

image

With the this PR, I added an else if condition in map_grps_ to use base::split to split the grouped tidytable (instead of dplyr::group_split), such that grouped tidytables also produce stacked barcharts.

I have not tested it thoroughly. I don't know all the features that depend on map_grps_, but the proposed addition should not affect any dplyr-dependent code. Happy to work on this more if needed.

Thanks!

rdatasculptor commented 11 months ago

Nice addition!

JohnCoene commented 11 months ago

Looks good, thank you!