|
25 | 25 | #
|
26 | 26 | group2NA <- function(data, groupNames = "group", nested = NULL, ordered = NULL,
|
27 | 27 | retrace.first = inherits(data, "GeomPolygon")) {
|
| 28 | + |
28 | 29 | if (NROW(data) == 0) return(data)
|
29 | 30 |
|
| 31 | + ## make copy and eliminate duplicated column names |
| 32 | + ## If data is already an internal copy and does not need to be protected from in place modifications, then |
| 33 | + ## the copy being created here could be eliminated for all cases except where column names are duplicated -- |
| 34 | + ## wouldn't save much time, but could lower amt. of memory allocation required for plotly calls. |
| 35 | + |
30 | 36 | if(data.table::is.data.table(data)){
|
31 | 37 | data <- data[,unique(names(data)),with=FALSE]
|
32 | 38 | } else {
|
33 | 39 | data <- data[!duplicated(names(data))]
|
34 | 40 | }
|
35 | 41 |
|
36 |
| - # a few workarounds since dplyr clobbers classes that we rely on in ggplotly |
| 42 | + ## store class information from function input |
37 | 43 | retrace <- force(retrace.first)
|
38 | 44 | datClass <- class(data)
|
39 | 45 |
|
40 |
| - # sanitize variable names |
| 46 | + ## sanitize variable names |
41 | 47 | groupNames <- groupNames[groupNames %in% names(data)]
|
42 | 48 | nested <- nested[nested %in% names(data)]
|
43 | 49 | ordered <- ordered[ordered %in% names(data)]
|
44 | 50 |
|
45 |
| - # ignore any already existing groups (not required w/data.table?) |
46 |
| - |
47 |
| - # if group doesn't exist, just arrange before returning |
| 51 | + ## if group doesn't exist, just arrange before returning |
48 | 52 | if (!length(groupNames)) {
|
49 | 53 | if (length(ordered)) {
|
50 |
| - data.table::setDT(data,key = c(nested, ordered)) |
| 54 | + return( |
| 55 | + structure( |
| 56 | + data.table::setDT(data,key = c(nested, ordered)), |
| 57 | + class = datClass) |
| 58 | + ) |
| 59 | + } else { |
| 60 | + return(data) |
51 | 61 | }
|
52 |
| - return(data) |
53 | 62 | }
|
54 | 63 |
|
55 | 64 | allVars <- c(nested, groupNames, ordered)
|
56 | 65 |
|
57 |
| - # TODO: better now |
58 |
| - d <- if (retrace.first) { |
59 |
| - data.table::setDT(data, key = allVars)[ data[, .I[c(seq_along(.I), 1L, .N+1L)], by=allVars]$V1 ] |
| 66 | + ## if retrace.first is TRUE,repeat the first row of each group and add an empty row of NA's after each group. |
| 67 | + ## if retrace.first is FALSE, just add an empty row to each group. |
| 68 | + ## delete final row of NA's, return d with the original class |
| 69 | + |
| 70 | + ## IMPORTANT: does it matter if operating w/data.table setDT() clobbers row names attribute? |
| 71 | + if (retrace.first) { |
| 72 | + return( |
| 73 | + data.table::setDT(data, key = allVars)[ data[, .I[c(seq_along(.I), 1L, .N+1L)], by=allVars]$V1 ][-.N,] %>% |
| 74 | + structure(class = datClass) |
| 75 | + ) |
60 | 76 | } else {
|
61 |
| - data.table::setDT(data, key = allVars)[ data[, .I[c(seq_along(.I), 1L, .N+1L)], by=allVars]$V1 ] |
| 77 | + return( |
| 78 | + structure( |
| 79 | + data.table::setDT(data, key = allVars)[ data[, .I[c(seq_along(.I), 1L, .N+1L)], by=allVars]$V1 ][-.N,], |
| 80 | + class = datClass) |
| 81 | + ) |
62 | 82 | }
|
63 | 83 |
|
64 |
| - # TODO: how to drop the NAs separating the nested values? Does it even matter? |
| 84 | + ## IMPORTANT: does this still need to be done? |
| 85 | + ## TODO: how to drop the NAs separating the nested values? Does it even matter? |
65 | 86 | # d <- dplyr::ungroup(d)
|
66 | 87 | # for (i in nested) {
|
67 | 88 | # d <- dplyr::group_by_(dplyr::ungroup(d), i, add = TRUE)
|
68 | 89 | # }
|
69 | 90 | # d <- dplyr::do(d, .[seq_len(NROW(.)),])
|
70 |
| - |
71 |
| - if (all(is.na(d[.N, ]))) d <- d[-.N,] |
72 |
| - structure(d, class = datClass) |
73 |
| - |
74 | 91 | }
|
75 | 92 |
|
76 | 93 |
|
|
0 commit comments