Skip to content

Pre-run examples in website manual - #7955

Open
aitap wants to merge 1 commit into
masterfrom
site-manual-examples
Open

aitap wants to merge 1 commit into
masterfrom
site-manual-examples

Conversation

@aitap

@aitap aitap commented Nov 9, 2026

Copy link
Copy Markdown
Member

Ask tools::Rd2HTML to export the example code in special chunks, parse them and let litedown process them. example(data.table) leaves a new.pdf in the current directory because file.remove("new.pdf") is marked in \dontshow{}, which is, indeed, not shown in the HTML page.

The tables don't look great. We could add some styles (increase margins around each <td>?) or disable HTML tables by overriding litedown:::record_print.data.frame for the duration of the script.

Fixes: #7746

@aitap
aitap requested a review from MichaelChirico as a code owner November 9, 2026 07:10
Comment thread site/generate_reference.R
# The <!--begin.rcode ... end.rcode--> blocks are unsafe to parse as HTML, as an --> inside R code would terminate the comment.
env = new.env()
html_text = readLines(temp_html)
begin.rcode = which(startsWith(html_text, "<!--begin.rcode"))

@MichaelChirico MichaelChirico Nov 9, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

FWIW I don't see this documented in ?Rd2HTML -- should we add a defensive check here to alert us if some internals change upstream that break our assumption on how to process the result?

The current stopifnot() might suffice... anything more we can assert?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

These "rcode" blocks follow the knitr convention for *.Rhtml documents, see the source of the example R HTML vignette. The documentation is at https://yihui.org/knitr/patterns/, but the patterns themselves live in code:

> knitr::all_patterns$html
$chunk.begin
[1] "^\\s*<!--\\s*begin.rcode\\s*(.*)"

$chunk.end
[1] "^\\s*end.rcode\\s*-->"

$ref.chunk
[1] "^\\s*<<(.+)>>\\s*$"

$inline.code
[1] "<!--\\s*rinline(.+?)-->"

$header.begin
[1] "\\s*<head>"

I can adjust our script to use the exact patterns from knitr.

@MichaelChirico

Copy link
Copy Markdown
Member

The tables don't look great.

Any png to share? Or can I preview it somehow?

@aitap

aitap commented Nov 9, 2026

Copy link
Copy Markdown
Member Author

This is what an HTML table looks without any CSS applied:
image

<table>
<thead><tr>
<th>x</th>
<th align="right">y</th>
<th align="right">v</th>
</tr></thead>
<tbody>
<tr>
<td>b</td>
<td align="right">1</td>
<td align="right">1</td>
</tr>

To preview, run Rscript site/generate_reference.R and open a file in site/reference/*.html. After running generate_reference.R, it's also possible to run setwd("site"); litedown::fuse_site('.') and navigate to site/manual.html.

Comment thread site/generate_reference.R

library(tools)
library(xml2)
library(data.table)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

any reason we use library?
we don't use any dt code, Rd2HTML refers to data.table, stopifnot(requireNamespace()) should be enough

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

it seems library() is better regardless for simplicity sake, no?

This branch has not been deployed

No deployments
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.

litedown-rendered man pages could have pre-run examples

3 participants