Add possibility to facet plots
Great package! :)
It would be nice to be able to facet plots computed in the database, e.g. via facet_wrap() or on a grouped table. Or is there a straightforward approach already that I am missing?
You're right, at this time db_compute_count() handles one grouping only, although it's able to handle multiple calculations on that group, but that's not what you're asking for.
I'll look into it, thanks for the suggestion!
I second this request :)
I achieved this by simply allowing the user to pass in a grouped dataframe to the db_compute_boxplot function, rather than requiring the grouping variable. I'll also return the uncollected and grouped dataframe so it's
db_compute_boxplot <- function(data, var, coef = 1.5) {
var <- enquo(var)
res <- data
if("tbl_spark" %in% class(data)) {
res <- summarise(
res,
lower = percentile_approx(!!var, 0.25),
middle = percentile_approx(!!var, 0.5),
upper = percentile_approx(!!var, 0.75),
max_raw = max(!!var, na.rm = TRUE),
min_raw = min(!!var, na.rm = TRUE)
)
} else {
res <- summarise(
res,
lower = quantile(!!var, 0.25),
middle = quantile(!!var, 0.5),
upper = quantile(!!var, 0.75),
max_raw = max(!!var, na.rm = TRUE),
min_raw = min(!!var, na.rm = TRUE)
)
}
res <- mutate(res, iqr = (upper - lower) * coef)
res <- mutate(
res,
min_iqr = lower - iqr,
max_iqr = upper + iqr
)
res <- mutate(
res,
ymax = ifelse(max_raw > max_iqr, max_iqr, max_raw),
ymin = ifelse(min_raw < min_iqr, min_iqr, min_raw)
)
return (res)
}