How to convert R Markdown to HTML? I.e., What does "Knit HTML" do in Rstudio 0.96?
Asked Answered
C

4

75

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.

Chicken answered 18/5, 2012 at 4:20 Comment(2)
I don't actually know for sure but I suspect it's a combination of using knit to convert from R Markdown to Markdown and then maybe using something like pandoc to convert to html. Or maybe RStudio is doing the conversion to html. But pandoc is pretty easy to use and could be easily added into a makefile.Streetlight
I slightly updated the title to "How to convert R Markdown to HTML" and put the RStudio bit at the end of the title on the assumption that people may be more likely to be googling "how to convert R markdown to html".Chicken
A
45

Put Sys.sleep(30) in a chunk and you will see clearly what commands are called by RStudio. Basically they are

  1. library(knitr); knit() to get the markdown file;
  2. RStudio has internal functions to convert markdown to HTML;

The second step will be more transparent in the next version of the markdown package. Currently you can use knitr::knit2html('your_file.Rmd') to get a similar HTML file as RStudio gives you.


Update on 2019/09/17: The above answer applies to RStudio v0.96 (in the year 2012). Now R Markdown is compiled through rmarkdown::render(), which uses Pandoc instead of the retired R package markdown. See the post Relationship between R Markdown, Knitr, Pandoc, and Bookdown for more details.

Azole answered 18/5, 2012 at 14:16 Comment(4)
I have an example for which knit2html("Report.Rmd", encoding='ISO8859-1') does not produce the same html as the knit button: there's an encoding problem when I use knit2html. However when I press the knit button, I indeed see in the log that ISO8859-1 is the encoding option. Thus the difference should lie in md to html conversion. Does the knit button really use markdownToHTML ?Ur
I think the canonical answer here should mention rmarkdown::render(), which is how I got to this page and which solved my questionBortz
@Bortz You are correct. This answer was written in 2012, when the rmarkdown package was not born yet (rmarkdown was what I meant by "the next version of the markdown package"). Please feel free to edit my answer. Thank you!Azole
I figured as much! I would have edited it but I wanted to leave it to you how best to fit it in the flow of the answer/distinguishing knitr from rmarkdown approach as I'm not very confident I'll do it justice (I just learned how to do this today after all!)Bortz
C
64

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')

Chicken answered 10/6, 2012 at 13:30 Comment(2)
now the answer is given by options(rstudio.markdownToHTML=... rstudio.com/ide/docs/authoring/markdown_custom_renderingUr
Rather than using markdownToHTML, knitr now seems to use rmarkdown::render(), see the note here: rforge.net/doc/packages/knitr/knit2html.htmlPhthalocyanine
A
45

Put Sys.sleep(30) in a chunk and you will see clearly what commands are called by RStudio. Basically they are

  1. library(knitr); knit() to get the markdown file;
  2. RStudio has internal functions to convert markdown to HTML;

The second step will be more transparent in the next version of the markdown package. Currently you can use knitr::knit2html('your_file.Rmd') to get a similar HTML file as RStudio gives you.


Update on 2019/09/17: The above answer applies to RStudio v0.96 (in the year 2012). Now R Markdown is compiled through rmarkdown::render(), which uses Pandoc instead of the retired R package markdown. See the post Relationship between R Markdown, Knitr, Pandoc, and Bookdown for more details.

Azole answered 18/5, 2012 at 14:16 Comment(4)
I have an example for which knit2html("Report.Rmd", encoding='ISO8859-1') does not produce the same html as the knit button: there's an encoding problem when I use knit2html. However when I press the knit button, I indeed see in the log that ISO8859-1 is the encoding option. Thus the difference should lie in md to html conversion. Does the knit button really use markdownToHTML ?Ur
I think the canonical answer here should mention rmarkdown::render(), which is how I got to this page and which solved my questionBortz
@Bortz You are correct. This answer was written in 2012, when the rmarkdown package was not born yet (rmarkdown was what I meant by "the next version of the markdown package"). Please feel free to edit my answer. Thank you!Azole
I figured as much! I would have edited it but I wanted to leave it to you how best to fit it in the flow of the answer/distinguishing knitr from rmarkdown approach as I'm not very confident I'll do it justice (I just learned how to do this today after all!)Bortz
C
30

Very easy command line method from knitr in a knutshell:

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).

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; just like compilation in RStudio does.

Caboodle answered 26/6, 2015 at 0:36 Comment(2)
+1! Combining it with the bash command find, you can actually render a whole directory structure of Rmd files (e.g. for a course that has many presentations/tutorials): find . -name '*Rmd' -exec R -e "rmarkdown::render('{}')" \; > /dev/null.Schelling
Does escaping the inner quotes with a backslash work?Caboodle
A
6

It seems you should call rmarkdown::render() instead of knitr::knit2html() because a.rmd appears to be an R Markdown v2 document.

Archimage answered 6/7, 2016 at 23:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.