From 914a504d68a3c8d3121c04fee7cca6a3411e4737 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 18 Dec 2023 15:51:03 +0100 Subject: [PATCH] lintr --- R/describe_distribution.R | 18 ++++---- tests/testthat/test-describe_distribution.R | 47 +++++++++++---------- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/R/describe_distribution.R b/R/describe_distribution.R index 00a8a2475..ee5c3a903 100644 --- a/R/describe_distribution.R +++ b/R/describe_distribution.R @@ -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] } } @@ -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 diff --git a/tests/testthat/test-describe_distribution.R b/tests/testthat/test-describe_distribution.R index 5976286ae..a33e2c52a 100644 --- a/tests/testthat/test-describe_distribution.R +++ b/tests/testthat/test-describe_distribution.R @@ -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) }) @@ -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", { @@ -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", { @@ -182,8 +182,8 @@ 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)) }) @@ -191,7 +191,7 @@ 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)) }) @@ -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)) }) @@ -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")) @@ -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" @@ -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 ------------------------------ @@ -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 ) })