Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 561
Eduardo Bergel Avatar asked Aug 12 '16 02:08

Eduardo Bergel


2 Answers

You can tell rmarkdown to keep the .md file in the YAML header (see the documentation):

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

Furthermore, if you know you're going to call rmarkdown::render with additional options (like you do with self_contained = TRUE), you can also specify them in the header:

---
title: "test"
author: "me"
date: '`r Sys.Date()`'
output: 
  html_document:
    keep_md: true
    self_contained: true
---
like image 148
Tutuchan Avatar answered Sep 22 '22 07:09

Tutuchan


Can be done like this

x <- rmarkdown::render("file.RMD", run_pandoc = FALSE, clean = FALSE)
knit_meta <- attr(x, "knit_meta") 
rmarkdown::render(   input = 'file.knit.md'    , knit_meta = knit_meta )
like image 29
Eduardo Bergel Avatar answered Sep 22 '22 07:09

Eduardo Bergel