如何从包含htmlwidget的rmarkdown文件生成md文件

6
我正在使用这个rmd文件创建一个HTML文件。
---
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")  
```

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

输出结果没问题,但我需要 markdown 文件 (file.md) 以便稍后重新创建 html 文件(没有访问数据的情况下)。rmarkdown 生成了两个 md 文件 [file.knit.md] 和 [file.utf8.md],但是当我渲染其中任意一个时,htmlwidget 在 file.html 中丢失了。
rmarkdown::render(  input = 'file.utf8.md' , output_file = 'file.html'
                    , output_format=   html_document(  self_contained=TRUE)   )  

rmarkdown调用pandoc时有一个[--include-in-header]参数,指向一个包含小部件依赖项的临时文件,但我没有找到在渲染md文件时包含它的方法(即--include-in-header /tmp/Rtmp1M0RpP/rmarkdown-str1a169e14827.html")

下面是rmarkdown中的pandoc调用,但执行此命令仍会生成一个没有htmlwidget的html文件。

/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 

我需要的是具有交互式htmlwidget的html文件,而不是该widget的png图像。使用以下代码,我可以生成一个file.md文件,然后渲染出一个带有widget png图像的html文件,而不是交互式js widget。
rmarkdown::render(  input = 'file.RMD' , output_file = 'file.md'
                    , output_format=   md_document( ) )  

我发现这个问题与这个问题有关: 提取包含htmlwidgets的.Rmd文件的html依赖项

2个回答

2
您可以告诉rmarkdown在YAML头中保留.md文件(请参阅文档):
---
title: "test"
author: "me"
date: '`r Sys.Date()`'
output: 
  html_document:
    keep_md: true
---

此外,如果您知道要使用额外的选项调用rmarkdown::render(就像您使用self_contained = TRUE一样),您也可以在标头中指定它们:
---
title: "test"
author: "me"
date: '`r Sys.Date()`'
output: 
  html_document:
    keep_md: true
    self_contained: true
---

我已经测试过了。在头部中使用keep_md: true与rmarkdown::render中的clean = FALSE是等效的。这两个参数可以防止[file.knit.md]和[file.utf8.md]被删除。我的问题是,我无法找到一种方法从这些md文件生成带有js小部件的html。 - Eduardo Bergel
1
哦,没错,我的错误。你需要在最终文件中包含HTML依赖项。如果你调用 x <- rmarkdown::render("file.RMD", run_pandoc = FALSE, clean = FALSE)attr(x, "knit_meta"),你将得到一个包含所有必需依赖项的列表。我认为你可以创建一个函数,将所有这些添加到从 .md 文件上的 render 调用生成的 .html 文件中。 - Tutuchan
我刚找到了一种实现你的建议的方法。在rmarkdown::render中有一个knit_meta参数。 - Eduardo Bergel

1
可以这样做。
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 )

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接