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

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

Open
ThierryO opened this issue Oct 10, 2023 · 2 comments
Open

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

ThierryO opened this issue Oct 10, 2023 · 2 comments

Comments

@ThierryO
Copy link

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

@cderv
Copy link

cderv commented Oct 10, 2023

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
Copy link

cderv commented Oct 10, 2023

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()
)
```

```{r plotly, fig.cap = "Working caption"}
plot_ly(mtcars, x = ~wt, y = ~mpg, color = ~factor(cyl)) |>
  add_markers()
```

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

No branches or pull requests

2 participants