Skip to content

Commit

Permalink
xlsx_file -> file
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin committed Jul 26, 2023
1 parent a53985e commit 779b32a
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 81 deletions.
5 changes: 1 addition & 4 deletions R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -1466,7 +1466,7 @@ wbWorkbook <- R6::R6Class(
standardize_case_names(...)

wb_to_df(
xlsx_file = self,
file = self,
sheet = sheet,
start_row = start_row,
start_col = start_col,
Expand All @@ -1493,15 +1493,13 @@ wbWorkbook <- R6::R6Class(
### load workbook ----
#' @description load workbook
#' @param file file
#' @param xlsx_file xlsx_file
#' @param sheet sheet
#' @param data_only data_only
#' @param calc_chain calc_chain
#' @param ... additional arguments
#' @return The `wbWorkbook` object invisibly
load = function(
file,
xlsx_file = NULL,
sheet,
data_only = FALSE,
calc_chain = FALSE,
Expand All @@ -1511,7 +1509,6 @@ wbWorkbook <- R6::R6Class(
if (missing(sheet)) sheet <- substitute()
self <- wb_load(
file = file,
xlsx_file = xlsx_file,
sheet = sheet,
data_only = data_only,
calc_chain = calc_chain,
Expand Down
33 changes: 23 additions & 10 deletions R/read.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' written down. [read_xlsx()] and [wb_read()] are just internal wrappers for
#' [wb_to_df()] intended for people coming from other packages.
#'
#' @param xlsx_file An xlsx file, Workbook object or URL to xlsx file.
#' @param file An xlsx file, Workbook object or URL to xlsx file.
#' @param sheet Either sheet name or index. When missing the first sheet in the workbook is selected.
#' @param col_names If TRUE, the first row of data will be used as column names.
#' @param row_names If TRUE, the first col of data will be used as row names.
Expand Down Expand Up @@ -91,7 +91,7 @@
#'
#' @export
wb_to_df <- function(
xlsx_file,
file,
sheet,
start_row = 1,
start_col = NULL,
Expand All @@ -116,19 +116,26 @@ wb_to_df <- function(
...
) {


xlsx_file <- list(...)$xlsx_file
standardize_case_names(...)

if (!is.null(xlsx_file)) {
.Deprecated(old = "xlsx_file", new = "file", package = "openxlsx2")
file <- xlsx_file %||% file
}

if (!is.null(cols)) cols <- col2int(cols)

if (inherits(xlsx_file, "wbWorkbook")) {
wb <- xlsx_file
if (inherits(file, "wbWorkbook")) {
wb <- file
} else {
# passes missing further on
if (missing(sheet))
sheet <- substitute()

# possible false positive on current lintr runs
wb <- wb_load(xlsx_file, sheet = sheet, data_only = TRUE) # nolint
wb <- wb_load(file, sheet = sheet, data_only = TRUE) # nolint
}

if (!missing(named_region)) {
Expand Down Expand Up @@ -418,7 +425,7 @@ wb_to_df <- function(
# TODO there probably is a better way in not reducing cc above, so
# that we do not have to go through large xlsx files multiple times
z_fill <- wb_to_df(
xlsx_file = wb,
file = wb,
sheet = sheet,
dims = filler,
na.strings = na.strings,
Expand Down Expand Up @@ -578,7 +585,7 @@ wb_to_df <- function(
#'
#' @export
read_xlsx <- function(
xlsx_file,
file,
sheet,
start_row = 1,
start_col = NULL,
Expand All @@ -598,11 +605,14 @@ read_xlsx <- function(

# keep sheet missing // read_xlsx is the function to replace.
# dont mess with wb_to_df
if (missing(file))
file <- substitute()

if (missing(sheet))
sheet <- substitute()

wb_to_df(
xlsx_file,
file = file,
sheet = sheet,
start_row = start_row,
start_col = start_col,
Expand Down Expand Up @@ -633,7 +643,7 @@ read_xlsx <- function(
#' @seealso [wb_get_named_regions()]
#' @export
wb_read <- function(
xlsx_file,
file,
sheet = 1,
start_row = 1,
start_col = NULL,
Expand All @@ -652,11 +662,14 @@ wb_read <- function(

# keep sheet missing // read_xlsx is the function to replace.
# dont mess with wb_to_df
if (missing(file))
file <- substitute()

if (missing(sheet))
sheet <- substitute()

wb_to_df(
xlsx_file = xlsx_file,
file = file,
sheet = sheet,
start_row = start_row,
start_col = start_col,
Expand Down
9 changes: 6 additions & 3 deletions R/wb_load.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#' @name wb_load
#' @title Load an existing .xlsx file
#' @param file A path to an existing .xlsx or .xlsm file
#' @param xlsx_file alias for file
#' @param sheet optional sheet parameter. if this is applied, only the selected
#' sheet will be loaded.
#' @param data_only mode to import if only a data frame should be returned. This
Expand Down Expand Up @@ -33,16 +32,20 @@
#' wb$add_worksheet("A new worksheet")
wb_load <- function(
file,
xlsx_file = NULL,
sheet,
data_only = FALSE,
calc_chain = FALSE,
...
) {

xlsx_file <- list(...)$xlsx_file
standardize_case_names(...)

file <- xlsx_file %||% file
if (!is.null(xlsx_file)) {
.Deprecated(old = "xlsx_file", new = "file", package = "openxlsx2")
file <- xlsx_file %||% file
}

file <- getFile(file)

if (!file.exists(file)) {
Expand Down
11 changes: 1 addition & 10 deletions man/wbWorkbook.Rd

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

11 changes: 1 addition & 10 deletions man/wb_load.Rd

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

8 changes: 4 additions & 4 deletions man/wb_to_df.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ expect_pseudo_wrapper <- function(
method_args <- names(method_forms)
fun_args <- names(fun_forms)

ignore <- "xlsx_file"
ignore <- "file"

# remove ignores from fun
m <- match(ignore, fun_args, 0L)
Expand Down
42 changes: 21 additions & 21 deletions tests/testthat/test-named_regions.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,18 @@ test_that("Missing rows in named regions", {
######################################################################## from Workbook

## Skip empty rows
x <- read_xlsx(xlsxFile = wb, namedRegion = "iris", colNames = TRUE, skipEmptyRows = TRUE)
x <- read_xlsx(file = wb, namedRegion = "iris", colNames = TRUE, skipEmptyRows = TRUE)
expect_equal(dim(x), c(4, 2))

x <- read_xlsx(xlsxFile = wb, namedRegion = "iris2", colNames = TRUE, skipEmptyRows = TRUE)
x <- read_xlsx(file = wb, namedRegion = "iris2", colNames = TRUE, skipEmptyRows = TRUE)
expect_equal(dim(x), c(5, 2))


## Keep empty rows
x <- read_xlsx(xlsxFile = wb, namedRegion = "iris", colNames = TRUE, skipEmptyRows = FALSE)
x <- read_xlsx(file = wb, namedRegion = "iris", colNames = TRUE, skipEmptyRows = FALSE)
expect_equal(dim(x), c(5, 2))

x <- read_xlsx(xlsxFile = wb, namedRegion = "iris2", colNames = TRUE, skipEmptyRows = FALSE)
x <- read_xlsx(file = wb, namedRegion = "iris2", colNames = TRUE, skipEmptyRows = FALSE)
expect_equal(dim(x), c(6, 2))


Expand All @@ -198,18 +198,18 @@ test_that("Missing rows in named regions", {
wb_save(wb, temp_file)

## Skip empty rows
x <- read_xlsx(xlsxFile = temp_file, namedRegion = "iris", colNames = TRUE, skipEmptyRows = TRUE)
x <- read_xlsx(file = temp_file, namedRegion = "iris", colNames = TRUE, skipEmptyRows = TRUE)
expect_equal(dim(x), c(4, 2))

x <- read_xlsx(xlsxFile = temp_file, namedRegion = "iris2", colNames = TRUE, skipEmptyRows = TRUE)
x <- read_xlsx(file = temp_file, namedRegion = "iris2", colNames = TRUE, skipEmptyRows = TRUE)
expect_equal(dim(x), c(5, 2))


## Keep empty rows
x <- read_xlsx(xlsxFile = temp_file, namedRegion = "iris", colNames = TRUE, skipEmptyRows = FALSE)
x <- read_xlsx(file = temp_file, namedRegion = "iris", colNames = TRUE, skipEmptyRows = FALSE)
expect_equal(dim(x), c(5, 2))

x <- read_xlsx(xlsxFile = temp_file, namedRegion = "iris2", colNames = TRUE, skipEmptyRows = FALSE)
x <- read_xlsx(file = temp_file, namedRegion = "iris2", colNames = TRUE, skipEmptyRows = FALSE)
expect_equal(dim(x), c(6, 2))

unlink(temp_file)
Expand Down Expand Up @@ -255,18 +255,18 @@ test_that("Missing columns in named regions", {
######################################################################## from Workbook

## Skip empty cols
x <- read_xlsx(xlsxFile = wb, namedRegion = "iris", colNames = TRUE, skipEmptyCols = TRUE)
x <- read_xlsx(file = wb, namedRegion = "iris", colNames = TRUE, skipEmptyCols = TRUE)
expect_equal(dim(x), c(4, 1))

x <- read_xlsx(xlsxFile = wb, namedRegion = "iris2", colNames = TRUE, skipEmptyCols = TRUE)
x <- read_xlsx(file = wb, namedRegion = "iris2", colNames = TRUE, skipEmptyCols = TRUE)
expect_equal(dim(x), c(4, 2))


## Keep empty cols
x <- read_xlsx(xlsxFile = wb, namedRegion = "iris", colNames = TRUE, skipEmptyCols = FALSE)
x <- read_xlsx(file = wb, namedRegion = "iris", colNames = TRUE, skipEmptyCols = FALSE)
expect_equal(dim(x), c(4, 2))

x <- read_xlsx(xlsxFile = wb, namedRegion = "iris2", colNames = TRUE, skipEmptyCols = FALSE)
x <- read_xlsx(file = wb, namedRegion = "iris2", colNames = TRUE, skipEmptyCols = FALSE)
expect_equal(dim(x), c(4, 3))


Expand All @@ -275,18 +275,18 @@ test_that("Missing columns in named regions", {
wb_save(wb, temp_file)

## Skip empty cols
x <- read_xlsx(xlsxFile = temp_file, namedRegion = "iris", colNames = TRUE, skipEmptyCols = TRUE)
x <- read_xlsx(file = temp_file, namedRegion = "iris", colNames = TRUE, skipEmptyCols = TRUE)
expect_equal(dim(x), c(4, 1))

x <- read_xlsx(xlsxFile = temp_file, namedRegion = "iris2", colNames = TRUE, skipEmptyCols = TRUE)
x <- read_xlsx(file = temp_file, namedRegion = "iris2", colNames = TRUE, skipEmptyCols = TRUE)
expect_equal(dim(x), c(4, 2))


## Keep empty cols
x <- read_xlsx(xlsxFile = temp_file, namedRegion = "iris", colNames = TRUE, skipEmptyCols = FALSE)
x <- read_xlsx(file = temp_file, namedRegion = "iris", colNames = TRUE, skipEmptyCols = FALSE)
expect_equal(dim(x), c(4, 2))

x <- read_xlsx(xlsxFile = temp_file, namedRegion = "iris2", colNames = TRUE, skipEmptyCols = FALSE)
x <- read_xlsx(file = temp_file, namedRegion = "iris2", colNames = TRUE, skipEmptyCols = FALSE)
expect_equal(dim(x), c(4, 3))

unlink(temp_file)
Expand Down Expand Up @@ -324,13 +324,13 @@ test_that("Matching Substrings breaks reading named regions", {


## read file named region
expect_equal(t1, read_xlsx(xlsxFile = temp_file, namedRegion = "t"), ignore_attr = TRUE)
expect_equal(t2, read_xlsx(xlsxFile = temp_file, namedRegion = "t2", rowNames = TRUE), ignore_attr = TRUE)
expect_equal(head(t1, 3), read_xlsx(xlsxFile = temp_file, namedRegion = "t1"), ignore_attr = TRUE)
expect_equal(head(t2, 3), read_xlsx(xlsxFile = temp_file, namedRegion = "t22", rowNames = TRUE), ignore_attr = TRUE)
expect_equal(t1, read_xlsx(file = temp_file, namedRegion = "t"), ignore_attr = TRUE)
expect_equal(t2, read_xlsx(file = temp_file, namedRegion = "t2", rowNames = TRUE), ignore_attr = TRUE)
expect_equal(head(t1, 3), read_xlsx(file = temp_file, namedRegion = "t1"), ignore_attr = TRUE)
expect_equal(head(t2, 3), read_xlsx(file = temp_file, namedRegion = "t22", rowNames = TRUE), ignore_attr = TRUE)

## read Workbook named region
expect_equal(t1, read_xlsx(xlsxFile = wb, namedRegion = "t"), ignore_attr = TRUE)
expect_equal(t1, read_xlsx(file = wb, namedRegion = "t"), ignore_attr = TRUE)
expect_equal(t2, read_xlsx(xlsxFile = wb, namedRegion = "t2", rowNames = TRUE), ignore_attr = TRUE)
expect_equal(head(t1, 3), read_xlsx(xlsxFile = wb, namedRegion = "t1"), ignore_attr = TRUE)
expect_equal(head(t2, 3), read_xlsx(xlsxFile = wb, namedRegion = "t22", rowNames = TRUE), ignore_attr = TRUE)
Expand Down
Loading

0 comments on commit 779b32a

Please sign in to comment.