Skip to content
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ CHANGES

* `data_read()` now also reads zip-files from URLs (#682).

* `data_tabluate()` now returns an attribute "by" with the
`by` variable name when the `by` parameter is used.
Comment thread
etiennebacher marked this conversation as resolved.
Outdated

# datawizard 1.3.1

CHANGES
Expand Down
16 changes: 16 additions & 0 deletions R/data_tabulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ data_tabulate.default <- function(

# we go into another function for crosstables here...
if (!is.null(by)) {
by_name <- tryCatch(
insight::safe_deparse(substitute(by)),
error = function(e) NULL
)
by <- .validate_by(by, x)
return(.crosstable(
x,
Expand All @@ -191,6 +195,7 @@ data_tabulate.default <- function(
remove_na = remove_na,
proportions = proportions,
obj_name = obj_name,
by_name = by_name,
group_variable = group_variable
))
}
Expand Down Expand Up @@ -322,6 +327,10 @@ data_tabulate.data.frame <- function(
verbose = verbose
)

by_name <- tryCatch(
insight::safe_deparse(substitute(by)),
error = function(e) NULL
)
# validate "by"
by <- .validate_by(by, x)
# validate "weights"
Expand All @@ -345,6 +354,7 @@ data_tabulate.data.frame <- function(
class(out) <- c("datawizard_tables", "list")
} else {
class(out) <- c("datawizard_crosstabs", "list")
attr(out, "by") <- gsub('\\"', "", by_name, fixed = TRUE)
Comment thread
elinw marked this conversation as resolved.
Outdated
}
attr(out, "collapse") <- isTRUE(collapse)
attr(out, "is_weighted") <- !is.null(weights)
Expand Down Expand Up @@ -383,6 +393,11 @@ data_tabulate.grouped_df <- function(
verbose = verbose
)

by_name <- tryCatch(
insight::safe_deparse(substitute(by)),
error = function(e) NULL
)

x <- as.data.frame(x)

out <- list()
Expand Down Expand Up @@ -416,6 +431,7 @@ data_tabulate.grouped_df <- function(
class(out) <- c("datawizard_tables", "list")
} else {
class(out) <- c("datawizard_crosstabs", "list")
attr(out, "by") <- gsub('\\"', "", by_name, fixed = TRUE)
Comment thread
elinw marked this conversation as resolved.
Outdated
}
attr(out, "collapse") <- isTRUE(collapse)
attr(out, "is_weighted") <- !is.null(weights)
Expand Down
3 changes: 3 additions & 0 deletions R/data_xtabulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
remove_na = FALSE,
proportions = NULL,
obj_name = NULL,
by_name = NULL,
group_variable = NULL
) {
if (!is.null(proportions)) {
Expand Down Expand Up @@ -89,6 +90,8 @@
attr(out, "weights") <- weights
attr(out, "proportions") <- proportions
attr(out, "varname") <- obj_name
attr(out, "by") <- by_name

attr(out, "grouped_df") <- !is.null(group_variable)
attr(out, "prop_table") <- .prop_table(out)

Expand Down
Loading