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

[read] add check_names argument. closes #1050 #1051

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Changes from 1 commit
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
Next Next commit
[read] add check_names argument. closes #1050
JanMarvin committed Jun 14, 2024
commit 02afc3bcbd1755ede86f88e0806f1563f24efa35
5 changes: 4 additions & 1 deletion R/class-workbook.R
Original file line number Diff line number Diff line change
@@ -2478,6 +2478,7 @@ wbWorkbook <- R6::R6Class(
#' @param na.numbers A numeric vector of digits which are to be interpreted as NA. Blank cells will be returned as NA.
#' @param fill_merged_cells If TRUE, the value in a merged cell is given to all cells within the merge.
#' @param keep_attributes If TRUE additional attributes are returned. (These are used internally to define a cell type.)
#' @param check_names If TRUE then the names of the variables in the data frame are checked to ensure that they are syntactically valid variable names.
#' @return a data frame
to_df = function(
sheet,
@@ -2501,6 +2502,7 @@ wbWorkbook <- R6::R6Class(
types,
named_region,
keep_attributes = FALSE,
check_names = FALSE,
...
) {

@@ -2531,7 +2533,8 @@ wbWorkbook <- R6::R6Class(
show_formula = show_formula,
convert = convert,
types = types,
named_region = named_region
named_region = named_region,
check_names = check_names
)
},

10 changes: 10 additions & 0 deletions R/read.R
Original file line number Diff line number Diff line change
@@ -59,6 +59,7 @@
#' @param fill_merged_cells If `TRUE`, the value in a merged cell is given to all cells within the merge.
#' @param keep_attributes If `TRUE` additional attributes are returned.
#' (These are used internally to define a cell type.)
#' @param check_names If `TRUE` then the names of the variables in the data frame are checked to ensure that they are syntactically valid variable names.
#' @param ... additional arguments
#'
#' @examples
@@ -149,6 +150,7 @@ wb_to_df <- function(
types,
named_region,
keep_attributes = FALSE,
check_names = FALSE,
...
) {

@@ -596,6 +598,10 @@ wb_to_df <- function(
}

if (col_names) {
if (check_names) {
xlsx_cols_names <- make.names(xlsx_cols_names, unique = TRUE)
}

names(z) <- xlsx_cols_names
names(tt) <- xlsx_cols_names
}
@@ -630,6 +636,7 @@ read_xlsx <- function(
na.strings = "#N/A",
na.numbers = NA,
fill_merged_cells = FALSE,
check_names = FALSE,
...
) {

@@ -657,6 +664,7 @@ read_xlsx <- function(
na.strings = na.strings,
na.numbers = na.numbers,
fill_merged_cells = fill_merged_cells,
check_names = check_names,
...
)
}
@@ -679,6 +687,7 @@ wb_read <- function(
named_region,
na.strings = "NA",
na.numbers = NA,
check_names = FALSE,
...
) {

@@ -705,6 +714,7 @@ wb_read <- function(
named_region = named_region,
na.strings = na.strings,
na.numbers = na.numbers,
check_names = check_names,
...
)

3 changes: 3 additions & 0 deletions man/wbWorkbook.Rd

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

5 changes: 5 additions & 0 deletions man/wb_to_df.Rd

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

20 changes: 20 additions & 0 deletions tests/testthat/test-read_from_created_wb.R
Original file line number Diff line number Diff line change
@@ -201,3 +201,23 @@ test_that("column names are not missing with col_names = FALSE", {
expect_equal(exp, got)

})

test_that("check_names works", {

dd <- data.frame(
"a and b" = 1:2,
"a-and-b" = 3:4,
check.names = FALSE
)

wb <- write_xlsx(x = dd)

exp <- c("a and b", "a-and-b")
got <- names(wb_to_df(wb, check_names = FALSE))
expect_equal(exp, got)

exp <- c("a.and.b", "a.and.b.1")
got <- names(wb_to_df(wb, check_names = TRUE))
expect_equal(exp, got)

})