rstudio / bookdown

Authoring Books and Technical Documents with R Markdown
https://pkgs.rstudio.com/bookdown/
GNU General Public License v3.0
3.71k stars 1.27k forks source link

crosstalk html widget doesn't display the figure caption #1443

Open ThierryO opened 9 months ago

ThierryO commented 9 months ago

A chunk with a plot_ly() gets the correct caption as fixed in #118. However, the crosstalk version gets no caption.

Minimal example

--- 
title: "A Minimal Book Example"
author: "John Doe"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
output:
  bookdown::gitbook: default
---

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

Session info

R version 4.3.1 (2023-06-16)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.3 LTS, RStudio 2023.12.0.99

Locale:
  LC_CTYPE=nl_BE.UTF-8       LC_NUMERIC=C               LC_TIME=nl_BE.UTF-8        LC_COLLATE=nl_BE.UTF-8    
  LC_MONETARY=nl_BE.UTF-8    LC_MESSAGES=nl_BE.UTF-8    LC_PAPER=nl_BE.UTF-8       LC_NAME=C                 
  LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=nl_BE.UTF-8 LC_IDENTIFICATION=C       

time zone: Europe/Brussels
tzcode source: system (glibc)

Package version:
  base64enc_0.1.3   bookdown_0.35     bslib_0.5.1       cachem_1.0.8      cli_3.6.1         digest_0.6.33    
  ellipsis_0.3.2    evaluate_0.22     fastmap_1.1.1     fontawesome_0.5.2 fs_1.6.3          glue_1.6.2       
  graphics_4.3.1    grDevices_4.3.1   highr_0.10        htmltools_0.5.6   jquerylib_0.1.4   jsonlite_1.8.7   
  knitr_1.44        lifecycle_1.0.3   magrittr_2.0.3    memoise_2.0.1     methods_4.3.1     mime_0.12        
  R6_2.5.1          rappdirs_0.3.3    rlang_1.1.1       rmarkdown_2.25    sass_0.4.7        stats_4.3.1      
  stringi_1.7.12    stringr_1.5.0     tinytex_0.47      tools_4.3.1       utils_4.3.1       vctrs_0.6.3      
  xfun_0.40         yaml_2.3.7  
cderv commented 9 months ago

This is because bscols() does not return an object of class htmlwidget so it is not seen as an object to add caption too (like #118 fixed).

@yihui How could we handle that ? Should we look maybe into the object when attr(x, "browsable_html") == TRUE ? Or rather this should be crosstalk that should add a class to the object returned by bscols() so that it is identified as a crosstalk object we can then handle ?

yihui commented 9 months ago

Should we look maybe into the object when attr(x, "browsable_html") == TRUE ?

We could do that.

ThierryO commented 9 months ago

Based on your feedback, I've found a workaround. Ugly but effective.

--- 
title: "A Minimal Book Example"
author: "John Doe"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
output:
  bookdown::gitbook: default
---

```{r crosstalk, fig.cap = "Workaround caption"}
library(crosstalk)
library(plotly)
shared_mtcars <- SharedData$new(mtcars)
x <- 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()
)
class(x) <- c(class(x), "htmlwidget")
x
```