在knitr中自动化ggplot2示例库 [英] Automated ggplot2 example gallery in knitr

查看:131
本文介绍了在knitr中自动化ggplot2示例库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用 knitr 制作一个 ggplot2 geoms的画廊。为了做到这一点,我想使用内置的例子来填充内容。



After一些试验和错误我已经到了这一步:

  \documentclass [a4paper,titlepage] {tufte-handout} 
\ usepackage {pdflscape}
\ usepackage {graphicx}
\ usepackage {alltt}

<<< setup<> include = FALSE,cache = FALSE> ;> =
options(replace.assign = TRUE,tidy = TRUE)
library(ggplot2)
library(plyr)
library(scales)
geoms< ; - setdiff(apropos(^ geom _),geom_blank)
@

\title {ggplot2 Gallery}

\ begin {document}
\maketitle
$ b<<<<<<例子,echo = FALSE,comment = NA>> =
for(i in geoms){
writeLines(paste0 (\\section {,gsub(_,\\\\\ _,i),}))
do.call(example,list( i))
}
@

\ end {document}

但是仍然有一些数字我无法解决的问题:


  • 我似乎无法混合标记和 asis 块中的结果选项,以便生成节标题。 (这对以后的文档导航至关重要)。有没有其他方法可以在块中生成LaTex?

  • 有些例子会抛出错误(这就是为什么 geom_blank 被排除在外)。 knitr文档指出,在出现错误时计算仍在继续,但它似乎只是编织过程继续;示例循环在那一点停止。有没有办法避免出现错误的例子? 例子未被语法高亮显示。 (有用但并非必要)。 https://github.com/yihui/knitr-examples/blob/master/021-ggplot2-geoms.Rnwrel =noreferrer> 021-ggplot2-geoms.Rnw 查看完整的代码。基本的想法是在编织它们之前构造代码块。代码很简短,所以我可能不需要太多解释它。



    理论上你应该能够得到类似的东西(超过200页的 ggplot2 例子):


    I'm trying to produce a gallery of ggplot2 geoms using knitr. In order to do this quickly I thought to use the built-in examples to populate the content.

    After some trial and error I've got to this point:

    \documentclass[a4paper,titlepage]{tufte-handout}
    \usepackage{pdflscape}
    \usepackage{graphicx}
    \usepackage{alltt}
    
    <<setup, include=FALSE, cache=FALSE>>=
    options(replace.assign=TRUE,tidy=TRUE)
    library(ggplot2)
    library(plyr)
    library(scales)
    geoms <- setdiff(apropos("^geom_"),"geom_blank")
    @
    
    \title{ggplot2 Gallery}
    
    \begin{document}
    \maketitle
    
    <<examples, echo=FALSE, comment=NA>>=
    for(i in geoms){
        writeLines(paste0("\\section{",gsub("_","\\\\_",i),"}"))
        do.call("example",list(i))
    }
    @
    
    \end{document}
    

    But there are still a number of issues I can't resolve:

    • I can't seem to be able to mix markup and asis results options within the chunk so that section headings get produced. (This will be crucial for navigation of the document later). Is there any other way of producing writing LaTex within the chunk?

    • There are some examples which throw errors (which is why geom_blank is excluded). The knitr documentation states that computation continues in the case of errors, but it only seems to be the knit process which continues; the example loop ceases at that point. Is there a way to avoid the parts of the examples that are errors?

    • The example code output isn't syntaxed highlighted. (Useful but not essential).

    解决方案

    See 021-ggplot2-geoms.Rnw for the full code. The basic idea is to construct the code chunks before knit them. The code is short, so probably I do not need to explain it too much.

    In theory you should be able to get something like this (more than 200 pages of ggplot2 examples):

    这篇关于在knitr中自动化ggplot2示例库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆