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

[R] remove unused imports in tests #8614

Merged
merged 2 commits into from
Dec 24, 2022
Merged

Conversation

jameslamb
Copy link
Contributor

This PR proposes removing all uses of require() in the R package's tests.

Most of those are unnecessary. For example, when using {testthat} it isn't necessary to load {testthat} or the library being tested in test files.

Benefit of this change

Simplifies the tests by removing unnecessary code.

Moves {xgboost} closer to the standard for managing optional dependencies prescribed in Writing R Extensions. From "Suggested Packages" in that doc (link):

... someone wanting to run the ... tests ... may not have a suggested package available... using if(requireNamespace("pkgname")) is preferred, if possible.

What's wrong with require()?

Despite its name, require() does not actually fail if the library you ask it to load isn't installed.

Rscript -e "require('does.not.exist')"
echo $?
# 0

require(pkg) is more expensive than requireNamespace(pkg) because it both loads and attaches pkg, while also being less strict than library(pkg) because it warns but does not raise an exception if pkg is not installed.

For this reasons, either requireNamespace() or library() is almost always preferable to require(). See ?requireNamespace or https://r-pkgs.org/dependencies-mindset-background.html#sec-dependencies-namespace for more discussion.

How I tested this

Installed {xgboost} on this branch.

R CMD INSTALL --with-keep.source R-package/

Then removed suggested packages {vcd} and {titanic} and ran the tests.

Rscript -e "remove.packages(c('vcd', 'titanic'))"
cd R-package/tests
Rscript testthat.R

The resulted in the following results

[ FAIL 0 | WARN 2 | SKIP 21 | PASS 1010 ]

And a summary explaining the reason for tests being skipped.

── Skipped tests  ──────────────────────────────────────
• Optional testing dependency 'titanic' not found. (1)
• Optional testing dependency 'vcd' not found. (20)

Next, I installed those two packages and ran the tests again.

Rscript -e "install.packages(c('titanic', 'vcd'), repos = 'https://cran.r-project.org')"
Rscript testthat.R

And saw all the tests succeed + around 300 more run.

[ FAIL 0 | WARN 2 | SKIP 1 | PASS 1326 ]

Notes for Reviewers

Thanks for your time and consideration! Please let me know if you'd prefer that I slow down these small maintenance contributions on the R package, or batch them up into larger PRs.

Copy link
Member

@trivialfis trivialfis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for cleaning up the imports and the educational description! The PR looks good to me.

@trivialfis trivialfis merged commit f489d82 into dmlc:master Dec 24, 2022
@jameslamb jameslamb deleted the r/test-imports branch December 24, 2022 19:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants