Skip to content

Commit

Permalink
Fix #376 (#377)
Browse files Browse the repository at this point in the history
* Fix #376

Did I forget anything?

* Update NEWS further [no ci]
  • Loading branch information
chainsawriot committed Sep 19, 2023
1 parent 9044169 commit e85f12d
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 10 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: rio
Type: Package
Title: A Swiss-Army Knife for Data I/O
Version: 1.0.0
Version: 1.0.1
Authors@R: c(person("Jason", "Becker", role = "aut", email = "jason@jbecker.co"),
person("Chung-hong", "Chan", role = c("aut", "cre"), email = "chainsawtiney@gmail.com",
comment = c(ORCID = "0000-0002-6232-7530")),
Expand Down Expand Up @@ -49,7 +49,6 @@ Imports:
curl (>= 0.6),
data.table (>= 1.11.2),
readxl (>= 0.1.1),
arrow (>= 0.17.0),
tibble,
stringi,
writexl,
Expand All @@ -72,7 +71,8 @@ Suggests:
rmatio,
xml2 (>= 1.2.0),
yaml,
qs
qs,
arrow (>= 0.17.0)
License: GPL-2
VignetteBuilder: knitr
Encoding: UTF-8
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# rio 1.0.1

* POTENTIALLY BREAKING: Due to compiling time concerns, roll back the decision to move `arrow` to `Imports`. It is now `Suggests`. `setclass = "arrow"` works if `arrow` is installed. #315 #376

# rio 1.0.0

* Stop loading the entire namespace of a suggested package when it is available #296
Expand Down
5 changes: 4 additions & 1 deletion R/import.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' @param setclass An optional character vector specifying one or more classes
#' to set on the import. By default, the return object is always a
#' \dQuote{data.frame}. Allowed values include \dQuote{tbl_df}, \dQuote{tbl}, or
#' \dQuote{tibble} (if using tibble), \dQuote{arrow}, \dQuote{arrow_table} (if using arrow table) or \dQuote{data.table} (if using
#' \dQuote{tibble} (if using tibble), \dQuote{arrow}, \dQuote{arrow_table} (if using arrow table; the suggested package `arrow` must be installed) or \dQuote{data.table} (if using
#' data.table). Other values are ignored, such that a data.frame is returned.
#' The parameter takes precedents over parameters in \dots which set a different class.
#' @param which This argument is used to control import from multi-object files; as a rule `import` only ever returns a single data frame (use [import_list()] to import multiple data frames from a multi-object file). If `file` is a compressed directory, `which` can be either a character string specifying a filename or an integer specifying which file (in locale sort order) to extract from the compressed directory. For Excel spreadsheets, this can be used to specify a sheet name or number. For .Rdata files, this can be an object name. For HTML files, it identifies which table to extract (from document order). Ignored otherwise. A character string value will be used as a regular expression, such that the extracted file is the first match of the regular expression against the file names in the archive.
Expand Down Expand Up @@ -102,6 +102,9 @@
#' @seealso [import_list()], [characterize()], [gather_attrs()], [export()], [convert()]
#' @export
import <- function(file, format, setclass = getOption("rio.import.class", "data.frame"), which, ...) {
if (setclass %in% c("arrow", "arrow_table")) {
.check_pkg_availability("arrow")
}
.check_file(file, single_only = TRUE)
if (R.utils::isUrl(file)) {
file <- remote_to_local(file, format = format)
Expand Down
2 changes: 2 additions & 0 deletions R/import_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ import_delim <- function(file, which = 1, sep = "auto", header = "auto", strings

#' @export
.import.rio_feather <- function(file, which = 1, ...) {
.check_pkg_availability("arrow")
.docall(arrow::read_feather, ..., args = list(file = file))
}

Expand Down Expand Up @@ -386,6 +387,7 @@ extract_html_row <- function(x, empty_value) {

#' @export
.import.rio_parquet <- function(file, which = 1, ...) {
.check_pkg_availability("arrow")
.docall(arrow::read_parquet, ..., args = list(file = file, as_data_frame = TRUE))
}

Expand Down
2 changes: 2 additions & 0 deletions R/set_class.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ set_class <- function(x, class = NULL) {
}

if (any(c("arrow", "arrow_table") %in% class)) {
## because setclass can be used without import, must check again
.check_pkg_availability("arrow")
return(.ensure_arrow(x))
}
return(.ensure_data_frame(x))
Expand Down
Binary file modified R/sysdata.rda
Binary file not shown.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,13 @@ The full list of supported formats is below:
| Archive files (handled by tar) | bzip2 / xz / tar | utils | utils | Default | |
| Gzip files | gz / gzip | base | base | Default | |
| Zip files | zip | utils | utils | Default | |
| Apache Arrow (Parquet) | parquet | arrow | arrow | Default | |
| CSVY (CSV + YAML metadata header) | csvy | data.table | data.table | Default | |
| Comma-separated data | csv | data.table | data.table | Default | |
| Comma-separated data (European) | csv2 | data.table | data.table | Default | |
| Data Interchange Format | dif | utils | | Default | |
| Epiinfo | epiinfo / rec | foreign | | Default | |
| Excel | excel / xlsx | readxl | writexl | Default | |
| Excel (Legacy) | xls | readxl | | Default | |
| Feather R/Python interchange format | feather | arrow | arrow | Default | |
| Fixed-width format data | fwf | utils | utils | Default | |
| Fortran data | fortran | utils | | Default | No recognized extension |
| Google Sheets | googlesheets | data.table | | Default | As comma-separated data |
Expand All @@ -163,9 +161,11 @@ The full list of supported formats is below:
| Text Representations of R Objects | dump | base | base | Default | |
| Weka Attribute-Relation File Format | arff / weka | foreign | foreign | Default | |
| XBASE database files | dbf | foreign | foreign | Default | |
| Apache Arrow (Parquet) | parquet | arrow | arrow | Suggest | |
| Clipboard | clipboard | clipr | clipr | Suggest | default is tsv |
| EViews | eviews / wf1 | hexView | | Suggest | |
| Fast Storage | fst | fst | fst | Suggest | |
| Feather R/Python interchange format | feather | arrow | arrow | Suggest | |
| Graphpad Prism | pzfx | pzfx | pzfx | Suggest | |
| HTML Tables | htm / html | xml2 | xml2 | Suggest | |
| JSON | json | jsonlite | jsonlite | Suggest | |
Expand Down
4 changes: 2 additions & 2 deletions data-raw/single.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"input": "parquet",
"format": "parquet",
"type": "import",
"type": "suggest",
"format_name": "Apache Arrow (Parquet)",
"import_function": "arrow::read_parquet",
"export_function": "arrow::write_parquet",
Expand Down Expand Up @@ -209,7 +209,7 @@
{
"input": "feather",
"format": "feather",
"type": "import",
"type": "suggest",
"format_name": "Feather R/Python interchange format",
"import_function": "arrow::read_feather",
"export_function": "arrow::write_feather",
Expand Down
2 changes: 1 addition & 1 deletion man/import.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/import_list.Rd

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

3 changes: 3 additions & 0 deletions tests/testthat/test_format_feather.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
context("feather imports/exports")
skip_if_not_installed("arrow")
require("datasets")

test_that("Export to feather", {
skip_if_not_installed("arrow")
expect_true(export(iris, "iris.feather") %in% dir())
})

test_that("Import from feather", {
skip_if_not_installed("arrow")
expect_true(is.data.frame(import("iris.feather")))
})

Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test_format_parquet.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ context("Parquet imports/exports")
require("datasets")

test_that("Export to and import from parquet", {
skip_if_not_installed("arrow")
expect_true(export(iris, "iris.parquet") %in% dir())
expect_true(is.data.frame(import("iris.parquet")))
unlink("iris.parquet")
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test_identical.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ unlink("mtcars.rds")
unlink("mtcars.R")
unlink("mtcars.RData")
test_that("Data identical (R formats), feather", {
skip_if_not_installed("arrow")
expect_equivalent(import(export(mtcars, "mtcars.feather")), mtcars)
unlink("mtcars.feather")
})
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test_import_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ test_that("Universal dummy `which` #326", {

test_that("Universal dummy `which` (Suggests) #326", {
skip_if_not_installed("qs")
skip_if_not_installed("arrow")
skip_if_not_installed("readODS")
formats <- c("qs", "parquet", "ods")
for (format in formats) {
tempzip <- tempfile(fileext = paste0(".", format, ".zip"))
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test_set_class.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ test_that("Set object class as data.table", {

test_that("Set object class as arrow table", {
skip_if(getRversion() <= "4.2")
skip_if_not_installed("arrow")
mtcars_arrow <- arrow::arrow_table(mtcars)
expect_false(inherits(set_class(mtcars_arrow), "data.frame")) ## arrow table is not data.frame
expect_true(inherits(set_class(mtcars, class = "arrow"), "ArrowTabular"))
Expand All @@ -39,8 +40,19 @@ test_that("Set object class as arrow table", {

test_that("ArrowTabular can be exported", {
skip_if(getRversion() <= "4.2")
skip_if_not_installed("arrow")
mtcars_arrow <- arrow::arrow_table(mtcars)
expect_error(export(mtcars_arrow, "mtcars.csv"), NA) ## no concept of rownames
expect_true(inherits(import("mtcars.csv"), "data.frame"))
unlink("mtcars.csv")
})

test_that("Simulate arrow is not installed, #376", {
## although this is pretty meaningless
with_mocked_bindings({
export(mtcars, "mtcars.csv")
expect_error(import("mtcars.csv", setclass = "arrow"), "Suggested package")
}, .check_pkg_availability = function(pkg, lib.loc = NULL) {
stop("Suggested package `", pkg, "` is not available. Please install it individually or use `install_formats()`", call. = FALSE)
})
})

0 comments on commit e85f12d

Please sign in to comment.