r-lib / pkgdown

Generate static html documentation for an R package
https://pkgdown.r-lib.org/
Other
721 stars 335 forks source link

Allow disabling the wrapping of `\dontrun{}` blocks with `if (FALSE) {}` in example sections #2752

Closed dvg-p4 closed 3 months ago

dvg-p4 commented 3 months ago

We use \dontrun{...} in all of our examples, which require database connections or otherwise should not be automatically run when building our package docs. In R or Rstudio, these unfortunately render as ## Not run: ... ## End(Not run), which at least renders in an easily-ignorable gray. However, pkgdown renders the tags as if (FALSE) {...}, which I think clutters up the page to very little benefit and looks kind of ugly. So, it would be nice if this could be turned off.

jayhesselberth commented 3 months ago

It's not clear to me what you're advocating for. Hiding the blocks completely? I'm guess not sure what you mean by "turned off".

dvg-p4 commented 3 months ago

Current: image

Proposed: image

hadley commented 3 months ago

I’d recommend using a different technique like @exampleIf

dvg-p4 commented 3 months ago

I’d recommend using a different technique like @exampleIf

@exampleIf FALSE actually removes the ## Not run: from the R/Rstudio docs, but still leaves the if (FALSE) in pkgdown

dvg-p4 commented 3 months ago

and @exampleIf 1 == 0 yields if (FALSE) { # 1 == 0

hadley commented 3 months ago

You should use a real condition that evaluates to TRUE when pkgdown is running.

dvg-p4 commented 3 months ago

What would that be?

hadley commented 3 months ago

Presumably you have some idea of when connect() will succeed. You might find https://r-pkgs.org/man.html#sec-man-examples-dependencies-conditional-execution helpful.

dvg-p4 commented 3 months ago

In short, it won't ever, because the functions in our class require a live connection to an actual database, for which we don't want to put credentials in our package examples.

dvg-p4 commented 3 months ago

I guess I'm confused as to why pkgdown wants to add this whole if (FALSE) in the first place. The point is to render examples on a webpage; the code isn't going to magically run itself (right?) -- and if someone manually copy-pastes the example code into an R terminal, the if (FALSE) won't stop them cause they can just not copy that part.

dvg-p4 commented 1 month ago

@hadley Am I misunderstanding something about how these example blocks work, is pkgdown actually evaluating the code when it generates the site?

hadley commented 1 month ago

Yes, pkgdown runs all the examples.