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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

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.

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.60%. Comparing base (058dd4d) to head (5a52ad9).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6589      +/-   ##
==========================================
- Coverage   98.60%   98.60%   -0.01%     
==========================================
  Files          79       79              
  Lines       14516    14514       -2     
==========================================
- Hits        14314    14312       -2     
  Misses        202      202              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -104,6 +104,8 @@ if (getRversion() >= "4.0.0") {
# version of R (and that is checked in .onLoad with error if not).
export(.rbind.data.table) # only export in R<4.0.0 where it is still used; R-devel now detects it is missing doc, #5600
}
if (getRversion() >= "3.6.0") S3method(knitr::knit_print, data.table)
Copy link
Member

Choose a reason for hiding this comment

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

what happens if knitr is not installed?

IIUC this is an Enhances dependency; weve done something similar for {lintr}

https://github.com/r-lib/lintr/blob/1db7cc70655a41c77fcc89dbed5e40d5dd8cf74a/R/zzz.R#L337

Copy link
Contributor Author

Choose a reason for hiding this comment

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

data.table still loads with no warnings. R CMD check fails even with _R_CHECK_FORCE_SUGGESTS_=FALSE because it's the VignetteBuilder.

WRE says:

function gen.cls will get registered as an S3 method for class cls and generic gen from package pkg only when the namespace of pkg is loaded

Here's the code:

https://github.com/r-devel/r-svn/blob/320c6deb8bb07f9f83b3aa6cb3ca9b572c5d0b00/src/library/base/R/namespace.R#L1730-L1753

Copy link
Member

Choose a reason for hiding this comment

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

even with R_CHECK_FORCE_SUGGESTS=FALSE because it's the VignetteBuilder.

Right, we need the flags to skip vignettes too, e.g. --no-build-vignettes --no-vignettes

https://github.com/r-devel/r-svn/blob/e1cbaee23a2c12c8faa85f91aa1cc19fe709ca03/src/library/tools/R/check.R#L6851-L6852

Anyway, my concern was about loading {data.table}, thanks for the WRE cite!

I still think we should put {knitr} into Enhances:, WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On the one hand, WRE says

the Enhances field lists packages “enhanced” <...> by providing methods for classes from these packages

...which is a good argument for Enhances, but then it says

A package should be listed in only one of these fields.

The VignetteBuilder <...> may include the current package, or ones listed in Depends, Suggests or Imports.

If we switch to litedown as the vignette builder, knitr should definitely go into Enhances. Currently, it looks like knitr and markdown should be in both Suggests and VignetteBuilder, although the latter doesn't seem to be tested. (E.g. cmocean had VignetteBuilder: knitr for 5 years despite having %\VignetteEngine{knitr::rmarkdown} and not a single test flagged it.)

Although it's of course possible to read WRE differently, in which case knitr could move from Suggests to Enhances.

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