Test of knitr -> md

33 views
Skip to first unread message

Dennis Murphy

unread,
Mar 13, 2012, 12:08:18 PM3/13/12
to knitr
Hi:

As a follow-up to my questions of last night,
the attached file knittest_knit_.md, a small subset of the
ggplot2-0.9.0.Rnw document originally prepared for pdfLaTeX, was run
using knitr to create the output file knittest.md, visible at
https://github.com/djmurphy420/ggplot2-transition-guide/blob/master/knittest.md.
The attempt to create a table similar to that in the pdf document
didn't work, and the attempt to place graphics side by side in the
same row also failed. I realize that markdown is meant to be a simple
language, so it's possible to work around the multiple graph per line
problem if necessary (unless I can figure out an opts_chunk$set()
hook). I tried to mimic the example on the Github markdown page, but
no dice. Any assistance or pointers to relevant documentation would be
appreciated.

Dennis

knittest.md
knittest_knit_.md

Yihui Xie

unread,
Mar 14, 2012, 3:07:00 AM3/14/12
to Dennis Murphy, knitr
1. leave a blank line before the plot chunk, so that the plots appear
in a new paragraph and they will sit side by side unless they are two
wide to fit in one line; see markdown syntax (empty lines indicate new
paragraphs)

2. since you are using ggplot2, you are free to remove the hook

knit_hooks$set(fig = function(before, options, envir){
if(before && options$fig.show != "none")
par(mar = c(4, 4, 0.1, 0.1), cex.lab = 0.95, cex.axis = 0.9,
mgp = c(2, 0.7, 0), tcl = -0.3)
} )

it was designed for base graphics only; ggplot2 does not use par().

3. for tables, you are right; the main idea of markdown is to be
simple, simple and simple; there are other "markdown" variants like
Textile/reStructedText which might support tables... I have not got a
chance to look at them yet.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA

Ramnath Vaidyanathan

unread,
Mar 16, 2012, 12:13:42 PM3/16/12
to kn...@googlegroups.com, Dennis Murphy
Github Flavored Markdown does support Tables. You can use the ascii package and a little bit of regex hackery to achieve this. 

I have put together a gist with an example https://gist.github.com/2050761.

Two things to note:

1. The regex used is naive as it would replace any + symbol in the table which is not desired.
2. To compile the source file gfm-table_knit_.md, remove the ``` markup, which I had to use to force the gist to display code.

Hope this helps.


Yihui Xie

unread,
Mar 16, 2012, 5:30:42 PM3/16/12
to Ramnath Vaidyanathan, kn...@googlegroups.com, Dennis Murphy
gosh, where did you dig this out? I have read the GFM documentation
for many times, and I was never aware of the support for tables!

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA

Ramnath Vaidyanathan

unread,
Mar 17, 2012, 5:19:55 PM3/17/12
to kn...@googlegroups.com, Ramnath Vaidyanathan, Dennis Murphy
It is not well documented. I just happened to see nicely formatted tables in README.md files for some repositories. So I looked at the source and figured out what they were doing. 

My approach right now is a hack, as it just tweaks the output of ascii for 'org' format. It would be easy enough to extend ascii to support 'gfm'. The main advantage is that ascii already has functions to display different types of output including data frames, coefficient tables, etc.

Yihui Xie

unread,
Mar 17, 2012, 6:45:03 PM3/17/12
to Ramnath Vaidyanathan, kn...@googlegroups.com, Dennis Murphy
The bottom line for the ggplot2 transition guide is to write raw HTML
code for these tables. They should not be too hard.

<table>
<tr>
<td>one cell</td>
<td>another cell</td>
</tr>
</table>

If you want to generate tables from data, you can still use the xtable
package, e.g.

library(xtable)
print(xtable(head(iris)), type='html')

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA

Reply all
Reply to author
Forward
0 new messages