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

Fix #389 #390

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel-1'}
- {os: windows-latest, r: '3.6'}
- {os: ubuntu-latest, r: '3.6'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ Suggests:
License: GPL-2
VignetteBuilder: knitr
Encoding: UTF-8
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE)
Config/Needs/website: gesistsa/tsatemplate
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* For missing files in `import_list` it gives more informative warnings fix #389
* Single-item list of data frames can be exported fix #385
* Move `stringi` to Suggests to reduce compilation time. Add an attribution to the internal data to list out all required packages #378
* Move `readr` to Imports for `fwf`. `readr` is a dependency of `haven` so it does not increase the number of dependencies. Remove the original `read.fwf2` which doesn't guess `widths`. Keep the `widths` and `col.names` to maintain compatibility. #381
Expand Down
61 changes: 27 additions & 34 deletions R/import_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#' @param \dots Additional arguments passed to [import()]. Behavior may be unexpected if files are of different formats.
#' @inheritParams import
#' @return If `rbind=FALSE` (the default), a list of a data frames. Otherwise, that list is passed to [data.table::rbindlist()] with `fill = TRUE` and returns a data frame object of class set by the `setclass` argument; if this operation fails, the list is returned.
#' @details When file is a vector of file paths and any files are missing, those files are ignored (with warnings) and this function will not raise any error.
#' @examples
#' ## For demo, a temp. file path is created with the file extension .xlsx
#' xlsx_file <- tempfile(fileext = ".xlsx")
Expand All @@ -25,46 +26,37 @@
#' # import all worksheets, the return value is a list
#' import_list(xlsx_file)
#'
#' # import and rbind all worksheets, the return valye is a data frame
#' # import and rbind all worksheets, the return value is a data frame
#' import_list(xlsx_file, rbind = TRUE)
#' @seealso [import()], [export_list()], [export()]
#' @export
import_list <-
function(file,
setclass = getOption("rio.import.class", "data.frame"),
which,
rbind = FALSE,
rbind_label = "_file",
rbind_fill = TRUE,
...) {
.check_file(file, single_only = FALSE)

## special cases
if (length(file) == 1) {
x <- .read_file_as_list(file = file, which = which, setclass = setclass, rbind = rbind, rbind_label = rbind_label, ...)
import_list <- function(file, setclass = getOption("rio.import.class", "data.frame"), which, rbind = FALSE,
rbind_label = "_file", rbind_fill = TRUE, ...) {
.check_file(file, single_only = FALSE)
## special cases
if (length(file) == 1) {
x <- .read_file_as_list(file = file, which = which, setclass = setclass, rbind = rbind, rbind_label = rbind_label, ...)
} else {
## note the plural
x <- .read_multiple_files_as_list(files = file, setclass = setclass, rbind = rbind, rbind_label = rbind_label, ...)
}
## optionally rbind
if (isTRUE(rbind)) {
if (length(x) == 1) {
x <- x[[1L]]
} else {
## note the plural
x <- .read_multiple_files_as_list(files = file, setclass = setclass, rbind = rbind, rbind_label = rbind_label, ...)
}
# optionally rbind
if (isTRUE(rbind)) {
if (length(x) == 1) {
x <- x[[1L]]
x2 <- try(data.table::rbindlist(x, fill = rbind_fill), silent = TRUE)
if (inherits(x2, "try-error")) {
warning("Attempt to rbindlist() the data did not succeed. List returned instead.", call. = FALSE)
return(x)
} else {
x2 <- try(data.table::rbindlist(x, fill = rbind_fill), silent = TRUE)
if (inherits(x2, "try-error")) {
warning("Attempt to rbindlist() the data did not succeed. List returned instead.")
return(x)
} else {
x <- x2
}
x <- x2
}
## set class
x <- set_class(x, class = setclass)
}

return(x)
x <- set_class(x, class = setclass)
}
return(x)
}

.strip_exts <- function(file) {
vapply(file, function(x) tools::file_path_sans_ext(basename(x)), character(1))
Expand All @@ -75,8 +67,9 @@ import_list <-
x <- lapply(files, function(thisfile) {
out <- try(import(thisfile, setclass = setclass, ...), silent = TRUE)
if (inherits(out, "try-error")) {
warning(sprintf("Import failed for %s", thisfile))
out <- NULL
warning(sprintf("Import failed for %s", thisfile), call. = FALSE)
##out <- NULL
return(NULL)
} else if (isTRUE(rbind)) {
out[[rbind_label]] <- thisfile
}
Expand Down
3 changes: 3 additions & 0 deletions R/set_class.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ set_class <- function(x, class = NULL) {

.ensure_data_frame <- function(x) {
out <- structure(x, class = "data.frame")
if (nrow(out) == 0) {
return(out)
}
if (!length(rownames(out))) {
rownames(out) <- as.character(seq_len(length(out[, 1L, drop = TRUE])))
}
Expand Down
5 changes: 4 additions & 1 deletion man/import_list.Rd

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

2 changes: 1 addition & 1 deletion man/rio.Rd

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

10 changes: 10 additions & 0 deletions tests/testthat/test_import_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ test_that("Universal dummy `which` (Suggests) #326", {
}
})

test_that("Informative message when files are not found #389", {
expect_warning(import_list(c("mtcars.rds", "nonexisting.rds")), "^Import failed for nonexisting")
})

test_that("Missing files and rbind", {
expect_warning(x <- import_list(c("mtcars.rds", "nonexisting.rds"), rbind = TRUE), "^Import failed for nonexisting")
expect_warning(x <- import_list(c("nonexisting.rds", "nonexisting2.rds"), rbind = TRUE), "^Import failed for nonexisting")
expect_true(is.data.frame(x))
})

unlink("data.rdata")
unlink("mtcars.rds")
unlink("mtcars.csv.zip")
Loading