Przeglądaj źródła

Add tidyr:: and dplyr:: where needed

Adds a few additional dplyr functions to the package imports, in particular: slice, data_frame, and row_number.
pull/18/merge
Garrick Aden-Buie 7 lat temu
rodzic
commit
e600e4f31d
5 zmienionych plików z 26 dodań i 22 usunięć
  1. +3
    -0
      NAMESPACE
  2. +4
    -4
      R/move_together.R
  3. +4
    -3
      R/process_data_helpers.R
  4. +14
    -14
      R/tidyr_helpers.R
  5. +1
    -1
      R/zzzz-package.R

+ 3
- 0
NAMESPACE Wyświetl plik

@@ -17,6 +17,7 @@ importFrom(dplyr,anti_join)
importFrom(dplyr,arrange)
importFrom(dplyr,bind_cols)
importFrom(dplyr,bind_rows)
importFrom(dplyr,data_frame)
importFrom(dplyr,filter)
importFrom(dplyr,full_join)
importFrom(dplyr,group_by)
@@ -25,6 +26,8 @@ importFrom(dplyr,left_join)
importFrom(dplyr,mutate)
importFrom(dplyr,pull)
importFrom(dplyr,right_join)
importFrom(dplyr,row_number)
importFrom(dplyr,select)
importFrom(dplyr,semi_join)
importFrom(dplyr,slice)
importFrom(magrittr,"%>%")

+ 4
- 4
R/move_together.R Wyświetl plik

@@ -15,12 +15,12 @@ move_together <- function(lhs, rhs, type) {
all <- bind_rows(lhs, rhs)

# separate column and row-filter (ids)
x_cols <- distinct(lhs, .col)
y_cols <- distinct(rhs, .col)
x_cols <- dplyr::distinct(lhs, .col)
y_cols <- dplyr::distinct(rhs, .col)

# separate header columns from ids and treat them as columns
x_ids <- distinct(lhs, .id, .id_long)
y_ids <- distinct(rhs, .id, .id_long)
x_ids <- dplyr::distinct(lhs, .id, .id_long)
y_ids <- dplyr::distinct(rhs, .id, .id_long)

x_headers <- filter(x_ids, grepl("^\\.header", .id_long))
y_headers <- filter(y_ids, grepl("^\\.header", .id_long))

+ 4
- 3
R/process_data_helpers.R Wyświetl plik

@@ -24,11 +24,11 @@ process_join <- function(x, y, by, fill = TRUE, ...) {
}

x <- x %>%
unite(one_of(by), col = ".id", remove = FALSE) %>%
tidyr::unite(dplyr::one_of(by), col = ".id", remove = FALSE) %>%
mutate(.id_long = add_duplicate_number(.id))

y <- y %>%
unite(one_of(by), col = ".id", remove = FALSE) %>%
tidyr::unite(dplyr::one_of(by), col = ".id", remove = FALSE) %>%
mutate(.id_long = add_duplicate_number(.id))

ids <- dplyr::union(x %>% dplyr::select(.id, .id_long),
@@ -67,7 +67,8 @@ process_data_join <- function(x, ids, by, width = 1, side = NA, fill = TRUE, ...

x <- x %>%
mutate(.r = row_number()) %>%
gather_(key = ".col", value = ".val", names(x)[grepl("^[^.]", names(x))]) %>%
# TODO re-evaluate gather_ here
tidyr::gather_(key = ".col", value = ".val", names(x)[grepl("^[^.]", names(x))]) %>%
mutate(.x = x_keys[.col],
.y = -.r) %>%
bind_rows(data_frame(.id = ".header",

+ 14
- 14
R/tidyr_helpers.R Wyświetl plik

@@ -86,10 +86,10 @@ process_wide <- function(x, ids, key, color_id = "lightgray", ...) {
key_values <- key_values[!key_values %in% ids]

id_values <- x %>% select(one_of(ids))
id_values <- id_values %>% gather(key = ".key_map", value = ".id_map")
id_values <- id_values %>% tidyr::gather(key = ".key_map", value = ".id_map")

x <- x %>% mutate(.r = row_number()) %>%
unite(one_of(ids), col = ".id_map", remove = F)
tidyr::unite(dplyr::one_of(ids), col = ".id_map", remove = F)

x <- x %>%
gather(key = ".col", value = ".val", names(x)[grepl("^[^\\.]", names(x))]) %>%
@@ -104,13 +104,13 @@ process_wide <- function(x, ids, key, color_id = "lightgray", ...) {
tmp <- x %>% filter(.key_map %in% ids)
x <- bind_rows(
left_join(tmp %>% select(-.key_map),
tmp %>% select(.id_map) %>% crossing(.key_map = key_values),
tmp %>% select(.id_map) %>% tidyr::crossing(.key_map = key_values),
by = ".id_map"),
x %>% filter(!.key_map %in% ids)
)

# add header:
crosser <- crossing(.id_map = as.character(id_values$.id_map),
crosser <- tidyr::crossing(.id_map = as.character(id_values$.id_map),
.key_map = key_values)
key_header <- data_frame(
.key_map = key_values,
@@ -132,13 +132,13 @@ process_wide <- function(x, ids, key, color_id = "lightgray", ...) {
.x = 1:length(ids),
.y = 0,
.header = TRUE),
crossing(.id_map = ids, .key_map = key_values),
tidyr::crossing(.id_map = ids, .key_map = key_values),
by = ".id_map"
)

x <- bind_rows(id_header, key_header, x)

x <- x %>% unite(.key_map, .id_map, .val, col = ".id", remove = F)
x <- x %>% tidyr::unite(.key_map, .id_map, .val, col = ".id", remove = F)

x %>%
add_color_tidyr(key_values = key_values) %>%
@@ -172,8 +172,8 @@ process_long <- function(x, ids, key, value, ...) {
xn <- names(x)

x <- x %>% mutate(.r = row_number()) %>%
unite(ids, col = ".id_map", remove = F) %>%
unite(key, col = ".key_map", remove = F)
tidyr::unite(ids, col = ".id_map", remove = F) %>%
tidyr::unite(key, col = ".key_map", remove = F)

key_values <- x %>% pull(key) %>% unique()

@@ -184,7 +184,7 @@ process_long <- function(x, ids, key, value, ...) {
names(x_dict) <- xn

x <- x %>%
gather(key = ".col", value = ".val", names(x)[grepl("^[^\\.]", names(x))]) %>%
tidyr::gather(key = ".col", value = ".val", names(x)[grepl("^[^\\.]", names(x))]) %>%
mutate(
.x = x_dict[.col],
.y = -rep(1:nr, nc),
@@ -195,9 +195,9 @@ process_long <- function(x, ids, key, value, ...) {

# add headers:

id_headers <- crossing(.id_map = ids, # x$.id_map %>% unique()
.key_map = key_values,
) %>%
id_headers <- tidyr::crossing(.id_map = ids, # x$.id_map %>% unique()
.key_map = key_values,
) %>%
mutate(
.r = 0,
.col = "id",
@@ -209,7 +209,7 @@ process_long <- function(x, ids, key, value, ...) {
)

x <- x %>%
add_row(
dplyr::add_row(
.before = T,
.id_map = c(rep("key", length(key)), rep("value", length(value))),
.key_map = c(rep("key", length(key)), rep("value", length(value))),
@@ -225,7 +225,7 @@ process_long <- function(x, ids, key, value, ...) {
x <- bind_rows(id_headers, x)

x <- x %>%
unite(.key_map, .id_map, .val, col = ".id", remove = F)
tidyr::unite(.key_map, .id_map, .val, col = ".id", remove = F)

x %>% add_color_tidyr(key_values = key_values) %>%
mutate(.alpha = ifelse(.header == TRUE, 1, 0.6))

+ 1
- 1
R/zzzz-package.R Wyświetl plik

@@ -1,4 +1,4 @@
#' @importFrom dplyr left_join right_join full_join inner_join semi_join anti_join
#' @importFrom dplyr mutate select filter arrange bind_rows bind_cols group_by pull
#' @importFrom dplyr mutate select filter arrange bind_rows bind_cols group_by pull slice data_frame row_number
#' @keywords internal
"_PACKAGE"

Ładowanie…
Anuluj
Zapisz