rstudio / htmltools

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

Add selfcontained option to save_html() #73

Open timelyportfolio opened 7 years ago

timelyportfolio commented 7 years ago

I could have easily missed an easier solution, but saving a tagList especially as selfcontained = TRUE seems difficult. Here is the function I wrote to accomplish this using bits and pieces of htmltools. Would this be deemed as something worth of htmltools?

save_tags <- function (tags, file, selfcontained = F, libdir = "./lib") 
{
    if (is.null(libdir)) {
        libdir <- paste(tools::file_path_sans_ext(basename(file)), 
            "_files", sep = "")
    }
    htmltools::save_html(tags, file = file, libdir = libdir)
    if (selfcontained) {
        if (!htmlwidgets:::pandoc_available()) {
            stop("Saving a widget with selfcontained = TRUE requires pandoc. For details see:\n", 
                "https://github.com/rstudio/rmarkdown/blob/master/PANDOC.md")
        }
        htmlwidgets:::pandoc_self_contained_html(file, file)
        unlink(libdir, recursive = TRUE)
    }
    return(file)
}
cpsievert commented 7 years ago

IMO, yes, this could have a lot of impact. And ideally, the implementation would not depend on pandoc, but instead depend on the base64enc package.

Furthermore, it would be nice if there was an option to just return a character string of HTML (instead of always writing to disk). This would be especially useful for embedding tags/htmlwidgets in Jupyter/nteract/etc notebooks, for instance.

If I can get a 👍 from the maintainer, I may be able to find time for a prototype...

cpsievert commented 6 years ago

FWIW, I just implemented something related to this for Jupyter notebooks -- https://github.com/IRkernel/repr/blob/master/R/repr_htmlwidget.r

cpsievert commented 5 years ago

Maybe it makes sense to have renderDependencies() have a self_contained arg (which would base64 encode the dependencies), so other functions, like renderDocument() can take advantage

cpsievert commented 1 year ago

What we should really do is basically copy https://github.com/ramnathv/htmlwidgets/blob/master/R/pandoc.R over to {htmltools} (and use it to implement htmltools::save_html(selfcontained = TRUE) which would mean htmlwidgets::saveWidget() could be a simple wrapper around htmltools::save_html())

Also, when we copy over the implementation, we should look into using {pandoc} over {rmarkdown} for pandoc_available() and pandoc_convert()

cderv commented 1 year ago

Just some thought about this

pandoc.R in htmlwidget seems to be adapted to work with rmarkdown::pandoc_self_contained_html

Forked from htmltools::save_html to work better with pandoc_self_contained_html

However, we need to revisit the function in {rmarkdown} https://github.com/rstudio/rmarkdown/issues/2087, and maybe this is something to consider lower level and store in {pandoc}

I think we should try synchronize on this Carson when you think you'll tackle this.

cpsievert commented 1 year ago

@cderv I'm starting to look into this, and it does seem like having essentially the equivalent of

https://github.com/rstudio/rstudio/blob/0420c87/src/cpp/session/modules/ModuleTools.R#L375

(but with a sensible default for template?) exported by {pandoc} or {rmarkdown} would indeed be quite useful for {htmltools} (and thus {htmlwidgets}). That said, I may just experiment with a version that's local to {htmltools} for now and send you a PR (probably to {pandoc}?) if it feels general enough

cderv commented 1 year ago

Yes it feels to me general enough. We have https://github.com/rstudio/rmarkdown/issues/2087 for rmarkdown function, so we need to rewrite this function. I agree.

aloboa commented 11 months ago

Is there a planned date for implementing this feature? Meanwhile I'm using the suggested function (@ timelyportfolio thanks!), using rmarkdown::pandoc_available() and rmarkdown::pandoc_self_contained_html