-
Notifications
You must be signed in to change notification settings - Fork 988
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
Solve the knitr
auto-printing problem by registering a method for knit_print
#6589
Conversation
Implementing a method for the knitr::knit_print generic makes it possible to customise the behaviour without looking up the call stack. The current solution only works on R >= 3.6.0 because that's where delayed S3 registration has been introduced.
Use setHook() to ensure that registerS3method() will be called in the same session if 'knitr' is loaded later. Not needed on R >= 3.6.0 where S3method(knitr::knit_print) will do the right thing by itself.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #6589 +/- ##
==========================================
- Coverage 98.61% 98.61% -0.01%
==========================================
Files 79 79
Lines 14535 14533 -2
==========================================
- Hits 14334 14332 -2
Misses 201 201 ☔ View full report in Codecov by Sentry. |
LGTM, thanks! |
Generated via commit 8b692a7 Download link for the artifact containing the test results: ↓ atime-results.zip
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW please add a NEWS entry
Avoid breaking it again like in #6589
Co-authored-by: Michael Chirico <[email protected]>
Good $localtime @MichaelChirico, the NEWS entry is now done too. Looks like were making commits at the same time. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent, thanks!
(greetings from 🇸🇬 😄)
It turns out that there is a better solution for #6566 after all. It's documented in
vignette('knit_print')
that packages can overrideknitr
's (auto-)printing behaviour by registering a method for it. R ≥ 3.6 can perform "delayed registration" of S3 methods for a generic in a package without requiring it to be loaded, hence keepingknitr
inSuggests:
. In R ∈ [3.3, 3.6), the same thing can be done manually: I've reused a piece of code frombase::registerS3methods
to registerknit_print.data.table
if the package is already loaded and set a hook to register it later if needed.The method itself just checks
shouldPrint(x)
and returnsinvisible(x)
if not, otherwise delegates to the default method.This can close #6566 if we don't want to change the behaviour of
.Primitive("[")
with regards to visibility.