forked from plotly/plotly.R
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup2NA.R
More file actions
76 lines (70 loc) · 2.69 KB
/
group2NA.R
File metadata and controls
76 lines (70 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Insert missing values to create trace groupings
#
# If a group of traces share the same non-positional characteristics (i.e.,
# color, fill, etc), it is more efficient to draw them as a single trace
# with missing values that separate the groups (instead of multiple traces).
# This is a helper function for inserting missing values into a data set
#
# @param data a data frame.
# @param groupNames name(s) of the grouping variable(s) as a character vector
# @param nested other variables that group should be nested
# (i.e., ordered) within.
# @param ordered a variable to arrange by (within nested & groupNames). This
# is useful primarily for ordering by x
# @param retrace.first should the first row of each group be appended to the
# last row? This is useful for enclosing polygons with lines.
# @examples
#
# group2NA(mtcars, "vs", "cyl")
#
# elong <- tidyr::gather(economics, variable, value, -date)
# plot_ly(group2NA(elong, "variable"), x = ~date, y = ~value)
#
group2NA <- function(data, groupNames = "group", nested = NULL, ordered = NULL,
retrace.first = inherits(data, "GeomPolygon")) {
if (NROW(data) == 0) return(data)
data <- data[!duplicated(names(data))]
# a few workarounds since dplyr clobbers classes that we rely on in ggplotly
retrace <- force(retrace.first)
datClass <- class(data)
# sanitize variable names
groupNames <- groupNames[groupNames %in% names(data)]
nested <- nested[nested %in% names(data)]
ordered <- ordered[ordered %in% names(data)]
# ignore any already existing groups
data <- dplyr::ungroup(data)
# if group doesn't exist, just arrange before returning
if (!length(groupNames)) {
if (length(ordered)) {
data <- dplyr::arrange_(data, c(nested, ordered))
}
return(data)
}
allVars <- c(nested, groupNames, ordered)
for (i in allVars) {
data <- dplyr::group_by_(data, i, add = TRUE)
}
# first, arrange everything
data <- dplyr::do(data, dplyr::arrange_(., allVars))
data <- dplyr::ungroup(data)
for (i in c(nested, groupNames)) {
data <- dplyr::group_by_(data, i, add = TRUE)
}
# TODO: this is slow, can it be done with dplyr::slice()?
d <- if (retrace.first) {
dplyr::do(data, rbind.data.frame(., .[1,], NA))
} else {
dplyr::do(data, rbind.data.frame(., NA))
}
# TODO: how to drop the NAs separating the nested values? Does it even matter?
# d <- dplyr::ungroup(d)
# for (i in nested) {
# d <- dplyr::group_by_(dplyr::ungroup(d), i, add = TRUE)
# }
# d <- dplyr::do(d, .[seq_len(NROW(.)),])
n <- NROW(d)
if (all(is.na(d[n, ]))) d <- d[-n, ]
structure(d, class = datClass)
}
# to appease R CMD check (currently we reference '.' in group2NA)
utils::globalVariables(".")