rstudio / crosstalk

Inter-htmlwidget communication for R (with and without Shiny)
http://rstudio.github.io/crosstalk
Other
288 stars 52 forks source link

add "htmlwidget" class to `bscols()` object #143

Open ThierryO opened 12 months ago

ThierryO commented 12 months ago

Adding this class makes bookdown recognise it as a htmlwidget and handles it as a figure. See rstudio/bookdown#1443

cderv commented 12 months ago

Thanks !

To be more precise on this, it is not bookdown directly but rather knitr.

fig.cap can be used on htmlwidgets, and it will be added by knitr for HTML format as a figure caption (like a usual plot). This happens in https://github.com/yihui/knitr/blob/8e0341c2591e6f7bcabb8e29bd4151473006d036/R/output.R#L502-L513 when knit_asis_htmlwidget is set, which happens when knitr detects this is a htmlwidgets output when doing the knit_print (https://github.com/yihui/knitr/blob/8e0341c2591e6f7bcabb8e29bd4151473006d036/R/utils.R#L858-L863)

So we can try in knitr to detect inside the browsable() object to find the htmlwidgets, or we can see which class to set for crosstalk output so that knitr knows what to do with it.

Hope it helps understand

cderv commented 12 months ago

Example with rmarkdown showing that not fig.cap is inserted

--- 
title: "test"
author: "John Doe"
output: html_document
---

```{r crosstalk, fig.cap = "Missing caption"}
library(crosstalk)
library(plotly)
shared_mtcars <- SharedData$new(mtcars)
bscols(widths = 12,
  filter_slider("hp", "Horsepower", shared_mtcars, ~hp, width = "100%"),
  plot_ly(shared_mtcars, x = ~wt, y = ~mpg, color = ~factor(cyl)) |>
    add_markers()
)
plot_ly(mtcars, x = ~wt, y = ~mpg, color = ~factor(cyl)) |>
  add_markers()