使用rmarkdown自动编号图

时间:2016-06-12 09:13:47

标签: r knitr r-markdown

我找到了this page来编号和参考图。它工作正常,但最近我将rstudio升级到版本0.99.902并删除了缓存,不再将Rmd转换为html。我曾经使用过cache = TRUE但现在我想从头开始重新编译它,但是我在第一个代码块上得到一个错误,它以:

开头
```{r data.analysis, fig.cap=fig$cap("analysi", "Information in each column")}
meta.info <- as.data.frame(lapply(col.data, na.replace))
variable.info <- sapply(meta.info, filter.info)

错误是:

label: data.analysis (with options) 
List of 1
 $ fig.cap: language fig$cap("analysi", "Information in each column")

Quitting from lines 218-224 (IEO_report.Rmd) 
Error in ref[[refName]] <<- i : object 'ref' not found

我在rstudio和从基础R使用knit("file.Rmd", "file.md")时都会收到此错误,因此我认为这不是rstudio错误。

但是可以编译使用以下选项的那些:

```{r data.analysis, fig.cap="\\label{fig:col.data.analysis}Figure 1."}

我认为我需要更改fig选项的顺序,以便在ref之前使cap可用,但这不起作用。

fig <- local({
    i <- 0
    list(
        cap=function(refName, text, center=FALSE, col="black", inline=FALSE) {
            i <<- i + 1
            ref[[refName]] <<- i
            css_ctr <- ""
            if (center) css_ctr <- "text-align:center; display:inline-block; width:100%;"
            cap_txt <- paste0("<span style=\"color:", col, "; ", css_ctr, "\">Figure ", i, ": ", text , "</span>")
            anchor <- paste0("<a name=\"", refName, "\"></a>")
            if (inline) {
                paste0(anchor, cap_txt)    
            } else {
                list(anchor=anchor, cap_txt=cap_txt)
            }
        },

        ref=function(refName, link=FALSE, checkRef=TRUE) {

            ## This function puts in a cross reference to a caption. You refer to the
            ## caption with the refName that was passed to fig$cap() (not the code chunk name).
            ## The cross reference can be hyperlinked.

            if (checkRef && !refName %in% names(ref)) stop(paste0("fig$ref() error: ", refName, " not found"))
            if (link) {
                paste0("<A HREF=\"#", refName, "\">Figure ", ref[[refName]], "</A>")
            } else {
                paste0("Figure ", ref[[refName]])
            }
        },

        ref_all=function(){
            ## For debugging
            ref
        })
})

如何更改此代码以按预期工作? (为什么它不能用于预期的行为?knitr包的某些更新是否打破了这个?

0 个答案:

没有答案