Solve the knitr auto-printing problem by registering a method for knit_print#6589
Solve the knitr auto-printing problem by registering a method for knit_print#6589MichaelChirico merged 12 commits intomasterfrom
knitr auto-printing problem by registering a method for knit_print#6589Conversation
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
|
MichaelChirico
left a comment
There was a problem hiding this comment.
BTW please add a NEWS entry
Avoid breaking it again like in #6589
Co-authored-by: Michael Chirico <chiricom@google.com>
|
Good $localtime @MichaelChirico, the NEWS entry is now done too. Looks like were making commits at the same time. |
MichaelChirico
left a comment
There was a problem hiding this comment.
Excellent, thanks!
(greetings from 🇸🇬 😄)
|
Something happened in |

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 keepingknitrinSuggests:. In R ∈ [3.3, 3.6), the same thing can be done manually: I've reused a piece of code frombase::registerS3methodsto registerknit_print.data.tableif 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.