Skip to content

Commit

Permalink
50 prevents factor to fact in details() (#51)
Browse files Browse the repository at this point in the history
* rewsolves #50

* changes argument order

* corrects use of factor_n

* syntax typo
  • Loading branch information
jmbarbone committed Oct 26, 2021
1 parent 36eb03e commit 48601c1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# mark (development version)

* `details(factor)` no longer adds `fact` class to `factors` [#50](https://github.com/jmbarbone/mark/issues/50)
* `details()` gains new argument `factor_n` to control threshold for making character vectors into factors
* `detail.data.frame()` now works with single column data.frames [#48](https://github.com/jmbarbone/mark/issues/48)

# mark 0.4.0
Expand All @@ -26,7 +28,7 @@
* `import(, overwrite = TRUE)` now works
* `ls_function()`, `ls_object()`, `ls_dataframe()`, and `ls_all()` have improvements for environmental searching
* `assign_labels.data.frame(.missing = "warning")` correctly removes missing columns
* `remove_na.factor()` no long drops additional classes other than `ordered` and `factor`
* `remove_na.factor()` no long drops additional classes other than `ordered` and `factor`

## Others

Expand Down
29 changes: 17 additions & 12 deletions R/detail.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ detail <- function(x, ...) {
}

#' @rdname detail
#' @param factor_n An `integer` threshold for making factors; will convert any
#' character vectors with `factor_n` or less unique values into a `fact`;
#' setting as `NA` will ignore this
#' @export
detail.default <- function(x, ...) {
detail.default <- function(x, factor_n = 5L, ...) {
stopifnot(!is.list(x))

op <- options(stringsAsFactors = FALSE)
Expand All @@ -32,19 +35,21 @@ detail.default <- function(x, ...) {
nas <- is.na(x)
x2 <- x[!nas]

# If either of these exact, make as factor
has_lls <-
!is.null(attr(x, "levels", exact = TRUE)) ||
!is.null(attr(x, "labels", exact = TRUE))

if (has_lls) {
x <- fact(x)
}

facts <- is.factor(x)
quants <- !is.character(x) && !facts
nc <- nchar(as.character(x))

if (!is.na(factor_n) && !facts) {
# If either of these exist, make as factor
has_lls <-
!is.null(attr(x, "levels", exact = TRUE)) ||
!is.null(attr(x, "labels", exact = TRUE))

if (has_lls) {
x <- fact(x)
}
}

if (!facts & !quants) {
if (length(unique(x)) <= 5) {
x <- fact(x)
Expand Down Expand Up @@ -80,7 +85,7 @@ detail.default <- function(x, ...) {

#' @rdname detail
#' @export
detail.data.frame <- function(x, ...) {
detail.data.frame <- function(x, factor_n = 5L, ...) {
op <- options(stringsAsFactors = FALSE)
on.exit(options(op), add = TRUE)

Expand All @@ -91,7 +96,7 @@ detail.data.frame <- function(x, ...) {
stop("x does not have any non-list columns", call. = FALSE)
}

details <- lapply(x, detail)
details <- lapply(x, detail, factor_n = factor_n)
reps <- vap_int(details, nrow)

cbind(
Expand Down
8 changes: 6 additions & 2 deletions man/detail.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tests/testthat/test-detail.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ test_that("details() works", {
expect_error(detail(data.frame()))
})

test_that("details() keeps factors [50]", {
expect_identical(detail(factor("a"))$class, "factor")
expect_identical(detail(ordered("a"))$class, "ordered; factor")
})

test_that("details() and tibbles", {
skip_if_not_installed("tibble")

Expand Down

0 comments on commit 48601c1

Please sign in to comment.