Skip to content

Commit

Permalink
Fix #154
Browse files Browse the repository at this point in the history
  • Loading branch information
chainsawriot committed Dec 29, 2023
1 parent 2a703c9 commit c09c50b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 32 deletions.
69 changes: 40 additions & 29 deletions R/read_ods.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,30 @@
return(g)
}



## Based on readxl, although the implementation is different.
## If max row is -1, read to end of row.
## Row and column-numbers are 1-based
.standardise_limits <- function(range, skip) {
.standardise_limits <- function(range, skip, n_max) {
if(is.null(range)) {
skip <- check_nonnegative_integer(x = skip, argument = "skip")
n_max <- check_nonnegative_integer(x = n_max, argument = "n_max")
if (n_max == Inf) {
max_row <- -1
} else {
max_row <- n_max + 1
}
limits <- c(
min_row = skip + 1,
max_row = -1,
max_row = max_row,
min_col = 1,
max_col = -1
)
} else {
if(skip != 0) {
warning("Range and non-zero value for skip given. Defaulting to range.", call. = FALSE)
if(skip != 0 || n_max != Inf) {
warning("Range and non-default value for skip or n_max given. Defaulting to range.", call. = FALSE)
}
tryCatch({
limits <- cellranger::as.cell_limits(range)
limits <- cellranger::as.cell_limits(range)
}, error = function(e) {
stop("Invalid `range`")
})
Expand Down Expand Up @@ -127,22 +131,22 @@
return(data.frame())
}

.type_convert <- function(df, col_types = NULL, verbose = TRUE, na = c("", "NA")) {
.type_convert <- function(df, col_types = NULL, verbose = TRUE, na = c("", "NA"), trim_ws = TRUE) {
if (verbose) {
res <- readr::type_convert(df = df, col_types, na = na)
} else {
suppressMessages({
res <- readr::type_convert(df = df, col_types, na = na)
res <- readr::type_convert(df = df, col_types, na = na, trim_ws = trim_ws)
})
}
return(res)
}

.handle_col_types <- function(res, col_types, verbose, na) {
.handle_col_types <- function(res, col_types, verbose, na, trim_ws) {
if (isTRUE(is.na(col_types)) || nrow(res) == 0) {
return(res)
}
.type_convert(df = res, col_types = col_types, verbose = verbose, na = na)
.type_convert(df = res, col_types = col_types, verbose = verbose, na = na, trim_ws = trim_ws)
}

## standardise `sheet` parameter as a number, i.e. sheet_index
Expand Down Expand Up @@ -173,19 +177,21 @@
}

.read_ods <- function(path,
sheet = 1,
col_names = TRUE,
col_types = NULL,
na = "",
skip = 0,
formula_as_formula = FALSE,
range = NULL,
row_names = FALSE,
strings_as_factors = FALSE,
verbose = FALSE,
as_tibble = TRUE,
.name_repair = "unique",
flat = FALSE) {
sheet = 1,
col_names = TRUE,
col_types = NULL,
na = "",
skip = 0,
formula_as_formula = FALSE,
range = NULL,
row_names = FALSE,
strings_as_factors = FALSE,
verbose = FALSE,
as_tibble = TRUE,
.name_repair = "unique",
flat = FALSE,
trim_ws = TRUE,
n_max = Inf) {
.check_read_args(path,
sheet,
col_names,
Expand All @@ -207,7 +213,7 @@
.read_ods_func <- read_ods_
}
## Get cell range info
limits <- .standardise_limits(range, skip)
limits <- .standardise_limits(range, skip, n_max)
sheet_index <- .standardise_sheet(sheet = sheet, sheet_names = .get_sheet_names_func(file = path, include_external_data = TRUE),
range = range)
strings <- .read_ods_func(file = path,
Expand All @@ -233,7 +239,7 @@
byrow = TRUE),
stringsAsFactors = FALSE)
res <- .change_df_with_col_row_header(x = res, col_header = col_names, row_header = row_names, .name_repair = .name_repair)
res <- .handle_col_types(res, col_types = col_types, verbose = verbose, na = na)
res <- .handle_col_types(res, col_types = col_types, verbose = verbose, na = na, trim_ws = trim_ws)
if (strings_as_factors) {
res <- .convert_strings_to_factors(df = res)
}
Expand Down Expand Up @@ -300,9 +306,10 @@
#' Default is `"unique"`.
#'
#' @param ods_format character, must be "auto", "ods" or "fods". The default "auto" is to determine the format automatically. By default, the format is determined by file extension, unless `guess` is `FALSE`.
#' @param guess logical. If the file extension is absent or not recognized, this
#' @param guess logical, If the file extension is absent or not recognized, this
#' controls whether we attempt to guess format based on the file signature or
#' "magic number".
#' @param trim_ws logical, should leading and trailing whitespace be trimmed?
#' @return A tibble (\code{tibble}) or data frame (\code{data.frame}) containing a representation of data in the (f)ods file.
#' @author Peter Brohan <peter.brohan+cran@@gmail.com>, Chung-hong Chan <chainsawtiney@@gmail.com>, Gerrit-Jan Schutten <phonixor@@gmail.com>
#' @examples
Expand Down Expand Up @@ -343,7 +350,9 @@ read_ods <- function(path,
as_tibble = TRUE,
.name_repair = "unique",
ods_format = c("auto", "ods", "fods"),
guess = FALSE) {
guess = FALSE,
trim_ws = TRUE,
n_max = Inf) {
ods_format <- .determine_ods_format(path = path, guess = guess, ods_format = match.arg(ods_format))
## Should use match.call but there's a weird bug if one of the variable names is 'file'
.read_ods(path = path,
Expand All @@ -359,7 +368,9 @@ read_ods <- function(path,
verbose = verbose,
as_tibble = as_tibble,
.name_repair = .name_repair,
flat = ods_format == "fods")
flat = ods_format == "fods",
trim_ws = trim_ws,
n_max = n_max)
}

#' @rdname read_ods
Expand Down
2 changes: 1 addition & 1 deletion man/list_ods_sheets.Rd

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

8 changes: 6 additions & 2 deletions man/read_ods.Rd

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

0 comments on commit c09c50b

Please sign in to comment.