Skip to content

Commit

Permalink
[read] add check_names argument. closes #1050 (#1051)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin committed Jun 17, 2024
1 parent d06c655 commit 2dd00b8
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
5 changes: 4 additions & 1 deletion R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -2501,6 +2502,7 @@ wbWorkbook <- R6::R6Class(
types,
named_region,
keep_attributes = FALSE,
check_names = FALSE,
...
) {

Expand Down Expand Up @@ -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
)
},

Expand Down
10 changes: 10 additions & 0 deletions R/read.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -149,6 +150,7 @@ wb_to_df <- function(
types,
named_region,
keep_attributes = FALSE,
check_names = FALSE,
...
) {

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -630,6 +636,7 @@ read_xlsx <- function(
na.strings = "#N/A",
na.numbers = NA,
fill_merged_cells = FALSE,
check_names = FALSE,
...
) {

Expand Down Expand Up @@ -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,
...
)
}
Expand All @@ -679,6 +687,7 @@ wb_read <- function(
named_region,
na.strings = "NA",
na.numbers = NA,
check_names = FALSE,
...
) {

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

Expand Down
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
Expand Up @@ -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)

})

0 comments on commit 2dd00b8

Please sign in to comment.