From 1545cb210b3ecbeef2360f1eeed5a20bcc95912c Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 18 Dec 2023 15:42:42 +0100 Subject: [PATCH] Fix lintrs --- R/data_rotate.R | 2 +- R/data_seek.R | 18 +++++++++--------- R/data_separate.R | 40 +++++++++++++++++++--------------------- 3 files changed, 29 insertions(+), 31 deletions(-) diff --git a/R/data_rotate.R b/R/data_rotate.R index f9a3389d3..2bb39a85d 100644 --- a/R/data_rotate.R +++ b/R/data_rotate.R @@ -56,7 +56,7 @@ data_rotate <- function(data, rownames = NULL, colnames = FALSE, verbose = TRUE) # warning after possible removal of columns if (verbose && insight::n_unique(vapply(data, typeof, FUN.VALUE = character(1L))) > 1L) { - insight::format_warning("Your data frame contains mixed types of data. After transposition, all variables will be transformed into characters.") + insight::format_warning("Your data frame contains mixed types of data. After transposition, all variables will be transformed into characters.") # nolint } # rotate data frame by 90 degrees diff --git a/R/data_seek.R b/R/data_seek.R index c0a56ab8f..c09e57489 100644 --- a/R/data_seek.R +++ b/R/data_seek.R @@ -71,7 +71,7 @@ data_seek <- function(data, pattern, seek = c("names", "labels"), fuzzy = FALSE) pos <- unlist(lapply(pattern, function(search_pattern) { # search in variable names? if (any(seek %in% c("names", "columns", "column_names", "all"))) { - pos1 <- which(grepl(search_pattern, colnames(data))) + pos1 <- grep(search_pattern, colnames(data)) # find in near distance? if (fuzzy) { pos1 <- c(pos1, .fuzzy_grep(x = colnames(data), pattern = search_pattern)) @@ -80,15 +80,15 @@ data_seek <- function(data, pattern, seek = c("names", "labels"), fuzzy = FALSE) # search in variable labels? if (any(seek %in% c("labels", "all"))) { - labels <- insight::compact_character(unlist(lapply(data, attr, which = "label", exact = TRUE))) - if (!is.null(labels) && length(labels)) { - found <- grepl(search_pattern, labels) - pos2 <- match(names(labels)[found], colnames(data)) + var_labels <- insight::compact_character(unlist(lapply(data, attr, which = "label", exact = TRUE))) + if (!is.null(var_labels) && length(var_labels)) { + found <- grepl(search_pattern, var_labels) + pos2 <- match(names(var_labels)[found], colnames(data)) # find in near distanc? if (fuzzy) { - found <- .fuzzy_grep(x = labels, pattern = search_pattern) + found <- .fuzzy_grep(x = var_labels, pattern = search_pattern) if (length(found)) { - pos2 <- c(pos2, match(names(labels)[found], colnames(data))) + pos2 <- c(pos2, match(names(var_labels)[found], colnames(data))) } } } @@ -129,7 +129,7 @@ data_seek <- function(data, pattern, seek = c("names", "labels"), fuzzy = FALSE) pos <- unique(pos) # variable labels of matching variables - labels <- vapply( + var_labels <- vapply( colnames(data[pos]), function(i) { l <- attr(data[[i]], "label", exact = TRUE) @@ -145,7 +145,7 @@ data_seek <- function(data, pattern, seek = c("names", "labels"), fuzzy = FALSE) out <- data.frame( index = pos, column = colnames(data)[pos], - labels = labels, + labels = var_labels, stringsAsFactors = FALSE ) # no row names diff --git a/R/data_separate.R b/R/data_separate.R index c5e83183d..d2c3362b0 100644 --- a/R/data_separate.R +++ b/R/data_separate.R @@ -254,7 +254,7 @@ data_separate <- function(data, # catch error if (is.null(separated_columns)) { insight::format_error( - "Something went wrong. Probably the number of provided column names did not match number of newly created columns?" + "Something went wrong. Probably the number of provided column names did not match number of newly created columns?" # nolint ) } @@ -338,29 +338,27 @@ data_separate <- function(data, out <- rep(NA_character_, times = n_cols) } else if (n_values > n_cols) { # we have more values than required - drop extra columns - if (extra == "drop_left") { - out <- i[(n_values - n_cols + 1):n_values] - } else if (extra == "drop_right") { - out <- i[1:n_cols] - } else if (extra == "merge_left") { - out <- paste(i[1:(n_values - n_cols + 1)], collapse = " ") - out <- c(out, i[(n_values - n_cols + 2):n_values]) - } else { - out <- i[1:(n_cols - 1)] - out <- c(out, paste(i[n_cols:n_values], collapse = " ")) - } + out <- switch(extra, + drop_left = i[(n_values - n_cols + 1):n_values], + drop_right = i[1:n_cols], + merge_left = { + tmp <- paste(i[1:(n_values - n_cols + 1)], collapse = " ") + c(tmp, i[(n_values - n_cols + 2):n_values]) + }, + { + tmp <- i[1:(n_cols - 1)] + c(tmp, paste(i[n_cols:n_values], collapse = " ")) + } + ) warn_extra <- TRUE } else if (n_values < n_cols) { # we have fewer values than required - fill columns - if (fill == "left") { - out <- c(rep(NA_character_, times = n_cols - n_values), i) - } else if (fill == "right") { - out <- c(i, rep(NA_character_, times = n_cols - n_values)) - } else if (fill == "value_left") { - out <- c(rep(i[1], times = n_cols - n_values), i) - } else { - out <- c(i, rep(i[length(i)], times = n_cols - n_values)) - } + out <- switch(fill, + left = c(rep(NA_character_, times = n_cols - n_values), i), + right = c(i, rep(NA_character_, times = n_cols - n_values)), + value_left = c(rep(i[1], times = n_cols - n_values), i), + c(i, rep(i[length(i)], times = n_cols - n_values)) + ) warn_fill <- TRUE } else { out <- i