Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple rows input to row_to_names() #542

Merged
merged 15 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions R/row_to_names.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@
#' x %>%
#' row_to_names(row_number = "find_header")
#' @export
row_to_names <- function(dat, row_number, ..., remove_row = TRUE, remove_rows_above = TRUE) {
row_to_names <- function(dat, row_number, ..., remove_row = TRUE, remove_rows_above = TRUE, sep = "_") {
billdenney marked this conversation as resolved.
Show resolved Hide resolved
# Check inputs
if (!(is.logical(remove_row) & length(remove_row) == 1)) {
stop("remove_row must be either TRUE or FALSE, not ", as.character(remove_row))
} else if (!(is.logical(remove_rows_above) & length(remove_rows_above) == 1)) {
stop("remove_rows_above must be either TRUE or FALSE, not ", as.character(remove_rows_above))
} else if (length(row_number) != 1) {
stop("row_number must be a scalar")
}
if (row_number %in% "find_header") {
if (any(row_number %in% "find_header")) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This would be better as identical(row_number, "find_header") because having some rows as numbers and one as "find_header" would not be an input we would want to support.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, it was changed.

# no need to check if it is a character string, %in% will do that for us
# (and will handle the odd-ball cases like someone sending in
# factor("find_header")).
Expand All @@ -44,7 +42,16 @@ row_to_names <- function(dat, row_number, ..., remove_row = TRUE, remove_rows_ab
} else {
stop("row_number must be a numeric value or 'find_header'")
}
new_names <- as.character(unlist(dat[row_number, ], use.names = FALSE))
new_names <- dat[row_number, ] %>%
lapply(reduce_na) %>%
as.data.frame() %>%
replace(is.na(.), "") %>%
lapply(paste_skip_na, collapse = sep) %>%
lapply(stringr::str_replace_all, pattern = "__+", replacement = "_") %>%
lapply(stringr::str_replace_all, pattern = "_$", replacement = "") %>%
unlist(use.names = FALSE) %>%
as.character()
billdenney marked this conversation as resolved.
Show resolved Hide resolved

if (any(duplicated(new_names))) {
rlang::warn(
message=paste("Row", row_number, "does not provide unique names. Consider running clean_names() after row_to_names()."),
Expand Down Expand Up @@ -139,3 +146,9 @@ find_header <- function(dat, ...) {
}
ret
}

reduce_na <- function(x) {
billdenney marked this conversation as resolved.
Show resolved Hide resolved
if (all(is.na(x)))
x[1] <- "NA"
x
}
6 changes: 0 additions & 6 deletions tests/testthat/test-row-to-names.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ example_data_row_to_names[[3]] <- tibble::as_tibble(example_data_row_to_names[[1
names(example_data_row_to_names)[3] <- "tibble"

test_that("row_to_names invalid and semi-valid input checking", {
expect_error(
example_data_row_to_names[[1]] %>%
row_to_names(row_number = 1:2),
regexp="row_number must be a scalar"
)

expect_error(
row_to_names(example_data_row_to_names[[1]], row_number=1, remove_row="A"),
regexp="remove_row must be either TRUE or FALSE, not A",
Expand Down