forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocation.R
89 lines (74 loc) · 2.13 KB
/
location.R
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
77
78
79
80
81
82
83
84
85
86
87
88
89
#' Print the location in memory of a data frame
#'
#' This is useful for understand how and when dplyr makes copies of data
#' frames
#'
#' @param df a data frame
#' @param x,y two data frames to compare
#' @export
#' @examples
#' location(mtcars)
#'
#' mtcars2 <- mutate(mtcars, cyl2 = cyl * 2)
#' location(mtcars2)
#'
#' changes(mtcars, mtcars)
#' changes(mtcars, mtcars2)
location <- function(df) {
assert_that(is.data.frame(df))
structure(list(
df = loc(df),
vars = dfloc(df),
attr = plfloc(attributes(df))
), class = "location")
}
#' @export
print.location <- function(x, ...) {
cat("<", x$df, ">\n", sep = "")
width <- max(nchar(c(names(x$vars), names(x$attr)))) + 1
def_list <- function(x) {
term <- format(paste0(names(x), ":"), width = width)
paste0(" * ", term, " <", format(x), ">")
}
vars <- paste0(def_list(x$vars), collapse = "\n")
cat("Variables:\n", vars, "\n", sep = "")
attr <- paste0(def_list(x$attr), collapse = "\n")
cat("Attributes:\n", attr, "\n", sep = "")
invisible(x)
}
#' @rdname location
#' @export
changes <- function(x, y) {
x <- location(x)
y <- location(y)
if (x$df == y$df) {
cat("<identical>\n")
return(invisible())
}
# match up x vars to y vars
vars <- match_up(x$vars, y$vars)
attr <- match_up(x$attr, y$attr)
width <- max(nchar(rownames(vars)), nchar(rownames(attr)))
if (nrow(vars) > 0) rownames(vars) <- format(rownames(vars), width = width)
if (nrow(attr) > 0) rownames(attr) <- format(rownames(attr), width = width)
if (nrow(vars) > 0) {
cat("Changed variables:\n")
print(vars, quote = FALSE)
}
if (nrow(vars) > 0 && nrow(attr)) cat("\n")
if (nrow(attr) > 0) {
cat("Changed attributes:\n")
print(attr, quote = FALSE)
}
}
match_up <- function(x, y) {
both <- intersect(names(x), names(y))
added <- setdiff(names(x), names(y))
deleted <- setdiff(names(y), names(x))
out <- cbind(
old = c(x[both], x[added], rep("<added>", length(deleted))),
new = c(y[both], rep("<deleted>", length(added)), y[deleted])
)
rownames(out) <- c(both, added, deleted)
out[out[, "old"] != out[, "new"], , drop = FALSE]
}