From 779b32ada72632391970f36fb862a350f8c196e3 Mon Sep 17 00:00:00 2001 From: Jan Marvin Garbuszus Date: Wed, 26 Jul 2023 19:50:04 +0200 Subject: [PATCH] xlsx_file -> file --- R/class-workbook.R | 5 +-- R/read.R | 33 ++++++++++----- R/wb_load.R | 9 ++-- man/wbWorkbook.Rd | 11 +---- man/wb_load.Rd | 11 +---- man/wb_to_df.Rd | 8 ++-- tests/testthat/helper.R | 2 +- tests/testthat/test-named_regions.R | 42 +++++++++---------- tests/testthat/test-read_xlsx_correct_sheet.R | 24 +++++------ vignettes/Update-from-openxlsx.Rmd | 4 +- vignettes/openxlsx2.Rmd | 8 ++-- 11 files changed, 76 insertions(+), 81 deletions(-) diff --git a/R/class-workbook.R b/R/class-workbook.R index 71f2b732a..e66f5843d 100644 --- a/R/class-workbook.R +++ b/R/class-workbook.R @@ -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, @@ -1493,7 +1493,6 @@ 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 @@ -1501,7 +1500,6 @@ wbWorkbook <- R6::R6Class( #' @return The `wbWorkbook` object invisibly load = function( file, - xlsx_file = NULL, sheet, data_only = FALSE, calc_chain = FALSE, @@ -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, diff --git a/R/read.R b/R/read.R index a1691afe6..64c1f1c57 100644 --- a/R/read.R +++ b/R/read.R @@ -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. @@ -91,7 +91,7 @@ #' #' @export wb_to_df <- function( - xlsx_file, + file, sheet, start_row = 1, start_col = NULL, @@ -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)) { @@ -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, @@ -578,7 +585,7 @@ wb_to_df <- function( #' #' @export read_xlsx <- function( - xlsx_file, + file, sheet, start_row = 1, start_col = NULL, @@ -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, @@ -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, @@ -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, diff --git a/R/wb_load.R b/R/wb_load.R index 29d9a5309..cbb26b664 100644 --- a/R/wb_load.R +++ b/R/wb_load.R @@ -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 @@ -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)) { diff --git a/man/wbWorkbook.Rd b/man/wbWorkbook.Rd index a724810bf..1b2f2320b 100644 --- a/man/wbWorkbook.Rd +++ b/man/wbWorkbook.Rd @@ -908,14 +908,7 @@ a data frame \subsection{Method \code{load()}}{ load workbook \subsection{Usage}{ -\if{html}{\out{
}}\preformatted{wbWorkbook$load( - file, - xlsx_file = NULL, - sheet, - data_only = FALSE, - calc_chain = FALSE, - ... -)}\if{html}{\out{
}} +\if{html}{\out{
}}\preformatted{wbWorkbook$load(file, sheet, data_only = FALSE, calc_chain = FALSE, ...)}\if{html}{\out{
}} } \subsection{Arguments}{ @@ -923,8 +916,6 @@ load workbook \describe{ \item{\code{file}}{file} -\item{\code{xlsx_file}}{xlsx_file} - \item{\code{sheet}}{sheet} \item{\code{data_only}}{data_only} diff --git a/man/wb_load.Rd b/man/wb_load.Rd index 4badaebd0..682d9e0f0 100644 --- a/man/wb_load.Rd +++ b/man/wb_load.Rd @@ -4,20 +4,11 @@ \alias{wb_load} \title{Load an existing .xlsx file} \usage{ -wb_load( - file, - xlsx_file = NULL, - sheet, - data_only = FALSE, - calc_chain = FALSE, - ... -) +wb_load(file, sheet, data_only = FALSE, calc_chain = FALSE, ...) } \arguments{ \item{file}{A path to an existing .xlsx or .xlsm file} -\item{xlsx_file}{alias for file} - \item{sheet}{optional sheet parameter. if this is applied, only the selected sheet will be loaded.} diff --git a/man/wb_to_df.Rd b/man/wb_to_df.Rd index b416f0017..6a096fd2f 100644 --- a/man/wb_to_df.Rd +++ b/man/wb_to_df.Rd @@ -7,7 +7,7 @@ \title{Create Dataframe from Workbook} \usage{ wb_to_df( - xlsx_file, + file, sheet, start_row = 1, start_col = NULL, @@ -33,7 +33,7 @@ wb_to_df( ) read_xlsx( - xlsx_file, + file, sheet, start_row = 1, start_col = NULL, @@ -52,7 +52,7 @@ read_xlsx( ) wb_read( - xlsx_file, + file, sheet = 1, start_row = 1, start_col = NULL, @@ -70,7 +70,7 @@ wb_read( ) } \arguments{ -\item{xlsx_file}{An xlsx file, Workbook object or URL to xlsx file.} +\item{file}{An xlsx file, Workbook object or URL to xlsx file.} \item{sheet}{Either sheet name or index. When missing the first sheet in the workbook is selected.} diff --git a/tests/testthat/helper.R b/tests/testthat/helper.R index ae40c42c9..35f1ac9e7 100644 --- a/tests/testthat/helper.R +++ b/tests/testthat/helper.R @@ -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) diff --git a/tests/testthat/test-named_regions.R b/tests/testthat/test-named_regions.R index 785a09a40..501754b16 100644 --- a/tests/testthat/test-named_regions.R +++ b/tests/testthat/test-named_regions.R @@ -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)) @@ -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) @@ -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)) @@ -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) @@ -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) diff --git a/tests/testthat/test-read_xlsx_correct_sheet.R b/tests/testthat/test-read_xlsx_correct_sheet.R index c1d12bd0c..30c3870d8 100644 --- a/tests/testthat/test-read_xlsx_correct_sheet.R +++ b/tests/testthat/test-read_xlsx_correct_sheet.R @@ -11,17 +11,17 @@ test_that("read_xlsx correct sheet", { expect_equal(object = sheet_names, expected = expected_sheet_names) - expect_equal(read_xlsx(xlsxFile = fl, sheet = 7), data.frame(x = 1), ignore_attr = TRUE) - expect_equal(read_xlsx(xlsxFile = fl, sheet = 8), data.frame(x = 11), ignore_attr = TRUE) - expect_equal(read_xlsx(xlsxFile = fl, sheet = 9), data.frame(x = 111), ignore_attr = TRUE) - expect_equal(read_xlsx(xlsxFile = fl, sheet = 10), data.frame(x = 1111), ignore_attr = TRUE) - expect_equal(read_xlsx(xlsxFile = fl, sheet = 11), data.frame(x = 11111), ignore_attr = TRUE) - expect_equal(read_xlsx(xlsxFile = fl, sheet = 12), data.frame(x = 111111), ignore_attr = TRUE) + expect_equal(read_xlsx(file = fl, sheet = 7), data.frame(x = 1), ignore_attr = TRUE) + expect_equal(read_xlsx(file = fl, sheet = 8), data.frame(x = 11), ignore_attr = TRUE) + expect_equal(read_xlsx(file = fl, sheet = 9), data.frame(x = 111), ignore_attr = TRUE) + expect_equal(read_xlsx(file = fl, sheet = 10), data.frame(x = 1111), ignore_attr = TRUE) + expect_equal(read_xlsx(file = fl, sheet = 11), data.frame(x = 11111), ignore_attr = TRUE) + expect_equal(read_xlsx(file = fl, sheet = 12), data.frame(x = 111111), ignore_attr = TRUE) - expect_equal(read_xlsx(xlsxFile = fl, sheet = "1"), data.frame(x = 1), ignore_attr = TRUE) - expect_equal(read_xlsx(xlsxFile = fl, sheet = "11"), data.frame(x = 11), ignore_attr = TRUE) - expect_equal(read_xlsx(xlsxFile = fl, sheet = "111"), data.frame(x = 111), ignore_attr = TRUE) - expect_equal(read_xlsx(xlsxFile = fl, sheet = "1111"), data.frame(x = 1111), ignore_attr = TRUE) - expect_equal(read_xlsx(xlsxFile = fl, sheet = "11111"), data.frame(x = 11111), ignore_attr = TRUE) - expect_equal(read_xlsx(xlsxFile = fl, sheet = "111111"), data.frame(x = 111111), ignore_attr = TRUE) + expect_equal(read_xlsx(file = fl, sheet = "1"), data.frame(x = 1), ignore_attr = TRUE) + expect_equal(read_xlsx(file = fl, sheet = "11"), data.frame(x = 11), ignore_attr = TRUE) + expect_equal(read_xlsx(file = fl, sheet = "111"), data.frame(x = 111), ignore_attr = TRUE) + expect_equal(read_xlsx(file = fl, sheet = "1111"), data.frame(x = 1111), ignore_attr = TRUE) + expect_equal(read_xlsx(file = fl, sheet = "11111"), data.frame(x = 11111), ignore_attr = TRUE) + expect_equal(read_xlsx(file = fl, sheet = "111111"), data.frame(x = 111111), ignore_attr = TRUE) }) diff --git a/vignettes/Update-from-openxlsx.Rmd b/vignettes/Update-from-openxlsx.Rmd index 96e912d14..dae3d5a20 100644 --- a/vignettes/Update-from-openxlsx.Rmd +++ b/vignettes/Update-from-openxlsx.Rmd @@ -22,7 +22,7 @@ While previous `openxlsx` functions used the `.` in function calls, as well as c The basic read function changed from `read.xlsx` to `read_xlsx`. Using a default xlsx file included in the package: ```{r read} -xlsx_file <- system.file("extdata", "openxlsx2_example.xlsx", package = "openxlsx2") +file <- system.file("extdata", "openxlsx2_example.xlsx", package = "openxlsx2") ``` The old syntax looked like this: @@ -36,7 +36,7 @@ This has changed to this: ```{r new_read} # read in openxlsx2 -openxlsx2::read_xlsx(xlsx_file = xlsx_file) +openxlsx2::read_xlsx(file = file) ``` diff --git a/vignettes/openxlsx2.Rmd b/vignettes/openxlsx2.Rmd index c8bb5b63e..60b9a8619 100644 --- a/vignettes/openxlsx2.Rmd +++ b/vignettes/openxlsx2.Rmd @@ -30,14 +30,14 @@ For this example we will use example data provided by the package. You can locat ### Basic import We begin with the `openxlsx2_example.xlsx` file by telling R where to find this file on our system ```{r} -xlsx_file <- system.file("extdata", "openxlsx2_example.xlsx", package = "openxlsx2") +file <- system.file("extdata", "openxlsx2_example.xlsx", package = "openxlsx2") ``` The object contains a path to the xlsx file and we pass this file to our function to read the workbook into R ```{r} # import workbook library(openxlsx2) -wb_to_df(xlsx_file) +wb_to_df(file) ``` The output is created as a data frame and contains data types date, logical, numeric and character. The function to import the file to R, `wb_to_df()` provides similar options as the `openxlsx` functions `read.xlsx()` and `readWorkbook()` and a few new functions we will go through the options. As you might have noticed, we return the column of the xlsx file as the row name of the data frame returned. @@ -148,9 +148,9 @@ In addition to importing directly from xlsx or xlsm files, `openxlsx2` provides Importing a file into a workbook looks like this: ```{r} # the file we are going to load -xlsx_file <- system.file("extdata", "openxlsx2_example.xlsx", package = "openxlsx2") +file <- system.file("extdata", "openxlsx2_example.xlsx", package = "openxlsx2") # loading the file into the workbook -wb <- wb_load(file = xlsx_file) +wb <- wb_load(file = file) ``` The additional options `wb_load()` provides are for internal use: `sheet` loads only a selected sheet from the workbook and `data_only` reads only the data parts from a workbook and ignores any additional graphics or pivot tables. Both functions create workbook objects that can only be used to read data, and we do not recommend end users to use them. Especially not if they intend to re-export the workbook afterwards.