【问题标题】:Cross referencing pander-table in rmarkdownrmarkdown中的交叉引用pander表
【发布时间】:2022-01-13 13:10:25
【问题描述】:

尽管关于这个主题的问题很多,尽管阅读了the manual,包括最后一段,我还是无法理解它。我找到了 pdf 输出的解决方案,但希望它也适用于 html 输出(理想情况下,一种适用于 pdf 和 html 输出的方法 - 就像我在下面使用 opts_knit$get("rmarkdown.pandoc.to") 的尝试一样,遗憾的是 html 失败了)。

使用 bookdown,很容易引用 kabel(Extra)- 或 flextable- 表格,但我对 pander-tables 的解决方法仅适用于 pdf 输出。谁能指导我如何使用 html 来实现这一点?

下面的 MWE 示例适用于 pdf,但在输出到 html 时对最后一个表的引用会中断。

MWE:

---
title: "Untitled"
output:
    bookdown::pdf_document2:
        keep_tex: yes
    bookdown::html_document2: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)

library(knitr)
library(tidyverse)
library(pander)
library(flextable)
```


```{r}

df <- expand.grid(A = LETTERS[1:2],
              B = letters[3:5], 
                 stringsAsFactors = FALSE)%>%
              mutate( x = round(rnorm(nrow(.)),1))%>%
              arrange(A,B)


```


```{r mypanderworkaround}

addlabref <- function(fcap, flab){

   if (opts_knit$get("rmarkdown.pandoc.to") %in% 
    c("beamer", "latex")){

    lab <- sprintf("\\label{tab:%s}",flab)

    sprintf("%s %s", lab, fcap)

  }else{

    lab <- sprintf("\\#tab:%s",flab)

    sprintf("<caption>%s %s</caption>", lab, fcap)

  }

}

```


```{r c1}

kable(df, caption = "table with kable")

```

```{r c2}

flextable(df)%>%set_caption(caption = "table with flextable")

```


```{r c3}

pander(df, caption = addlabref("table with pander", "c3") )
```


kable figure is \@ref(tab:c1).

flextable figure is \@ref(tab:c2)

pander figure is \@ref(tab:c3)

【问题讨论】:

    标签: r r-markdown knitr pander


    【解决方案1】:

    这应该会有所帮助:

    我们的餐桌:

    <br>
    <div style="text-align: center">
       <caption><span id="tab:c3">Table 3: </span>My table is here</caption>
    </div>
    
    <div style="width: 50%; margin: 0 auto;">
    ```{r c3}
    pander(head(mtcars[1:5]))
    ```
    </div>
    

    我们的参考:

    pander figure is <a href="#tab:c3">3</a>
    

    ...看起来像

    你的桌子也是如此:

    <br>
    <div style="text-align: center">
       <caption><span id="tab:c3">Table 3: </span>My table is here</caption>
    </div>
    
    <div style="width: 10%; margin: 0 auto;">
    ```{r c3}
    pander(df)
    ```
    </div>
    


    适用于任何地方:

    ```{r c3}
    pander(df, caption = "(\\#tab:table-label) Table caption" )
    ```
    
    pander figure is \@ref(tab:table-label)
    

    【讨论】:

    • 谢谢!问题是它不能轻易地结合到灵活的输出格式方法中。理想情况下,这将是给caption = ... 的东西,就像我的 MWE for pander 一样
    • @Dries 你想为 pdf 和 html 找到一个通用的解决方案吗?
    • 确实如此。我尝试让我的 rmd-docs 尽可能独立于输出格式
    • 太棒了!我以为我已经尝试过所有类似的组合,但显然我错过了正确的组合
    • 对于简单的表格,我认为它有更好的默认值。不需要,例如为booktabs = TRUE 避免看起来很糟糕的桌子。另外,我相信pander(tab, split.table=Inf) 没有快速的 kable 替代方案。
    【解决方案2】:

    根据@manro 的回答,我将 MWE 中的 addlabref-function 更新为

    addlabref <- function(fcap){
    
    sprintf("(\\#tab:%s) %s", knitr::opts_current$get("label"), fcap)
    
    }
    

    通过使用knitr::opts_current$get("label"),它保持kableflextable 使用块名称作为标签的行为。 此函数可用于添加标签以引用 pander-caption 像

    pander( df, caption = addlabref("my caption") )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-01
      • 2019-02-26
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      • 2018-10-14
      • 1970-01-01
      相关资源
      最近更新 更多