2

Compiling a report with bookdown I encounter difficulties in referencing tables generated with the huxtable package. For my work, LaTex/PDF, as well as an HTML version of the report, need to be created.

When rendering the document knitr::is_XXX_output() selects the optimal way to display the tables, see 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
}
```

I am not sure whether it is recommended to use the caption and label commands provided by huxtable

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

or knitr

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

For figures, the latter works very well, but unfortunately not for tables.

The hook for "tab.cap" as discussed in following did not work well with bookdown and if PDF & HTML are needed. Using table caption on R markdown file using knitr to use in pandoc to convert to pdf

Help and recommendations are very much appreciated!

mavericks
  • 1,005
  • 17
  • 42
  • I am not sure what your actual question is, but this might be relevant: https://stackoverflow.com/questions/46318658/how-do-i-create-a-huxtable-table-caption-using-bookdown-in-rmarkdown – dash2 Oct 18 '18 at 10:51
  • Thank you very much @dash2 for your reply. My aim is to generate huxtables with labels and captions, which work for both HTML and LaTeX output. In HTML I can use `set_caption('(#tab:setcaption) my caption')` which works without problems. In LaTeX/PDF I can use `set_caption('(\\#tab:setcaption) caption')` – which works for one figure only. If I include two figures I get `! Package caption Error: \caption outside float.` Also, `set_label()` from the huxtable package does not help much as it does not work in bookdown AFAIK. Anyone have an idea, maybe on how to circumvent the float error? – mavericks Oct 23 '18 at 17:26
  • also `hux <- add_footnote(hux, 'my footnote')` is not working in bookdown, yielding the following error: `Error in if (! Attr (input, "format")% in% c ("html", "latex")) {:   Argument has length 0` – mavericks Oct 23 '18 at 17:58
  • 1
    See https://github.com/hughjonesd/huxtable/issues/87 – dash2 Oct 25 '18 at 19:31

1 Answers1

1

If you upgrade to huxtable 4.3.0 (now on CRAN), it automatically takes care of bookdown table captions for you. Here's a short example:

---
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")

```

Not everything is perfect. If you set echo = TRUE you will need to manually insert \usepackage[table]{xcolor} before \usepackage{fancyvry} in the TeX header.

dash2
  • 2,024
  • 6
  • 15