diff --git a/R/categorize.R b/R/categorize.R index 0df7edd4c..d440069d8 100644 --- a/R/categorize.R +++ b/R/categorize.R @@ -192,11 +192,11 @@ categorize.numeric <- function(x, breaks <- split } else { breaks <- switch(split, - "median" = stats::median(x), - "mean" = mean(x), - "length" = n_groups, - "quantile" = stats::quantile(x, probs = seq_len(n_groups) / n_groups), - "range" = .equal_range(x, range, n_groups, lowest), + median = stats::median(x), + mean = mean(x), + length = n_groups, + quantile = stats::quantile(x, probs = seq_len(n_groups) / n_groups), + range = .equal_range(x, range, n_groups, lowest), NULL ) } diff --git a/R/data_codebook.R b/R/data_codebook.R index 077dfd6d5..7608f08d5 100644 --- a/R/data_codebook.R +++ b/R/data_codebook.R @@ -456,8 +456,14 @@ print_md.data_codebook <- function(x, ...) { # need to remove this one x$Prop <- NULL align <- c( - "ID" = "l", "Name" = "l", "Label" = "l", "Type" = "l", "Missings" = "r", - "Values" = "r", "Value Labels" = "l", "N" = "r" + ID = "l", + Name = "l", + Label = "l", + Type = "l", + Missings = "r", + Values = "r", + `Value Labels` = "l", + N = "r" ) align <- align[colnames(x)] paste0(unname(align), collapse = "") diff --git a/R/data_extract.R b/R/data_extract.R index df38dcbde..b5613309c 100644 --- a/R/data_extract.R +++ b/R/data_extract.R @@ -114,10 +114,10 @@ data_extract.data.frame <- function(data, # chose which matched variables to extract select <- switch(extract, - "first" = select[1L], - "last" = select[length(select)], - "odd" = select[seq(1L, length(select), 2L)], - "even" = select[seq(2L, length(select), 2L)], + first = select[1L], + last = select[length(select)], + odd = select[seq(1L, length(select), 2L)], + even = select[seq(2L, length(select), 2L)], select ) diff --git a/R/data_match.R b/R/data_match.R index ff567d952..7c19c7911 100644 --- a/R/data_match.R +++ b/R/data_match.R @@ -109,17 +109,17 @@ data_match <- function(x, to, match = "and", return_indices = FALSE, drop_na = T # evaluate match <- match.arg(tolower(match), c("and", "&", "&&", "or", "|", "||", "!", "not")) match <- switch(match, - "&" = , - "&&" = , - "and" = "and", - "!" = , - "not" = "not", + `&` = , + `&&` = , + and = "and", + `!` = , + not = "not", "or" ) # validation check shared_columns <- intersect(colnames(x), colnames(to)) - if (is.null(shared_columns) || length(shared_columns) == 0) { + if (is.null(shared_columns) || length(shared_columns) == 0L) { insight::format_error( "None of the columns from the data frame with matching conditions were found in `x`." ) @@ -277,7 +277,6 @@ data_filter.data.frame <- function(x, ...) { #' @export data_filter.grouped_df <- function(x, ...) { - # works only for dplyr >= 0.8.0 grps <- attr(x, "groups", exact = TRUE) grps <- grps[[".rows"]] diff --git a/R/data_merge.R b/R/data_merge.R index c8a90cb0e..58be12f8a 100644 --- a/R/data_merge.R +++ b/R/data_merge.R @@ -274,13 +274,13 @@ data_merge.data.frame <- function(x, y, join = "left", by = NULL, id = NULL, ver all_columns <- union(colnames(x), colnames(y)) out <- switch(join, - "full" = merge(x, y, all = TRUE, sort = FALSE, by = by), - "left" = merge(x, y, all.x = TRUE, sort = FALSE, by = by), - "right" = merge(x, y, all.y = TRUE, sort = FALSE, by = by), - "inner" = merge(x, y, sort = FALSE, by = by), - "semi" = x[x[[by]] %in% y[[by]], , drop = FALSE], - "anti" = x[!x[[by]] %in% y[[by]], , drop = FALSE], - "bind" = .bind_data_frames(x, y) + full = merge(x, y, all = TRUE, sort = FALSE, by = by), + left = merge(x, y, all.x = TRUE, sort = FALSE, by = by), + right = merge(x, y, all.y = TRUE, sort = FALSE, by = by), + inner = merge(x, y, sort = FALSE, by = by), + semi = x[x[[by]] %in% y[[by]], , drop = FALSE], + anti = x[!x[[by]] %in% y[[by]], , drop = FALSE], + bind = .bind_data_frames(x, y) ) diff --git a/R/data_read.R b/R/data_read.R index 2c3061570..a2f3b46d9 100644 --- a/R/data_read.R +++ b/R/data_read.R @@ -97,14 +97,14 @@ data_read <- function(path, # read data out <- switch(file_type, - "txt" = , - "csv" = .read_text(path, encoding, verbose, ...), - "xls" = , - "xlsx" = .read_excel(path, encoding, verbose, ...), - "sav" = , - "por" = .read_spss(path, encoding, convert_factors, verbose, ...), - "dta" = .read_stata(path, encoding, convert_factors, verbose, ...), - "sas7bdat" = .read_sas(path, path_catalog, encoding, convert_factors, verbose, ...), + txt = , + csv = .read_text(path, encoding, verbose, ...), + xls = , + xlsx = .read_excel(path, encoding, verbose, ...), + sav = , + por = .read_spss(path, encoding, convert_factors, verbose, ...), + dta = .read_stata(path, encoding, convert_factors, verbose, ...), + sas7bdat = .read_sas(path, path_catalog, encoding, convert_factors, verbose, ...), .read_unknown(path, convert_factors, verbose, ...) ) diff --git a/R/data_separate.R b/R/data_separate.R index eb6d571f1..c5e83183d 100644 --- a/R/data_separate.R +++ b/R/data_separate.R @@ -229,9 +229,9 @@ data_separate <- function(data, l <- l[!vapply(l, function(i) all(is.na(i)), TRUE)] # define number of new columns, based on user-choice n_cols <- switch(guess_columns, - "min" = min(l, na.rm = TRUE), - "max" = max(l, na.rm = TRUE), - "mode" = distribution_mode(l), + min = min(l, na.rm = TRUE), + max = max(l, na.rm = TRUE), + mode = distribution_mode(l), ) # tell user if (verbose && insight::n_unique(l) != 1 && !is.numeric(separator)) { @@ -374,10 +374,10 @@ data_separate <- function(data, "`", sep_column, "`", " returned more columns than expected after splitting. ", switch(extra, - "drop_left" = "Left-most columns have been dropped.", - "drop_right" = "Right-most columns have been dropped.", - "merge_left" = "Left-most columns have been merged together.", - "merge_right" = "Right-most columns have been merged together." + drop_left = "Left-most columns have been dropped.", + drop_right = "Right-most columns have been dropped.", + merge_left = "Left-most columns have been merged together.", + merge_right = "Right-most columns have been merged together." ) )) } @@ -386,10 +386,10 @@ data_separate <- function(data, "`", sep_column, "`", "returned fewer columns than expected after splitting. ", switch(fill, - "left" = "Left-most columns were filled with `NA`.", - "right" = "Right-most columns were filled with `NA`.", - "value_left" = "Left-most columns were filled with first value.", - "value_right" = "Right-most columns were filled with last value." + left = "Left-most columns were filled with `NA`.", + right = "Right-most columns were filled with `NA`.", + value_left = "Left-most columns were filled with first value.", + value_right = "Right-most columns were filled with last value." ) )) } diff --git a/R/data_tabulate.R b/R/data_tabulate.R index 71b694eb8..3b16932a3 100644 --- a/R/data_tabulate.R +++ b/R/data_tabulate.R @@ -489,14 +489,14 @@ print_md.dw_data_tabulates <- function(x, big_mark = NULL, ...) { } switch(vt, - "ord" = "ordinal", - "fct" = "categorical", - "dbl" = "numeric", - "int" = "integer", - "chr" = "character", - "lbl" = "labelled", - "cpl" = "complex", - "lgl" = "logical", + ord = "ordinal", + fct = "categorical", + dbl = "numeric", + int = "integer", + chr = "character", + lbl = "labelled", + cpl = "complex", + lgl = "logical", vt ) } diff --git a/R/data_write.R b/R/data_write.R index 324b71168..7e4e543d6 100644 --- a/R/data_write.R +++ b/R/data_write.R @@ -19,13 +19,13 @@ data_write <- function(data, # check file type, so we know the target dta format file_type <- .file_ext(path) type <- switch(file_type, - "txt" = , - "csv" = "csv", - "sav" = , - "por" = "spss", - "zsav" = "zspss", - "dta" = "stata", - "xpt" = "sas", + txt = , + csv = "csv", + sav = , + por = "spss", + zsav = "zspss", + dta = "stata", + xpt = "sas", "unknown" ) diff --git a/R/describe_distribution.R b/R/describe_distribution.R index e01243388..00a8a2475 100644 --- a/R/describe_distribution.R +++ b/R/describe_distribution.R @@ -230,7 +230,7 @@ describe_distribution.numeric <- function(x, out$n <- length(x) out$n_Missing <- n_missing - out$`.temp` <- NULL + out$.temp <- NULL class(out) <- unique(c("parameters_distribution", "see_parameters_distribution", class(out))) attr(out, "data") <- x diff --git a/R/select_nse.R b/R/select_nse.R index 8d312f7b1..118d40b15 100644 --- a/R/select_nse.R +++ b/R/select_nse.R @@ -96,11 +96,11 @@ type <- typeof(x) out <- switch(type, - "integer" = x, - "double" = as.integer(x), - "character" = .select_char(data, x, ignore_case, regex = regex, verbose), - "symbol" = .select_symbol(data, x, ignore_case, regex = regex, verbose), - "language" = .eval_call(data, x, ignore_case, regex = regex, verbose), + integer = x, + double = as.integer(x), + character = .select_char(data, x, ignore_case, regex = regex, verbose), + symbol = .select_symbol(data, x, ignore_case, regex = regex, verbose), + language = .eval_call(data, x, ignore_case, regex = regex, verbose), insight::format_error(paste0( "Expressions of type <", typeof(x), "> cannot be evaluated for use when subsetting." @@ -254,13 +254,13 @@ `[` = .select_square_bracket(x, data, ignore_case, regex, verbose), `$` = .select_dollar(x, data, ignore_case, regex, verbose), `~` = .select_tilde(x, data, ignore_case, regex, verbose), - "list" = .select_list(x, data, ignore_case, regex, verbose), - "names" = .select_names(x, data, ignore_case, regex, verbose), - "starts_with" = , - "ends_with" = , - "matches" = , - "contains" = , - "regex" = .select_helper(x, data, ignore_case, regex, verbose), + list = .select_list(x, data, ignore_case, regex, verbose), + names = .select_names(x, data, ignore_case, regex, verbose), + starts_with = , + ends_with = , + matches = , + contains = , + regex = .select_helper(x, data, ignore_case, regex, verbose), .select_context(x, data, ignore_case, regex, verbose) ) } @@ -369,10 +369,10 @@ helper <- insight::safe_deparse(lst_expr[[1]]) rgx <- switch(helper, - "starts_with" = paste0("^(", collapsed_patterns, ")"), - "ends_with" = paste0("(", collapsed_patterns, ")$"), - "contains" = paste0("(", collapsed_patterns, ")"), - "regex" = collapsed_patterns, + starts_with = paste0("^(", collapsed_patterns, ")"), + ends_with = paste0("(", collapsed_patterns, ")$"), + contains = paste0("(", collapsed_patterns, ")"), + regex = collapsed_patterns, insight::format_error("There is no select helper called '", helper, "'.") ) grep(rgx, colnames(data), ignore.case = ignore_case) diff --git a/R/skewness_kurtosis.R b/R/skewness_kurtosis.R index 13ad4e422..e0da83c54 100644 --- a/R/skewness_kurtosis.R +++ b/R/skewness_kurtosis.R @@ -474,16 +474,16 @@ summary.parameters_kurtosis <- function(object, test = FALSE, ...) { } switch(type, - "1" = , - "I" = , - "classic" = "1", - "2" = , - "II" = , - "SPSS" = , - "SAS" = "2", - "3" = , - "III" = , - "Minitab" = "3" + `1` = , + I = , + classic = "1", + `2` = , + II = , + SPSS = , + SAS = "2", + `3` = , + III = , + Minitab = "3" ) } diff --git a/tests/testthat/test-assign_labels.R b/tests/testthat/test-assign_labels.R index fd74705ef..300ae643f 100644 --- a/tests/testthat/test-assign_labels.R +++ b/tests/testthat/test-assign_labels.R @@ -16,7 +16,7 @@ test_that("assign_labels, named values", { out <- assign_labels( x, variable = "Labelled factor", - values = c(`a` = "low", `b` = "mid", `c` = "high") + values = c(a = "low", b = "mid", c = "high") ) expect_identical(attributes(out)$label, "Labelled factor") expect_identical(attributes(out)$labels, c(low = "a", mid = "b", high = "c")) diff --git a/tests/testthat/test-data_codebook.R b/tests/testthat/test-data_codebook.R index daaf1f77b..26a67ccf6 100644 --- a/tests/testthat/test-data_codebook.R +++ b/tests/testthat/test-data_codebook.R @@ -159,9 +159,11 @@ test_that("data_codebook, tagged NA", { 1:4, haven::tagged_na("a", "c", "z") ), labels = c( - "Agreement" = 1, "Disagreement" = 4, - "First" = haven::tagged_na("c"), "Refused" = haven::tagged_na("a"), - "Not home" = haven::tagged_na("z") + Agreement = 1, + Disagreement = 4, + First = haven::tagged_na("c"), + Refused = haven::tagged_na("a"), + `Not home` = haven::tagged_na("z") ) ) expect_snapshot(data_codebook(data.frame(x))) @@ -174,9 +176,11 @@ test_that("data_codebook, tagged NA", { 1:4, haven::tagged_na("a", "c") ), labels = c( - "Agreement" = 1, "Disagreement" = 4, - "First" = haven::tagged_na("c"), "Refused" = haven::tagged_na("a"), - "Not home" = haven::tagged_na("z") + Agreement = 1, + Disagreement = 4, + First = haven::tagged_na("c"), + Refused = haven::tagged_na("a"), + `Not home` = haven::tagged_na("z") ) ) expect_snapshot(data_codebook(data.frame(x))) @@ -187,11 +191,11 @@ test_that("data_codebook, negative label values #334", { skip_if_not_installed("haven") x1 <- haven::labelled( x = 1:4, - labels = c("Agreement" = 1, "Disagreement" = 4, "Missing" = -9) + labels = c(Agreement = 1, Disagreement = 4, Missing = -9) ) x2 <- haven::labelled( x = c(1:3, -9), - labels = c("Agreement" = 1, "Disagreement" = 4, "Missing" = -9) + labels = c(Agreement = 1, Disagreement = 4, Missing = -9) ) expect_snapshot(data_codebook(data.frame(x1, x2))) }) diff --git a/tests/testthat/test-data_merge.R b/tests/testthat/test-data_merge.R index b98f4b8c8..ef48355bb 100644 --- a/tests/testthat/test-data_merge.R +++ b/tests/testthat/test-data_merge.R @@ -250,9 +250,9 @@ test_that("join data frames in a list", { # join empty data frames ----------------------- -x <- data.frame("x" = character(), stringsAsFactors = FALSE) -y <- data.frame("x" = character(), stringsAsFactors = FALSE) -z <- data.frame("y" = character(), stringsAsFactors = FALSE) +x <- data.frame(x = character(), stringsAsFactors = FALSE) +y <- data.frame(x = character(), stringsAsFactors = FALSE) +z <- data.frame(y = character(), stringsAsFactors = FALSE) test_that("join empty data frames", { expect_identical(dim(data_merge(x, y, join = "left")), c(0L, 1L)) diff --git a/tests/testthat/test-data_rescale.R b/tests/testthat/test-data_rescale.R index 2c24f0687..3539e2fc0 100644 --- a/tests/testthat/test-data_rescale.R +++ b/tests/testthat/test-data_rescale.R @@ -37,8 +37,8 @@ test_that("rescale works as expected", { expect_snapshot( head(rescale(iris, to = list( - "Sepal.Length" = c(0, 1), - "Petal.Length" = c(-1, 0) + Sepal.Length = c(0, 1), + Petal.Length = c(-1, 0) ))) ) }) diff --git a/tests/testthat/test-data_reverse.R b/tests/testthat/test-data_reverse.R index b1fe1f73e..794591904 100644 --- a/tests/testthat/test-data_reverse.R +++ b/tests/testthat/test-data_reverse.R @@ -335,16 +335,20 @@ test_that("reverse_scale warns if single value to reverse", { test_that("reverse_scale select helpers", { data(iris) out <- rescale(iris, to = list( - "Sepal.Length" = c(0, 1), - "Petal.Length" = c(-1, 0) + Sepal.Length = c(0, 1), + Petal.Length = c(-1, 0) ), select = ends_with("length")) expect_identical(out$Sepal.Length, iris$Sepal.Length, tolerance = 1e-3) - out <- rescale(iris, to = list( - "Sepal.Length" = c(0, 1), - "Petal.Length" = c(-1, 0) - ), select = ends_with("length"), ignore_case = TRUE) + out <- rescale(iris, + to = list( + Sepal.Length = c(0, 1), + Petal.Length = c(-1, 0) + ), + select = ends_with("length"), + ignore_case = TRUE + ) expect_identical(head(out$Sepal.Length), c(0.22222, 0.16667, 0.11111, 0.08333, 0.19444, 0.30556), tolerance = 1e-3) }) diff --git a/tests/testthat/test-data_to_factor.R b/tests/testthat/test-data_to_factor.R index ee8a46c3e..0fd31b26a 100644 --- a/tests/testthat/test-data_to_factor.R +++ b/tests/testthat/test-data_to_factor.R @@ -22,7 +22,7 @@ test_that("to_factor", { # numeric, partially labelled test_that("to_factor", { x <- c(10, 11, 12) - attr(x, "labels") <- c("ten" = 10, "twelve" = 12) + attr(x, "labels") <- c(ten = 10, twelve = 12) expect_message( expect_identical( to_factor(x),