ramnathv / htmlwidgets

HTML Widgets for R
http://htmlwidgets.org
Other
788 stars 208 forks source link

Problem saving widget with custom html #239

Open johndharrison opened 7 years ago

johndharrison commented 7 years ago

I am trying to add custom html to an htmlwidget as per https://cran.r-project.org/web/packages/htmlwidgets/vignettes/develop_advanced.html#custom-widget-html

To replicate the issue I have create a simple htmlwidget package with minimal code using

htmlwidgets::scaffoldWidget("mywidget")

I added some custom html for the widget https://github.com/johndharrison/simplewidget/commit/23b38b3c392d959aabe37d4c7b10f09df25af106

The widget displays correctly and the custom html functions as expected. However htmlwidgets::saveWidget does not save the widget html as expected:

install.github("johndharrison/simplewidget")
library(simplewidget)
library(xml2)
library(magrittr)
sw <- simplewidget("hello, world")
tFile <- tempfile(fileext = ".html")
htmlwidgets::saveWidget(sw, file = tFile)
browseURL(tFile)
xml2::read_html(tFile) %>% 
  xml2::xml_find_all("//div[@id='htmlwidget_container']") %>% 
  as.character
[1] "<div id=\"htmlwidget_container\">\n<div>\n<pre><code>&lt;h1 id=\"someid\"&gt;My header&lt;/h1&gt;\n&lt;div id=\"htmlwidget-00d700b400000a3685d7\" style=\"width:960px;height:500px;\" class=\"simplewidget html-widget\"&gt;&lt;/div&gt;</code></pre>\n</div>\n</div>"
johndharrison commented 7 years ago

The issue seems to be with pandoc and indentation introduced by htmltools::tags. Using htmltools::renderTags to remove the indentation https://github.com/johndharrison/simplewidget/commit/3ecd3bcfa73a4711b04a799dab8b37a9f9e12ffc seems to solve the issue.

Is this expected behaviour? Should custom html be rendered in _html functions?