简体   繁体   English

如何将R Markdown转换为HTML?即,“编织HTML”在R96 0.96中做了什么?

[英]How to convert R Markdown to HTML? I.e., What does “Knit HTML” do in Rstudio 0.96?

What commands are run when pressing "Knit HTML" on an R Markdown file in Rstudio 0.96? 在Rstudio 0.96中的R Markdown文件上按“Knit HTML”时会运行什么命令?

My motivation is that I might want to run the same command when I'm in another text editing environment or I might want to combine the command in a larger makefile . 我的动机是,当我在另一个文本编辑环境中时,我可能想要运行相同的命令,或者我可能想要在更大的makefile组合命令。

Basic Script 基本脚本

So now that the R markdown package has been released , here is some code to replicate the features of Knit to Html. 现在已经发布R markdown软件包 ,这里有一些代码可以将Knit的功能复制到Html。

require(knitr) # required for knitting from rmd to md
require(markdown) # required for md to html 
knit('test.rmd', 'test.md') # creates md file
markdownToHTML('test.md', 'test.html') # creates html file
browseURL(paste('file://', file.path(getwd(),'test.html'), sep='')) # open file in browser 

where test.rmd is the name of your R markdown file. 其中test.rmd是R markdown文件的名称。 Note that I'm not 100% confident about the browseURL line (hence my question here about opening files in a web browser ). 请注意,我对browseURL行没有100%的信心(因此我的问题是关于在Web浏览器中打开文件 )。

markdownToHTML Options markdownToHTML选项

The good thing about markdownToHTML is that there are heaps of options in how the HTML is created (see ?markdownHTMLOptions ). markdownToHTML的好处在于如何创建HTML(参见?markdownHTMLOptions )。 So for example, if you want just a code fragment without all the header information, you could write: 因此,例如,如果您只想要一个没有所有标题信息的代码片段,您可以编写:

markdownToHTML('test.md', 'test.html', options='fragment_only')

or if you don't like hard wrapping (ie, inserting line breaks when there are single manual line breaks in the markdown source), you can omit the 'hard_wrap' option. 或者如果您不喜欢硬包装(即,在降价源中有单个手动换行符时插入换行符),则可以省略'hard_wrap'选项。

# The default options are 'hard_wrap', 'use_xhtml', 
#      'smartypants', and 'base64_images'.
markdownToHTML('test.md', 'test.html', 
       options=c('use_xhtml', 'base64_images'))

Makefile Makefile文件

This could also all be added to a makefile perhaps using Rscript -e (eg, something like this ). 这也可以全部添加到makefile中,也许使用Rscript -e (例如, 像这样的东西 )。 Here's a basic example makefile I put together, where test indicates that the rmd file is called test.rmd . 这是我放在一起的基本示例makefile,其中test表示test.rmd文件名为test.rmd

RMDFILE=test

html :
    Rscript -e "require(knitr); require(markdown); knit('$(RMDFILE).rmd', '$(RMDFILE).md'); markdownToHTML('$(RMDFILE).md', '$(RMDFILE).html', options=c('use_xhtml', 'base64_images')); browseURL(paste('file://', file.path(getwd(),'$(RMDFILE).html'), sep=''))"

The makefile uses my preferred markdown options: ie, options=c('use_xhtml', 'base64_images') makefile使用我喜欢的markdown选项:ie, options=c('use_xhtml', 'base64_images')

Put Sys.sleep(30) in a chunk and you will see clearly what commands are called by RStudio. Sys.sleep(30)放入一个块中,您将清楚地看到RStudio调用的命令。 Basically they are 基本上他们是

  1. library(knitr); knit() library(knitr); knit() to get the markdown file; library(knitr); knit()获取markdown文件;
  2. RStudio has internal functions to convert markdown to HTML; RStudio具有将markdown转换为HTML的内部函数;

The second step will be more transparent in the next version of the markdown package. 第二步将在下一版本的markdown包中更加透明。 Currently you can use knitr::knit2html('your_file.Rmd') to get a similar HTML file as RStudio gives you. 目前你可以使用knitr::knit2html('your_file.Rmd')来获得与RStudio相似的HTML文件。

Very easy command line method from knitr in a knutshell : knithell中knitr非常简单的命令行方法:

R -e "rmarkdown::render('knitr_example.Rmd')"

This requires rmarkdown to be installed with install.packages(rmarkdown) and that pandoc is installed (apparently it comes with Rstudio, see knitr in a knutshell for more details). 这需要rmarkdown与安装install.packages(rmarkdown)pandoc安装(显然它与Rstudio,看到knitr在knutshell有详细介绍)。

So far when I've used this it nicely puts all the plots in the HTML file rather than as images in a figure directory and cleans up any intermediate files, if any; 到目前为止,当我使用它时,它很好地将所有绘图放在HTML文件中,而不是图形目录中的图像,并清理任何中间文件,如果有的话; just like compilation in RStudio does. 就像在RStudio中编译一样。

看来你应该调用rmarkdown :: render()而不是knitr :: knit2html(),因为a.rmd似乎是一个R Markdown v2文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM