From 3c11b3805ff2c0a4cbef8a207dbba1b7d9621b54 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 12 Sep 2023 14:57:11 +0200 Subject: [PATCH] seek_ function name -> seek arg name --- R/seek_variables.R | 18 +++++++++--------- man/seek_variables.Rd | 6 +++--- tests/testthat/test-seek_data.R | 16 ++++++++-------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/R/seek_variables.R b/R/seek_variables.R index 86abffd66..734db9888 100644 --- a/R/seek_variables.R +++ b/R/seek_variables.R @@ -17,7 +17,7 @@ #' May also be a character vector of length > 1. `pattern` is searched for in #' column names, variable label and value labels attributes, or factor levels of #' variables in `data`. -#' @param search Character vector, indicating where `pattern` is sought. Use one +#' @param seek Character vector, indicating where `pattern` is sought. Use one #' of more of the following options: #' #' - `"name"`: searches in column names. @@ -45,30 +45,30 @@ #' # variable names and labels only, so no match #' seek_variables(efc, "female") #' # when we seek in all sources, we find the variable "e16sex" -#' seek_variables(efc, "female", search = "all") +#' seek_variables(efc, "female", seek = "all") #' #' # typo, no match #' seek_variables(iris, "Lenght") #' # typo, fuzzy match #' seek_variables(iris, "Lenght", fuzzy = TRUE) #' @export -seek_variables <- function(data, pattern, search = c("names", "labels"), fuzzy = FALSE) { +seek_variables <- function(data, pattern, seek = c("names", "labels"), fuzzy = FALSE) { # check valid args if (!is.data.frame(data)) { insight::format_error("`data` must be a data frame.") } # check valid args - search <- intersect(search, c("names", "labels", "values", "levels", "column_names", "columns", "all")) - if (is.null(search) || !length(search)) { - insight::format_error("`search` must be one of \"names\", \"labels\", \"values\", a combination of these options, or \"all\".") + seek <- intersect(seek, c("names", "labels", "values", "levels", "column_names", "columns", "all")) + if (is.null(seek) || !length(seek)) { + insight::format_error("`seek` must be one of \"names\", \"labels\", \"values\", a combination of these options, or \"all\".") } pos1 <- pos2 <- pos3 <- NULL pos <- unlist(lapply(pattern, function(search_pattern) { # search in variable names? - if (any(search %in% c("names", "columns", "column_names", "all"))) { + if (any(seek %in% c("names", "columns", "column_names", "all"))) { pos1 <- which(grepl(search_pattern, colnames(data))) # find in near distance? if (fuzzy) { @@ -77,7 +77,7 @@ seek_variables <- function(data, pattern, search = c("names", "labels"), fuzzy = } # search in variable labels? - if (any(search %in% c("labels", "all"))) { + 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) @@ -93,7 +93,7 @@ seek_variables <- function(data, pattern, search = c("names", "labels"), fuzzy = } # search for pattern in value labels or levels? - if (any(search %in% c("values", "levels", "all"))) { + if (any(seek %in% c("values", "levels", "all"))) { values <- insight::compact_list(lapply(data, function(i) { l <- attr(i, "labels", exact = TRUE) if (is.null(l) && is.factor(i)) { diff --git a/man/seek_variables.Rd b/man/seek_variables.Rd index b1493e10a..98132c7f7 100644 --- a/man/seek_variables.Rd +++ b/man/seek_variables.Rd @@ -4,7 +4,7 @@ \alias{seek_variables} \title{Find variables by its name, variable or value labels} \usage{ -seek_variables(data, pattern, search = c("names", "labels"), fuzzy = FALSE) +seek_variables(data, pattern, seek = c("names", "labels"), fuzzy = FALSE) } \arguments{ \item{data}{A data frame.} @@ -14,7 +14,7 @@ May also be a character vector of length > 1. \code{pattern} is searched for in column names, variable label and value labels attributes, or factor levels of variables in \code{data}.} -\item{search}{Character vector, indicating where \code{pattern} is sought. Use one +\item{seek}{Character vector, indicating where \code{pattern} is sought. Use one of more of the following options: \itemize{ \item \code{"name"}: searches in column names. @@ -57,7 +57,7 @@ seek_variables(efc, "dependency") # variable names and labels only, so no match seek_variables(efc, "female") # when we seek in all sources, we find the variable "e16sex" -seek_variables(efc, "female", search = "all") +seek_variables(efc, "female", seek = "all") # typo, no match seek_variables(iris, "Lenght") diff --git a/tests/testthat/test-seek_data.R b/tests/testthat/test-seek_data.R index 01bff9807..4b74b86af 100644 --- a/tests/testthat/test-seek_data.R +++ b/tests/testthat/test-seek_data.R @@ -5,18 +5,18 @@ test_that("seek_variables - simple use case", { expect_identical(out$labels, c("Sepal.Length", "Petal.Length")) }) -test_that("seek_variables - search label attribute", { +test_that("seek_variables - seek label attribute", { data(efc) out <- seek_variables(efc, "dependency") expect_identical(out$index, which(colnames(efc) == out$column)) expect_identical(out$labels, "elder's dependency") }) -test_that("seek_variables - search label attribute", { +test_that("seek_variables - seek label attribute", { data(efc) out <- seek_variables(efc, "female") expect_identical(nrow(out), 0L) - out <- seek_variables(efc, "female", search = "all") + out <- seek_variables(efc, "female", seek = "all") expect_identical(out$index, which(colnames(efc) == out$column)) expect_identical(out$labels, "elder's gender") }) @@ -32,7 +32,7 @@ test_that("seek_variables - fuzzy match", { test_that("seek_variables - fuzzy match, value labels", { data(efc) - out <- seek_variables(efc, "femlae", search = "all", fuzzy = TRUE) + out <- seek_variables(efc, "femlae", seek = "all", fuzzy = TRUE) expect_identical(nrow(out), 1L) expect_identical(out$index, which(colnames(efc) %in% out$column)) expect_identical(out$labels, "elder's gender") @@ -55,17 +55,17 @@ test_that("seek_variables - multiple pattern", { expect_identical(out$index, which(colnames(efc) %in% out$column)) expect_identical(out$labels, "elder's dependency") # two matches - out <- seek_variables(efc, c("female", "dependency"), search = "all") + out <- seek_variables(efc, c("female", "dependency"), seek = "all") expect_identical(nrow(out), 2L) expect_identical(out$index, which(colnames(efc) %in% out$column)) expect_identical(out$labels, c("elder's gender", "elder's dependency")) # only one match, typo - out <- seek_variables(efc, c("femlae", "dependency"), search = "all") + out <- seek_variables(efc, c("femlae", "dependency"), seek = "all") expect_identical(nrow(out), 1L) expect_identical(out$index, which(colnames(efc) %in% out$column)) expect_identical(out$labels, "elder's dependency") # two matches, despite typo - out <- seek_variables(efc, c("femlae", "dependency"), search = "all", fuzzy = TRUE) + out <- seek_variables(efc, c("femlae", "dependency"), seek = "all", fuzzy = TRUE) expect_identical(nrow(out), 2L) expect_identical(out$index, which(colnames(efc) %in% out$column)) expect_identical(out$labels, c("elder's gender", "elder's dependency")) @@ -73,5 +73,5 @@ test_that("seek_variables - multiple pattern", { test_that("seek_variables - valid input", { expect_error(seek_variables(rnorm(10), "Length"), regex = "`data` must be a data frame.") - expect_error(seek_variables(iris, "Length", search = "somewhere"), regex = "`search` must be") + expect_error(seek_variables(iris, "Length", seek = "somewhere"), regex = "`seek` must be") })