编织RMarkdown到Word会丢失内部锚点

时间:2015-01-07 22:03:59

标签: r anchor docx r-markdown pander

我正在将一个Rmd文件编织到docx(客户端要求)并且遇到了一个奇怪的问题:如果我编织HTML,内部锚点我已经包含了工作(所以他们可以单击“表1”并且它但是,如果我编织到.docx,链接会出现在文本中,但是锚点似乎已经消失了。如果我编织.pdf,则不会出现。

链接和锚点在函数(tabRef())中生成,该函数自动为每个表编号,并为其应用标题。

可以将以下内容复制到Rmd文件中以演示我的意思:

---
title: "Pander Test"
author: "Matt"
date: "Wednesday, January 07, 2015"
output:
  html_document:
    keep_md: yes
  pdf_document: default
  word_document: default
---

```{r, echo=FALSE, message=FALSE}
require(pander)

tabRef <- local({
    tag <- numeric()
    created <- logical()
    used <- logical()
    function(label, caption, prefix = options("tabcap.prefix"), 
             sep = options("tabcap.sep"), 
             prefix.highlight = options("tabcap.prefix.highlight")) {
                i <- which(names(tag) == label)
                if (length(i) == 0) {
                    i <- length(tag) + 1
                    tag <<- c(tag, i)
                    names(tag)[length(tag)] <<- label
                    used <<- c(used, FALSE)
                    names(used)[length(used)] <<- label
                    created <<- c(created, FALSE)
                    names(created)[length(created)] <<- label
                }
                if (!missing(caption)) {
                    # Create Anchor
                    created[label] <<- TRUE
                    sprintf('<a name="%s"/> %s', 
                            paste0("Table", i), 
                            paste0(prefix.highlight, prefix, i, sep, prefix.highlight, " ", caption))
                } else {
                    # Create Link to Anchor
                    used[label] <<- TRUE
                    sprintf('[%s](#%s)', 
                            paste0(prefix, tag[label]), 
                            paste0("Table", tag[label]))
                }
            }
})
options(tabcap.prefix = "Table I.", tabcap.sep = ".", tabcap.prefix.highlight = "**")
```

Reference the tables:

Here is the first `r tabRef("MLtab")`.  You should also see `r tabRef("USAtab")`.  

Oh, and this one is also pretty cool: `r tabRef("MLtab2")`.

Make the tables:

```{r, echo=FALSE}
ml <- with(lm(mpg ~ hp + wt), data = mtcars)

set.caption(tabRef("MLtab", "Linear regression with mtcars."))
pander(ml)
```
Add figure for some white space.

```{r}
plot(cars)
```

Here is another:

```{r, echo=FALSE}
pr <- prcomp(USArrests)
set.caption(tabRef("USAtab", "Principal Components Analysis."))
pander(pr)
```

Add figure for some white space.

```{r}
plot(cars)
```

Add figure for some white space.

```{r}
plot(cars)
```

Add figure for some white space.

```{r}
plot(cars)
```

```{r, echo=FALSE}
st <- table(mtcars$am, mtcars$gear)
set.caption(tabRef("MLtab2", "Simple Table."))
pander(st)
```

What about linking backward?  Let's try to see `r tabRef("MLtab")`.

任何建议都会非常感激!

0 个答案:

没有答案