Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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.

like image 919
Jeromy Anglim Avatar asked May 18 '12 04:05

Jeromy Anglim


People also ask

What does Knit do in RStudio?

Rendering Output If you are using RStudio, then the “Knit” button (Ctrl+Shift+K) will render the document and display a preview of it.

What does it mean to Knit a Markdown file in R?

knit - You can knit the file. The rmarkdown package will call the knitr package. knitr will run each chunk of R code in the document and append the results of the code to the document next to the code chunk. This workflow saves time and facilitates reproducible reports.

What is the difference between RStudio and R Markdown?

To put it simply - R is the actual programming language, RStudio is a convenient interface in which to use it, and R Markdown is a specific type of file format designed to produce documents that include both code and text.

Why can't I Knit to HTML in R?

No Knit HTML button This means that RStudio doesn't understand your document is supposed to be an RMarkdown document, often because your file extension is . txt. To fix this, go to the Files tab (lower right corner, same pane as Plots and Help) and select the checkbox next to your document's name.


1 Answers

Basic Script

So now that the R markdown package has been released, here is some code to replicate the features of Knit to 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. Note that I'm not 100% confident about the browseURL line (hence my question here about opening files in a web browser).

markdownToHTML Options

The good thing about markdownToHTML is that there are heaps of options in how the HTML is created (see ?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 (i.e., inserting line breaks when there are single manual line breaks in the markdown source), you can omit the 'hard_wrap' option.

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

Makefile

This could also all be added to a makefile perhaps using Rscript -e (e.g., something like this). Here's a basic example makefile I put together, where test indicates that the rmd file is called 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: i.e., options=c('use_xhtml', 'base64_images')

like image 86
Jeromy Anglim Avatar answered Nov 16 '22 00:11

Jeromy Anglim