Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What command converts knitr R Markdown into Stack-Exchange-friendly Markdown?

Tags:

r

knitr

Motivation: I often want to paste the results of a quick analysis using R Markdown into a StackExchange site. This includes the R-tag on Stack Overflow, Cross Validated, or even a domain specific analysis on sites like Cognitive Sciences Stack Exchange (e.g., this quick analysis of OECD life index data).

Problems with default conversion: The default markdown output of knitr is not suitable for inclusion on StackExchange. The main problems I can see are that

  • images are referenced to the local hard drive
  • code chunks are not tab or space indented; rather they use github style Markdown (i.e., no indentation)

I.e., the chunks look like this:

```r
some code
```

and output looks like this

```
## some output
## ...
```

There might also be other specific issues to consider, such as

  • ensuring tables are included properly
  • ensuring that equations are passed correctly for sites that support MathJax like Cross Validated and Cognitive Science Stack Exchange.

Question

What is a good command for converting R Markdown into Markdown (or HTML) suitable for simple inclusion into Stack Exchange sites?

I think an ideal command would be a one-liner that takes an R Markdown file and generates a file where the entire content can be pasted directly into Stack Exchange to yield a well-formatted question or answer.

I share this simple rmd file with a couple of code chunks, a figure, and an equation as a test example.

Initial thoughts: Hosting of images on imgur would presumably sort out the issue with images. This can be done by including the following in the R Markdown file, but it would probably be simpler if this instruction was incorporated into some one-liner command.

``` {r }
opts_knit$set(upload.fun = imgur_upload) 
````

It might be worth considering whether HTML or Markdown is the better format for pasting into StackExchange. The markdown package provides a lot of flexibility.

like image 656
Jeromy Anglim Avatar asked Jun 17 '12 02:06

Jeromy Anglim


People also ask

What is R markdown vs Markdown?

R Markdown is an extension of the markdown syntax. R Markdown files are plain text files that typically have the file extension . Rmd . They are written using an extension of markdown syntax that enables R code to be embedded in them in a way which can later be executed.

Is R Markdown a markup language?

Technically, R markdown is a variant of another language (yet another language!) called Markdown and both are a type of 'markup' language. A markup language simply provides a way of creating an easy to read plain text file which can incorporate formatted text, images, headers and links to other documents.


2 Answers

Here is a utility function that should get you started. It sets auto uploads to imgur, as well as markdown rendering of source code using tabs instead of fenced blocks. You can enhance this function to add other options that would be useful.

stackify <- function(rmd_file, ...){
  require(knitr)
  opts_knit$set(upload.fun = imgur_upload) 
  render_markdown(strict = TRUE)
  out <- knit(rmd_file, ...)
  return(invisible(out))
}

UPDATE: I tested this function on your test file, and it renders well on stats.stackexchange.com which is mathjax enabled.

like image 173
Ramnath Avatar answered Oct 01 '22 18:10

Ramnath


Although I'd still like to read other suggestions I hacked together this script using @Ramnath's answer as a starting point. It outputs a HTML fragment rather than Markdown.

Rscript -e 'rmd_file <- dir(pattern="rmd"); md_file <- sub("rmd", "md", rmd_file); html_file <- sub("rmd", "html", rmd_file); require(methods); require(knitr); require(markdown); opts_knit$set(upload.fun = imgur_upload); knit(rmd_file); markdownToHTML(md_file, html_file, options="fragment_only") '
  • It assumes that there is a single rmd file in the working directory. I imagine that selecting the rmd file could be done in a more sophisticated way.
  • It requires that the knitr and markdown packages are installed.
  • I think because we are using 'Rscript' the methods package needs to be loaded.
  • It uploads images to imgur
  • markdownToHTML exports only the code fragment as a html file. The contents of this file can then be copied into the Stack Exchange answer.

The result looks pretty good. It overcomes the issue of excessive blank lines. However, the output is not markdown, which makes the result harder to edit.

like image 37
Jeromy Anglim Avatar answered Oct 01 '22 17:10

Jeromy Anglim