【问题标题】:Huxtable package for R: How to correctly reference huxtables in bookdown?R 的 Huxtable 包:如何在 bookdown 中正确引用 huxtables?
【发布时间】:2018-10-18 06:47:39
【问题描述】:

使用 bookdown 编译报告我在引用使用 huxtable 包生成的表格时遇到了困难。对于我的工作,需要创建 LaTex/PDF 以及 HTML 版本的报告。

在渲染文档时 knitr::is_XXX_output() 选择显示表格的最佳方式,请参阅 MWE:

```{r chunk-label, results='asis', fig.cap='chunk-caption'}
set.seed(1234)
dat <- data.frame(cond = factor(rep(c("A","B"), each=2)), 
                   rating = c(rnorm(2),rnorm(2, mean=.8)))

hux <- as_hux(dat)                    %>%
  set_caption('hux caption')          %>% 
  set_label("tab:hux-label")                                              

if (knitr::is_html_output()) {
  print_html(hux)   # output table html friendly (requires in chunk options "results='asis'")
}
if (knitr::is_latex_output()) {
  hux
}
```

不确定是否推荐使用huxtable提供的caption和label命令

  set_caption('pipe caption') and set_label("tab:hux-label")    

或编织者

  chunk-label and fig.cap='chunk caption'

对于数字,后者效果很好,但不幸的是不适用于表格。

下面讨论的“tab.cap”钩子不适用于 bookdown 以及需要 PDF 和 HTML 的情况。 Using table caption on R markdown file using knitr to use in pandoc to convert to pdf

非常感谢您的帮助和建议!

【问题讨论】:

  • 我不确定您的实际问题是什么,但这可能是相关的:stackoverflow.com/questions/46318658/…
  • 非常感谢@dash2 的回复。我的目标是生成带有标签和标题的 huxtable,它们适用于 HTML 和 LaTeX 输出。在 HTML 中,我可以使用set_caption('(#tab:setcaption) my caption'),它可以正常工作。在 LaTeX/PDF 中,我可以使用set_caption('(\\#tab:setcaption) caption')——它只适用于一个数字。如果我包括两个数字,我会得到! Package caption Error: \caption outside float. 此外,来自 huxtable 包的set_label() 没有多大帮助,因为它在 bookdown AFAIK 中不起作用。任何人都有一个想法,也许关于如何规避浮动错误?
  • hux &lt;- add_footnote(hux, 'my footnote') 在 bookdown 中不起作用,产生以下错误:Error in if (! Attr (input, "format")% in% c ("html", "latex")) {:   Argument has length 0

标签: r


【解决方案1】:

如果您升级到 huxtable 4.3.0(现在在 CRAN 上),它会自动为您处理 bookdown 表格标题。这是一个简短的例子:

---
title: "Bookdown test"
output:
  bookdown::pdf_book: default
link-citations: yes
---

```{r setup, include=FALSE}

library(dplyr)
library(huxtable)
knitr::opts_chunk$set(echo = FALSE)

```

My table is \@ref(tab:foo1). The other table is \@ref(tab:foo2). The third is \@ref(tab:foo3).

```{r}


hux(a = 1:5, b = 1:5) %>% 
  set_caption("My labelled table") %>% 
  set_label("tab:foo1")

hux(a = 1:5, b = 1:5) %>% 
  set_caption("My unlabelled table")

hux(a = 1:5, b = 1:5) %>% 
  set_caption("My labelled table, prefix should be autoadded!") %>% 
  set_label("foo2")

hux(a = "A table with no caption, but a label") %>% 
  set_label("tab:foo3")

hux(a = "A table with no caption or label")

```

并非一切都是完美的。如果您设置了echo = TRUE,则需要在TeX 标头中的\usepackage{fancyvry} 之前手动插入\usepackage[table]{xcolor}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-02
    • 2011-07-12
    • 1970-01-01
    • 2022-06-10
    相关资源
    最近更新 更多