Skip to content

Commit

Permalink
Fix #453 (#455)
Browse files Browse the repository at this point in the history
* Update onLoad (WIP)

* Fix #453

* Rename

* Add tests

the second one is a mocking test

* More tests

* Update README

c
  • Loading branch information
chainsawriot authored Sep 24, 2024
1 parent 397ded5 commit 6d73e93
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 18 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# rio 1.2.3

* Fix #453, don't nudge the user to install all suggested packages

# rio 1.2.2

* Fix #447 - remove an ancient artefact of Vignette generation, h/t Tim Taylor for the help.
Expand Down
4 changes: 2 additions & 2 deletions R/onLoad.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
if (interactive()) {
w <- uninstalled_formats()
if (length(w)) {
msg <- "The following rio suggested packages are not installed: %s\nUse 'install_formats()' to install them"
packageStartupMessage(sprintf(msg, toString(sQuote(w))))
msg <- "Some optional R packages were not installed and therefore some file formats are not supported. Check file support with show_unsupported_formats()"
packageStartupMessage(msg)
}
}
}
23 changes: 21 additions & 2 deletions R/suggestions.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' @title Install rio's \sQuote{Suggests} Dependencies
#' @description This function installs various \sQuote{Suggests} dependencies for rio that expand its support to the full range of support import and export formats. These packages are not installed or loaded by default in order to create a slimmer and faster package build, install, and load.
#' @description Not all suggested packages are installed by default. These packages are not installed or loaded by default in order to create a slimmer and faster package build, install, and load. Use `show_unsupported_formats()` to check all unsupported formats. `install_formats()` installs all missing \sQuote{Suggests} dependencies for rio that expand its support to the full range of support import and export formats.
#' @param \dots Additional arguments passed to [utils::install.packages()].
#' @return `NULL`
#' @return For `show_unsupported_formats()`, if there is any missing unsupported formats, it return TRUE invisibly; otherwise FALSE. For `install_formats()` it returns TRUE invisibly if the installation is succuessful; otherwise errors.
#' @examples
#' \donttest{
#' if (interactive()) {
Expand All @@ -20,3 +20,22 @@ uninstalled_formats <- function() {
## which are not installed
suggested_packages[!R.utils::isPackageInstalled(suggested_packages)]
}

#' @rdname install_formats
show_unsupported_formats <- function() {
## default_formats <- sort(unique(rio_formats$format[rio_formats$type == "import"]))
suggested_formats <- rio_formats[rio_formats$type == "suggest",]
suggested_formats$pkg <- vapply(strsplit(suggested_formats$import_function, "::"), FUN = `[`, FUN.VALUE = character(1), 1)
missing_pkgs <- uninstalled_formats()
suggested_formats$installed <- vapply(suggested_formats$pkg, function(x) x %in% missing_pkgs, logical(1), USE.NAMES = FALSE)
unsupported_formats <- suggested_formats[suggested_formats$installed,]
if (nrow(unsupported_formats) == 0) {
message("All default and optional formats are supported.")
return(invisible(FALSE))
}
temp_display <- unsupported_formats[,c("input", "format", "pkg")]
colnames(temp_display)[3] <- "Suggested package"
print(temp_display)
message("These formats are not supported. If you need to use these formats, please either install the suggested packages individually, or install_formats() to install them all.")
return(invisible(TRUE))
}
8 changes: 7 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,13 @@ export(list(mtcars = mtcars, iris = iris), file = "mtcars.xlsx")

## Supported file formats

**rio** supports a wide range of file formats. To keep the package slim, several formats are supported via "Suggests" packages, which are not installed (or loaded) by default. To ensure rio is fully functional, install these packages the first time you use **rio** via:
**rio** supports a wide range of file formats. To keep the package slim, several formats are supported via "Suggests" packages, which are not installed (or loaded) by default. You can check which formats are **not** supported via:

```R
show_unsupported_formats()
```

You can install the suggested packages individually, depending your own needs. If you want to install all suggested packages:

```R
install_formats()
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,15 @@ export(list(mtcars = mtcars, iris = iris), file = "mtcars.xlsx")

**rio** supports a wide range of file formats. To keep the package slim,
several formats are supported via “Suggests” packages, which are not
installed (or loaded) by default. To ensure rio is fully functional,
install these packages the first time you use **rio** via:
installed (or loaded) by default. You can check which formats are
**not** supported via:

``` r
show_unsupported_formats()
```

You can install the suggested packages individually, depending your own
needs. If you want to install all suggested packages:

``` r
install_formats()
Expand Down
7 changes: 5 additions & 2 deletions man/install_formats.Rd

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

9 changes: 0 additions & 9 deletions tests/testthat/test_install_formats.R

This file was deleted.

30 changes: 30 additions & 0 deletions tests/testthat/test_suggestions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
test_that("uninstalled_formats()", {
skip_on_cran()
formats <- uninstalled_formats()
if (is.null(formats)) {
expect_true(install_formats())
} else {
expect_type(formats, "character")
}
})


test_that("show_unsupported_formats (in the fully supported environment) on CI", {
skip_on_cran()
for (pkg in attr(rio_formats, "suggested_packages")) {
skip_if_not_installed(pkg)
}
expect_false(show_unsupported_formats())
expect_message(show_unsupported_formats(), "All default")
})

test_that("show_unsupported_formats (in the partial supported environment) on CI", {
skip_on_cran()
fake_uninstalled_formats <- function() {
return(c("readODS"))
}
with_mocked_bindings(code = {
expect_true(show_unsupported_formats())
expect_message(show_unsupported_formats(), "These formats are not supported")
}, `uninstalled_formats` = fake_uninstalled_formats)
})

0 comments on commit 6d73e93

Please sign in to comment.