Skip to content

Commit

Permalink
lintr
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Dec 18, 2023
1 parent 1545cb2 commit 914a504
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
18 changes: 9 additions & 9 deletions R/describe_distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ describe_distribution.list <- function(x,
# ex: list(mtcars$mpg, mtcars$cyl) -> c("mtcars$mpg", "mtcars$cyl")
nm <- vapply(sys.call()[[2]], insight::safe_deparse, FUN.VALUE = character(1L))[-1]

if (!isTRUE(include_factors)) {
x <- x[num_el]
if (isTRUE(include_factors)) {
x <- x[c(num_el, factor_el)]
if (length(nm) != 0) {
nm <- nm[num_el]
nm <- nm[c(num_el, factor_el)]
}
} else {
x <- x[c(num_el, factor_el)]
x <- x[num_el]
if (length(nm) != 0) {
nm <- nm[c(num_el, factor_el)]
nm <- nm[num_el]
}
}

Expand Down Expand Up @@ -123,12 +123,12 @@ describe_distribution.list <- function(x,
}))


if (!is.null(names(x))) {
empty_names <- which(names(x) == "")
if (is.null(names(x))) {
new_names <- nm
} else {
empty_names <- which(!nzchar(names(x), keepNA = TRUE))
new_names <- names(x)
new_names[empty_names] <- nm[empty_names]
} else {
new_names <- nm
}

out$Variable <- new_names
Expand Down
47 changes: 24 additions & 23 deletions tests/testthat/test-describe_distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test_that("describe_distribution - numeric: works with basic numeric vector", {
skip_if_not_installed("bayestestR")

x <- describe_distribution(mtcars$mpg)
expect_equal(dim(x), c(1, 9))
expect_identical(dim(x), c(1L, 9L))
expect_equal(round(x$Mean), 20)
})

Expand Down Expand Up @@ -140,14 +140,14 @@ test_that("describe_distribution - list: works with include_factors", {
x2 <- describe_distribution(list(mtcars$mpg, factor(mtcars$cyl)),
include_factors = TRUE
)
expect_equal(dim(x2), c(2, 10))
expect_equal(x2$Variable, c("mtcars$mpg", "factor(mtcars$cyl)"))
expect_identical(dim(x2), c(2L, 10L))
expect_identical(x2$Variable, c("mtcars$mpg", "factor(mtcars$cyl)"))

x3 <- describe_distribution(list(mtcars$mpg, foo = factor(mtcars$cyl)),
include_factors = TRUE
)
expect_equal(dim(x3), c(2, 10))
expect_equal(x3$Variable, c("mtcars$mpg", "foo"))
expect_identical(dim(x3), c(2L, 10L))
expect_identical(x3$Variable, c("mtcars$mpg", "foo"))
})

test_that("describe_distribution - list: correctly removes character elements", {
Expand All @@ -167,10 +167,10 @@ test_that("describe_distribution - list: correctly handles variable names", {
named <- describe_distribution(list(foo = mtcars$mpg, foo2 = mtcars$cyl))
mix <- describe_distribution(list(foo = mtcars$mpg, mtcars$cyl))

expect_equal(stored$Variable, c("Var_1", "Var_2"))
expect_equal(unnamed$Variable, c("mtcars$mpg", "mtcars$cyl"))
expect_equal(named$Variable, c("foo", "foo2"))
expect_equal(mix$Variable, c("foo", "mtcars$cyl"))
expect_identical(stored$Variable, c("Var_1", "Var_2"))
expect_identical(unnamed$Variable, c("mtcars$mpg", "mtcars$cyl"))
expect_identical(named$Variable, c("foo", "foo2"))
expect_identical(mix$Variable, c("foo", "mtcars$cyl"))
})

test_that("describe_distribution - list: correctly handles missing values", {
Expand All @@ -182,16 +182,16 @@ test_that("describe_distribution - list: correctly handles missing values", {
test[1] <- NA
test2[1] <- NA
with_missing <- describe_distribution(list(test, test2))
expect_equal(unique(with_missing$n), 31)
expect_equal(unique(with_missing$n_Missing), 1)
expect_identical(unique(with_missing$n), 31)
expect_identical(unique(with_missing$n_Missing), 1)
expect_false(unique(with_missing$Mean == no_missing$Mean))
})

test_that("describe_distribution - list: works with quartiles", {
skip_if_not_installed("bayestestR")

x <- describe_distribution(list(mtcars$mpg, mtcars$cyl), quartiles = TRUE)
expect_equal(dim(x), c(2, 12))
expect_identical(dim(x), c(2L, 12L))
expect_true("Q1" %in% names(x))
expect_true("Q3" %in% names(x))
})
Expand All @@ -200,7 +200,7 @@ test_that("describe_distribution - list: works with range", {
skip_if_not_installed("bayestestR")

x <- describe_distribution(list(mtcars$mpg, mtcars$cyl), range = FALSE)
expect_equal(dim(x), c(2, 8))
expect_identical(dim(x), c(2L, 8L))
expect_false("min" %in% names(x))
expect_false("max" %in% names(x))
})
Expand All @@ -215,7 +215,7 @@ test_that("describe_distribution - select", {
data(iris)
out <- describe_distribution(iris, select = starts_with("Petal"))

expect_equal(out$Variable, c("Petal.Length", "Petal.Width"))
expect_identical(out$Variable, c("Petal.Length", "Petal.Width"))
expect_equal(out$Mean, c(3.758000, 1.199333), tolerance = 1e-3)

expect_null(describe_distribution(iris, select = "Species"))
Expand All @@ -235,12 +235,12 @@ test_that("describe_distribution - grouped df", {
x <- data_group(iris, Species)
out <- describe_distribution(x, select = starts_with("Petal"))

expect_equal(out$.group, c(
expect_identical(out$.group, c(
"Species=setosa", "Species=setosa",
"Species=versicolor", "Species=versicolor",
"Species=virginica", "Species=virginica"
))
expect_equal(out$Variable, c(
expect_identical(out$Variable, c(
"Petal.Length", "Petal.Width",
"Petal.Length", "Petal.Width",
"Petal.Length", "Petal.Width"
Expand All @@ -255,18 +255,18 @@ test_that("distribution_mode works as expected", {
skip_if_not_installed("bayestestR")

# atomic vector
expect_equal(distribution_mode(c(1, 2, 3, 3, 4, 5)), 3)
expect_equal(distribution_mode(c(1, 2, 3, 3, 4, 4, 5)), 3)
expect_equal(distribution_mode(c(1.5, 2.3, 3.7, 3.7, 4.0, 5)), 3.7)
expect_identical(distribution_mode(c(1, 2, 3, 3, 4, 5)), 3)
expect_identical(distribution_mode(c(1, 2, 3, 3, 4, 4, 5)), 3)
expect_identical(distribution_mode(c(1.5, 2.3, 3.7, 3.7, 4.0, 5)), 3.7)

# list
expect_equal(distribution_mode(list(1, 2, 3, 3, 4, 5)), list(3))
expect_identical(distribution_mode(list(1, 2, 3, 3, 4, 5)), list(3))

# scalar
expect_equal(distribution_mode("a"), "a")
expect_identical(distribution_mode("a"), "a")

# empty
expect_null(distribution_mode(c()))
expect_null(distribution_mode(NULL))
})

# select helpers ------------------------------
Expand All @@ -275,6 +275,7 @@ test_that("describe_distribution regex", {

expect_equal(
describe_distribution(mtcars, select = "pg", regex = TRUE),
describe_distribution(mtcars, select = "mpg")
describe_distribution(mtcars, select = "mpg"),
ignore_attr = TRUE
)
})

0 comments on commit 914a504

Please sign in to comment.