Skip to content

Commit

Permalink
Preserve attr (#142)
Browse files Browse the repository at this point in the history
* Update data_findcols.R

* Update test-get_columns.R

* Update test-data_relocate.R

* add tests

* Update test-data_remove.R

* Update data_relocate.R

* Update test-data_rename.R

* Update test-convert_na_to.R

* fix weighted_median

* add format-method

* docs, add alias

* docs

* typo
  • Loading branch information
strengejacke authored Mar 25, 2022
1 parent 603ccfb commit c97eed4
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 61 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export(data_rescale)
export(data_restoretype)
export(data_reverse)
export(data_rotate)
export(data_select)
export(data_to_long)
export(data_to_numeric)
export(data_to_wide)
Expand Down
39 changes: 2 additions & 37 deletions R/data_findcols.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#' @title Find or get columns in a data frame based on search patterns
#' @name find_columns
#'
#' @description `find_columns()` returns colum names from a data set that
#' @description `find_columns()` returns column names from a data set that
#' match a certain search pattern, while `get_columns()` returns the found data.
#' `data_select()` is an alias for `get_columns()`.
#'
#' @param data A data frame.
#' @param select Variables that will be included when performing the required
Expand Down Expand Up @@ -114,42 +115,6 @@ find_columns <- function(data,
}


#' @rdname find_columns
#' @export
get_columns <- function(data,
select = NULL,
exclude = NULL,
ignore_case = FALSE,
regex = FALSE,
verbose = TRUE,
...) {
columns <- .select_nse(
select,
data,
exclude,
ignore_case = ignore_case,
regex = regex,
verbose = FALSE
)

# save attributes
a <- attributes(data)

if (!length(columns) || is.null(columns)) {
if (isTRUE(verbose)) {
warning(insight::format_message("No column names that matched the required search pattern were found."), call. = FALSE)
}
return(NULL)
}

out <- data[columns]

# add back attributes
attributes(out) <- utils::modifyList(a, attributes(out))
out
}


#' @param pattern A regular expression (as character string), representing the
#' pattern to be matched in the in column names. Can also be one of the
#' following select-helpers: `starts_with("")`, `end_with("")`, `regex("")`,
Expand Down
16 changes: 8 additions & 8 deletions R/data_relocate.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#' @title Relocate (reorder) columns of a data frame
#' @name data_relocate
#'
#' @description
#' `data_relocate()` will reorder columns to specific positions, indicated by
#' `before` or `after`. `data_reorder()` will instead move selected columns to
#' the beginning of a data frame. Finally, `data_remove()` removes columns
#' from a data frame. All functions support select-helpers that allow flexible
#' specification of a search pattern to find matching columns, which should
#' be reordered or removed.
#'
#' @param data A data frame.
#' @param before,after Destination of columns. Supplying neither will move
#' columns to the left-hand side; specifying both is an error. Can be a
Expand All @@ -12,14 +20,6 @@
#'
#' @inherit data_rename seealso
#'
#' @details
#'
#' `data_relocate()` will reorder columns to specific positions, indicated by
#' `before` or `after`.
#'
#' `data_reorder()` will instead move selected columns to the beginning of the
#' data frame .
#'
#' @return A data frame with reordered columns.
#'
#' @examples
Expand Down
8 changes: 7 additions & 1 deletion R/data_rename.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#' @title Rename columns and variable names
#' @name data_rename
#'
#' @description Safe and intuitive functions to rename variables or rows in dataframes.
#' @description Safe and intuitive functions to rename variables or rows in
#' data frames. `data_rename()` will rename column names, i.e. it facilitates
#' renaming variables `data_addprefix()` or `data_addsuffix()` add prefixes
#' or suffixes to column names. `data_rename_rows()` is a convenient shortcut
#' to add or rename row names of a data frame, but unlike `row.names()`, its
#' input and output is a data frame, thus, integrating smoothly into a possible
#' pipe-workflow.
#'
#' @param data A data frame, or an object that can be coerced to a data frame.
#' @param pattern Character vector. For `data_rename()`, indicates columns that
Expand Down
39 changes: 39 additions & 0 deletions R/data_select.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#' @rdname find_columns
#' @export
get_columns <- function(data,
select = NULL,
exclude = NULL,
ignore_case = FALSE,
regex = FALSE,
verbose = TRUE,
...) {
columns <- .select_nse(
select,
data,
exclude,
ignore_case = ignore_case,
regex = regex,
verbose = FALSE
)

# save attributes
a <- attributes(data)

if (!length(columns) || is.null(columns)) {
if (isTRUE(verbose)) {
warning(insight::format_message("No column names that matched the required search pattern were found."), call. = FALSE)
}
return(NULL)
}

out <- data[columns]

# add back attributes
attributes(out) <- utils::modifyList(a, attributes(out))
out
}


#' @rdname find_columns
#' @export
data_select <- get_columns
12 changes: 5 additions & 7 deletions man/data_relocate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion man/data_rename.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 19 additions & 7 deletions man/find_columns.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit c97eed4

@strengejacke
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IndrajeetPatil I think you need to update snapshot tests here, everything else should be ok.

Please sign in to comment.