yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.37k stars 874 forks source link

Using `fig.cur` in rgl (or another htmlwidget)? #2241

Closed dmurdoch closed 1 year ago

dmurdoch commented 1 year ago

In rgl I'd like to include alt text in the displays. For regular plots, the alt text is found using the options$fig.cur value to index into options$fig.alt.

This code determines the fig.cur value and uses it to index into fig.alt:

https://github.com/yihui/knitr/blob/c50d30728857db278954e5a59a35a188b52934ce/R/output.R#L495-L499

where reduce_plot_opts does the indexing, changing fig.alt and some other options to scalars.

The code is run very late in the rendering process, because not all lines of code that generate plots result in having them shown in the final document: low level changes (like adding annotations to a base plot) suppress the display of the previous plot. By the time this is run for rgl widgets, the data in the widget is fixed, so there's no opportunity for rgl code to see the appropriate alt text.

Would it be possible to include the fig.cur value in some attribute of the div that wraps the widget? Then I could save the whole fig.alt vector in the rgl data, and index into it at rendering time.

Another approach to solve this would be to dispatch to the next method after knit_asis instead of hard-coding the knit_asis_htmlwidget handling.

dmurdoch commented 1 year ago

This seems related to #2169, but I don't think fig.id has any effect when displaying an htmlwidget.

dmurdoch commented 1 year ago

I have a better suggested change: the add_html_caption function should insert the alt text if it detects that the widget needs it. I will submit a PR for this soon.

dmurdoch commented 1 year ago

Thanks! I've tested this, and it's now working fine in the devel version of rgl.

yihui commented 1 year ago

Great! Thanks for testing! I just submitted the dev version to CRAN. Hopefully it will be on CRAN in a couple of days.

github-actions[bot] commented 7 months ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.