rstudio / htmltools

Tools for HTML generation and output
https://rstudio.github.io/htmltools/
214 stars 67 forks source link

Make it easier to render a tag as an iframe #365

Open cpsievert opened 1 year ago

cpsievert commented 1 year ago

Currently you need to save_html(), then tags$iframe(), and take care to mount the right directories. It'd be neat if you could just mark a tag with something like asIFrame() and it just prints sensibly (similar to {widgetframe}, which is now deprecated, but works on any tag and in any context).

As a first pass on this, it might make sense to use an approach similar to https://github.com/jcheng5/shadowdom (i.e., leverage web components to transform the (document-ified) tag into an iframe on the client and also put it under a personal repo until we have full confidence in it)

cpsievert commented 1 year ago

FWIW, here's a decent knitr-based solution.

```{r setup, include=FALSE}
library(knitr)
library(htmltools)

render_as_iframe <- function(x, options, ...) {
  lbl <- opts_current$get("label")
  if (!dir.exists(lbl)) {
    dir.create(lbl)
  }
  file <- file.path(lbl, paste0(lbl, ".html"))
  tryCatch(
    save_html(x, file), 
    error = function(e) {
      stop("Don't know how to render ", class(x)[[1]], " as an <iframe>")
    }
  )
  include_url(file)
}
plotly::plot_ly()