Skip to content

Commit

Permalink
Remove some aliases (#504)
Browse files Browse the repository at this point in the history
* Also address #265

* update docs

* update docs and tests

* Update extract_column_names.R

* update readme

* trigger CI

* fix

* do not remove find_columns yet

* fix news

* lintr, styler

* fix

* lintr

* add favicons to work around pkgdown bug

---------

Co-authored-by: Daniel <[email protected]>
Co-authored-by: Indrajeet Patil <[email protected]>
  • Loading branch information
3 people authored May 19, 2024
1 parent d9a40dc commit 139c61a
Show file tree
Hide file tree
Showing 96 changed files with 467 additions and 357 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ export(distribution_coef_var)
export(distribution_mode)
export(empty_columns)
export(empty_rows)
export(extract_column_names)
export(find_columns)
export(format_text)
export(get_columns)
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ BREAKING CHANGES
* `means_by_group()`
* `rescale_weights()`

* Following aliases are deprecated and will be removed in a future release (#504):

* `get_columns()`, use `data_select()` instead.
* `data_find()` and `find_columns()`, use `extract_column_names()` instead.
* `format_text()`, use `text_format()` instead.

CHANGES

* `recode_into()` is more relaxed regarding checking the type of `NA` values.
Expand Down
2 changes: 1 addition & 1 deletion R/adjust.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#' re-added. This avoids the centering around 0 that happens by default
#' when regressing out another variable (see the examples below for a
#' visual representation of this).
#' @inheritParams find_columns
#' @inheritParams extract_column_names
#' @inheritParams standardize
#'
#' @return A data frame comparable to `data`, with adjusted variables.
Expand Down
2 changes: 1 addition & 1 deletion R/assign_labels.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#' `x`, the right-hand side (RHS) the associated value label. Non-matching
#' labels are omitted.
#' @param ... Currently not used.
#' @inheritParams find_columns
#' @inheritParams extract_column_names
#'
#' @inheritSection center Selection of variables - the `select` argument
#'
Expand Down
2 changes: 1 addition & 1 deletion R/categorize.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
#' variables are appended with new column names (using the defined suffix) to
#' the original data frame.
#' @param ... not used.
#' @inheritParams find_columns
#' @inheritParams extract_column_names
#'
#' @inherit data_rename seealso
#'
Expand Down
48 changes: 24 additions & 24 deletions R/center.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#' order, unless a named vector is given. In this case, names are matched
#' against the names of the selected variables.
#' @param ... Currently not used.
#' @inheritParams find_columns
#' @inheritParams extract_column_names
#' @inheritParams standardize
#'
#' @section Selection of variables - the `select` argument:
Expand Down Expand Up @@ -118,21 +118,21 @@ center.numeric <- function(x,
center <- TRUE
}

args <- .process_std_center(x, weights, robust, verbose, reference, center, scale = NULL)
my_args <- .process_std_center(x, weights, robust, verbose, reference, center, scale = NULL)
dot_args <- list(...)

if (is.null(args)) {
if (is.null(my_args)) {
# all NA?
return(x)
} else if (is.null(args$check)) {
vals <- rep(0, length(args$vals)) # If only unique value
} else if (is.null(my_args$check)) {
vals <- rep(0, length(my_args$vals)) # If only unique value
} else {
vals <- as.vector(args$vals - args$center)
vals <- as.vector(my_args$vals - my_args$center)
}

centered_x <- rep(NA, length(args$valid_x))
centered_x[args$valid_x] <- vals
attr(centered_x, "center") <- args$center
centered_x <- rep(NA, length(my_args$valid_x))
centered_x[my_args$valid_x] <- vals
attr(centered_x, "center") <- my_args$center
attr(centered_x, "scale") <- 1
attr(centered_x, "robust") <- robust
# labels
Expand Down Expand Up @@ -197,29 +197,29 @@ center.data.frame <- function(x,
)

# process arguments
args <- .process_std_args(x, select, exclude, weights, append,
my_args <- .process_std_args(x, select, exclude, weights, append,
append_suffix = "_c", keep_factors = force, remove_na, reference,
.center = center, .scale = NULL
)

# set new values
x <- args$x
x <- my_args$x

for (var in args$select) {
for (var in my_args$select) {
x[[var]] <- center(
x[[var]],
robust = robust,
weights = args$weights,
weights = my_args$weights,
verbose = FALSE,
reference = reference[[var]],
center = args$center[var],
center = my_args$center[var],
force = force,
add_transform_class = FALSE
)
}

attr(x, "center") <- vapply(x[args$select], function(z) attributes(z)$center, numeric(1))
attr(x, "scale") <- vapply(x[args$select], function(z) attributes(z)$scale, numeric(1))
attr(x, "center") <- vapply(x[my_args$select], function(z) attributes(z)$center, numeric(1))
attr(x, "scale") <- vapply(x[my_args$select], function(z) attributes(z)$scale, numeric(1))
attr(x, "robust") <- robust
x
}
Expand Down Expand Up @@ -249,19 +249,19 @@ center.grouped_df <- function(x,
verbose = verbose
)

args <- .process_grouped_df(
my_args <- .process_grouped_df(
x, select, exclude, append,
append_suffix = "_c",
reference, weights, keep_factors = force
)

for (rows in args$grps) {
args$x[rows, ] <- center(
args$x[rows, , drop = FALSE],
select = args$select,
for (rows in my_args$grps) {
my_args$x[rows, ] <- center(
my_args$x[rows, , drop = FALSE],
select = my_args$select,
exclude = NULL,
robust = robust,
weights = args$weights,
weights = my_args$weights,
remove_na = remove_na,
verbose = verbose,
force = force,
Expand All @@ -272,8 +272,8 @@ center.grouped_df <- function(x,
)
}
# set back class, so data frame still works with dplyr
attributes(args$x) <- args$info
args$x
attributes(my_args$x) <- my_args$info
my_args$x
}


Expand Down
6 changes: 3 additions & 3 deletions R/convert_na_to.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ convert_na_to.character <- function(x, replacement = NULL, verbose = TRUE, ...)
#' @param replace_num Value to replace `NA` when variable is of type numeric.
#' @param replace_char Value to replace `NA` when variable is of type character.
#' @param replace_fac Value to replace `NA` when variable is of type factor.
#' @inheritParams find_columns
#' @inheritParams extract_column_names
#'
#' @rdname convert_na_to
#' @export
Expand All @@ -160,10 +160,10 @@ convert_na_to.data.frame <- function(x,
regex = FALSE,
verbose = TRUE,
...) {
data <- x
my_data <- x
select_nse <- .select_nse(
select,
data,
data = my_data,
exclude = exclude,
ignore_case,
regex = regex,
Expand Down
2 changes: 1 addition & 1 deletion R/convert_to_na.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' @param drop_levels Logical, for factors, when specific levels are replaced
#' by `NA`, should unused levels be dropped?
#' @param ... Not used.
#' @inheritParams find_columns
#' @inheritParams extract_column_names
#'
#' @return
#' `x`, where all values in `na` are converted to `NA`.
Expand Down
2 changes: 1 addition & 1 deletion R/data_addprefix.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' @rdname data_rename
#' @inheritParams find_columns
#' @inheritParams extract_column_names
#' @examples
#' # Add prefix / suffix to all columns
#' head(data_addprefix(iris, "NEW_"))
Expand Down
2 changes: 1 addition & 1 deletion R/data_codebook.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#' @param line_padding For HTML tables, the distance (in pixel) between lines.
#' @param row_color For HTML tables, the fill color for odd rows.
#' @inheritParams standardize.data.frame
#' @inheritParams find_columns
#' @inheritParams extract_column_names
#'
#' @return A formatted data frame, summarizing the content of the data frame.
#' Returned columns include the column index of the variables in the original
Expand Down
2 changes: 1 addition & 1 deletion R/data_duplicated.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' values for that row, to help in the decision-making when
#' selecting which duplicates to keep.
#'
#' @inheritParams find_columns
#' @inheritParams extract_column_names
#'
#' @keywords duplicates
#' @export
Expand Down
8 changes: 4 additions & 4 deletions R/data_extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#' @param verbose Toggle warnings.
#' @param ... For use by future methods.
#'
#' @inheritParams find_columns
#' @inheritParams extract_column_names
#'
#' @details `data_extract()` can be used to select multiple variables or pull a
#' single variable from a data frame. Thus, the return value is by default not
Expand Down Expand Up @@ -134,13 +134,13 @@ data_extract.data.frame <- function(data,
}

# we definitely should have a vector here when name not NULL
if (!is.null(name)) {
if (is.null(name)) {
data[, select, drop = !as_data_frame]
} else {
# if name indicates a variable, extract values for naming now
if (length(name) == 1L) {
name <- data[[name]]
}
stats::setNames(data[[select]], name)
} else {
data[, select, drop = !as_data_frame]
}
}
18 changes: 9 additions & 9 deletions R/data_group.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' grouping information from a grouped data frame.
#'
#' @param data A data frame
#' @inheritParams find_columns
#' @inheritParams extract_column_names
#'
#' @return A grouped data frame, i.e. a data frame with additional information
#' about the grouping structure saved as attributes.
Expand Down Expand Up @@ -41,26 +41,26 @@ data_group <- function(data,
verbose = verbose
)
# create grid with combinations of all levels
grid <- as.data.frame(expand.grid(lapply(data[select], unique)))
my_grid <- as.data.frame(expand.grid(lapply(data[select], unique)))
# sort grid
grid <- grid[do.call(order, grid), , drop = FALSE]
my_grid <- my_grid[do.call(order, my_grid), , drop = FALSE]

.rows <- lapply(seq_len(nrow(grid)), function(i) {
.rows <- lapply(seq_len(nrow(my_grid)), function(i) {
as.integer(data_match(
data,
to = grid[i, , drop = FALSE],
to = my_grid[i, , drop = FALSE],
match = "and",
return_indices = TRUE,
drop_na = FALSE
))
})
grid[[".rows"]] <- .rows
my_grid[[".rows"]] <- .rows

# remove data_match attributes
attr(grid, "out.attrs") <- NULL
attr(grid, ".drop") <- TRUE
attr(my_grid, "out.attrs") <- NULL
attr(my_grid, ".drop") <- TRUE

attr(data, "groups") <- grid
attr(data, "groups") <- my_grid
class(data) <- unique(c("grouped_df", "data.frame"), class(data))

data
Expand Down
4 changes: 2 additions & 2 deletions R/data_modify.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@
#' .modify = round
#' )
#'
#' # combine "data_find()" and ".at" argument
#' # combine "extract_column_names()" and ".at" argument
#' out <- data_modify(
#' d,
#' .at = data_find(d, select = starts_with("Sepal")),
#' .at = extract_column_names(d, select = starts_with("Sepal")),
#' .modify = as.factor
#' )
#' # "Sepal.Length" and "Sepal.Width" are now factors
Expand Down
2 changes: 1 addition & 1 deletion R/data_peek.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#' @param width Maximum width of line length to display. If `NULL`, width will
#' be determined using `options()$width`.
#' @param ... not used.
#' @inheritParams find_columns
#' @inheritParams extract_column_names
#'
#' @note To show only specific or a limited number of variables, use the
#' `select` argument, e.g. `select = 1:5` to show only the first five variables.
Expand Down
4 changes: 3 additions & 1 deletion R/data_relocate.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#' character vector, indicating the name of the destination column, or a
#' numeric value, indicating the index number of the destination column.
#' If `-1`, will be added before or after the last column.
#' @inheritParams find_columns
#' @inheritParams extract_column_names
#' @inheritParams data_rename
#'
#' @inherit data_rename seealso
Expand Down Expand Up @@ -97,6 +97,7 @@ data_relocate <- function(data,
original_after <- after

# Find new positions
# nolint start
if (!is.null(before)) {
before <- before[before %in% data_cols][1] # Take first that exists (if vector is supplied)
if (length(before) != 1 || is.na(before)) {
Expand All @@ -123,6 +124,7 @@ data_relocate <- function(data,
where <- 1
position <- union(position, where)
}
# nolint end

# Set left and right side
lhs <- setdiff(seq(1, where - 1), position)
Expand Down
2 changes: 1 addition & 1 deletion R/data_remove.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' @inheritParams find_columns
#' @inheritParams extract_column_names
#' @rdname data_relocate
#' @examples
#' # Remove columns
Expand Down
5 changes: 3 additions & 2 deletions R/data_rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@
#' - Functions to rename stuff: [data_rename()], [data_rename_rows()], [data_addprefix()], [data_addsuffix()]
#' - Functions to reorder or remove columns: [data_reorder()], [data_relocate()], [data_remove()]
#' - Functions to reshape, pivot or rotate data frames: [data_to_long()], [data_to_wide()], [data_rotate()]
#' - Functions to recode data: [rescale()], [reverse()], [categorize()], [recode_values()], [slide()]
#' - Functions to recode data: [rescale()], [reverse()], [categorize()],
#' [recode_values()], [slide()]
#' - Functions to standardize, normalize, rank-transform: [center()], [standardize()], [normalize()], [ranktransform()], [winsorize()]

Check warning on line 48 in R/data_rename.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/data_rename.R,line=48,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 134 characters.
#' - Split and merge data frames: [data_partition()], [data_merge()]
#' - Functions to find or select columns: [data_select()], [data_find()]
#' - Functions to find or select columns: [data_select()], [extract_column_names()]
#' - Functions to filter rows: [data_match()], [data_filter()]
#'
#' @export
Expand Down
2 changes: 1 addition & 1 deletion R/data_replicate.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#' provided in `expand` are removed from the data frame. If `FALSE` and `expand`
#' contains missing values, the function will throw an error.
#' @param ... Currently not used.
#' @inheritParams find_columns
#' @inheritParams extract_column_names
#'
#' @return A dataframe with each row replicated as many times as defined in `expand`.
#'
Expand Down
Loading

0 comments on commit 139c61a

Please sign in to comment.