Ron Talbot Ron Talbot - 3 months ago 17
R Question

How to generate an md file from a rmarkdown file containing an htmlwidget

I am creating an html file with this rmd

---
title: "test"
author: "me"
date: '`r Sys.Date()`'
output: html_document
---

```{r}
data(HairEyeColor)
rpivotTable::rpivotTable(data = HairEyeColor
, rows = "Hair"
,cols="Eye"
, vals = "Freq"
, aggregatorName = "Sum"
, rendererName = "Table"
, width="100%"
, height="400px")
```


and

rmarkdown::render( input = 'file.RMD' , output_file = 'file.html'
, output_format= html_document( self_contained=TRUE), clean = FALSE
, quiet = FALSE )


the output is ok, but I need the markdown file (file.md) to recreate the html file at a later time (without access to the data)

rmarkdown generates two md files [file.knit.md] and [file.utf8.md]
but when I render any of these two, the htmlwidget is missing in file.html.

rmarkdown::render( input = 'file.utf8.md' , output_file = 'file.html'
, output_format= html_document( self_contained=TRUE) )


rmarkdown call to pandoc has an [--include-in-header] parameter pointing to a temp file with the widget dependencies, but I am not finding a way to include it when rendering the md file (i.e. --include-in-header /tmp/Rtmp1M0RpP/rmarkdown-str1a169e14827.html" )

Below I pasted the pandoc call from rmarkdown, but again, executing this command alone produced an html file without the htmlwidget.

/usr/bin/pandoc +RTS -K512m -RTS file.utf8.md
--to html --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash
--output test._0021.html --smart --email-obfuscation none --self-contained --standalone
--section-divs --template /home/bergel/R/x86_64-pc-linux-gnu-library/3.3/rmarkdown/rmd/h/default.html
--variable 'theme:bootstrap' --include-in-header /tmp/RtmpqpGfD1/rmarkdown-str19b5232f45d7.html
--mathjax --variable 'mathjax-url:https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'
--no-highlight --variable highlightjs=/home/bergel/R/x86_64-pc-linux-gnu-library/3.3/rmarkdown/rmd/h/highlight
--variable navigationjs=/home/bergel/R/x86_64-pc-linux-gnu-library/3.3/rmarkdown/rmd/h/navigation-1.1


I want the html file with the interactive htmlwidget, not a png of the widget. Using the code below I can generate a file.md that can then render an html file but with a png of the widget, NOT an interactive js widget.

rmarkdown::render( input = 'file.RMD' , output_file = 'file.md'
, output_format= md_document( ) )


I found that this question is related to this one:
Extract html dependencies for .Rmd file (containing htmlwidgets)

Answer

Found the problem. I changed

rmarkdown::render(  input = 'file.RMD' , output_file = 'file.md'
                    , output_format=   md_document( ) ) 

to

rmarkdown::render(  input = 'file.RMD' , output_file = 'file.md'  )  

and now file.html is ok.

Comments