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

Solve the knitr auto-printing problem by registering a method for knit_print #6589

Merged
merged 12 commits into from
Dec 5, 2024

Conversation

aitap
Copy link
Contributor

@aitap aitap commented Oct 21, 2024

It turns out that there is a better solution for #6566 after all. It's documented in vignette('knit_print') that packages can override knitr'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 keeping knitr in Suggests:. In R ∈ [3.3, 3.6), the same thing can be done manually: I've reused a piece of code from base::registerS3methods to register knit_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 returns invisible(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.

aitap added 2 commits October 21, 2024 17:46
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.
Copy link

codecov bot commented Oct 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.61%. Comparing base (98cf24e) to head (8b692a7).
Report is 1 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

NAMESPACE Outdated Show resolved Hide resolved
NAMESPACE Outdated Show resolved Hide resolved
@MichaelChirico
Copy link
Member

LGTM, thanks!

Copy link

github-actions bot commented Dec 3, 2024

Comparison Plot

Generated via commit 8b692a7

Download link for the artifact containing the test results: ↓ atime-results.zip

Task Duration
R setup and installing dependencies 4 minutes and 35 seconds
Installing different package versions 7 minutes and 33 seconds
Running and plotting the test cases 2 minutes and 15 seconds

@MichaelChirico MichaelChirico added this to the 1.17.0 milestone Dec 3, 2024
R/onLoad.R Outdated Show resolved Hide resolved
Copy link
Member

@MichaelChirico MichaelChirico left a 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

@aitap
Copy link
Contributor Author

aitap commented Dec 5, 2024

Good $localtime @MichaelChirico, the NEWS entry is now done too. Looks like were making commits at the same time.

Copy link
Member

@MichaelChirico MichaelChirico left a 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 🇸🇬 😄)

@MichaelChirico MichaelChirico merged commit a94401a into master Dec 5, 2024
11 checks passed
@MichaelChirico MichaelChirico deleted the knit_print branch December 5, 2024 07:33
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.

medium- and long-term plans for tests/knitr.R
2 participants