[R] remove unused imports in tests #8614
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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):What's wrong with
require()
?Despite its name,
require()
does not actually fail if the library you ask it to load isn't installed.require(pkg)
is more expensive thanrequireNamespace(pkg)
because it both loads and attachespkg
, while also being less strict thanlibrary(pkg)
because it warns but does not raise an exception ifpkg
is not installed.For this reasons, either
requireNamespace()
orlibrary()
is almost always preferable torequire()
. 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.Then removed suggested packages
{vcd}
and{titanic}
and ran the tests.The resulted in the following results
And a summary explaining the reason for tests being skipped.
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.
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.