3

I know, this is probably not the wisest idea anyways, but I need to insert a table as figure/screenshot (.png) but refer to it as a table in the caption. Is it possible?

My gaol is basically the same as here with the only difference that I am working in RStudio with rmarkdown, knitr and bookdown. Ideally, the solution should work for both PDF and HTML output (though, PDF is more important for me now).

zx8754
  • 52,746
  • 12
  • 114
  • 209
ikashnitsky
  • 2,941
  • 1
  • 25
  • 43
  • AFAIK it is not possible with bookdown. Sorry. – Yihui Xie Dec 27 '17 at 19:38
  • @YihuiXie No way! You are a wizard, aren't you))) I never thought there exists impossible latex/rmd task for you. The world will never be the same. Thanks for your immediate reply! – ikashnitsky Dec 27 '17 at 19:43
  • @YihuiXie do you think it makes sense to file an issue to `bookdown`? Or would it be a too non-general-interest feature request? – ikashnitsky Dec 27 '17 at 19:52
  • 1
    Well, it might be possible if I work hard enough on it, but life is all about priorities -- I cannot afford priority on this one. I can understand it may be important to you, but [I have to say no sometimes.](https://yihui.name/en/2017/11/on-saying-no/) – Yihui Xie Dec 27 '17 at 20:15
  • Luckily it's not a matter of life for me. I understand your position and appreciate frankness. But I guess it might be of general use to have a sort of `include_table()` function. Especially since PDF table is already imported (with `include_graphics()`) as vector/text object. So it's only a matter of caption before one can use a manually layouted and saved as PDF table in rmd – ikashnitsky Dec 27 '17 at 20:24
  • Actually that sounds like a good idea to me! So please go ahead and submit a pull request! You can (and probably should) take advantage of existing code in `knitr::kable()`: https://github.com/yihui/knitr/blob/master/R/table.R Good luck! – Yihui Xie Dec 27 '17 at 21:04
  • @YihuiXie what about the approach I posted below? It works although there's still \toprule and \bottomrule making horizontal lines -- any way to turn those off? – Ben S. Apr 12 '19 at 15:11

2 Answers2

1

As a hack, you could create a dummy table that's printed with a miniscule fontsize, just to get the table caption, and then in the same chunk add the actual table image that you want printed. For example:

---
title: Document Title
output: 
  bookdown::pdf_document2:
    toc: no
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(xtable)
options(xtable.include.rownames=FALSE, xtable.comment=FALSE)

# Dummy table function
dt = function(label, caption=NULL) {
  print(xtable(setNames(data.frame(x=numeric()), " "),
               caption=caption,
               label=paste0("tab:", label)), 
        hline.after=NULL,
        booktabs=FALSE,
        size="\\fontsize{0.1pt}{0.1pt}\\selectfont")
}
```

Lorem Ipsum is simply dummy text of the printing and typesetting industry. As you can see, Table \@ref(tab:lab1) shows something. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. 

```{r, lab1, results="asis", fig.align="center", out.width="6in"}
dt("lab1", "This is table 1")
include_graphics("tab1.png")
```

Now for some more text and then here is Table \@ref(tab:lab2).

```{r, lab2, results="asis", fig.align="center", out.width="4.5in"}
dt("lab2", "This is table 2")
include_graphics("tab2.png")
```

Below, you can see what the output document looks like. As you can see, there some extra space between the caption and the table, due to the small amount of vertical space taken up by the invisible dummy table. Hopefully, some with better knowledge of latex can suggest how to get rid of that space.


enter image description here

eipi10
  • 91,525
  • 24
  • 209
  • 285
  • Thanks a lot! This is definitely a (not too elegant) working solution – ikashnitsky Dec 27 '17 at 21:55
  • The only problem I see in this solution is that the caption is linked to the nonexistent table and not the image. Hence, if the image is rather big and jumps to the nest page, the result may look like [this](https://habrastorage.org/webt/xv/79/gr/xv79grltpyeaoayr5bydngfjkvm.png) – ikashnitsky Dec 27 '17 at 22:19
  • 2
    Yes. You either need to add some latex tags to ensure they are automatically kept them together or add `\newpage` just before the chunk where the bad page break occurs. – eipi10 Dec 27 '17 at 22:22
  • As I mentioned in my last comment under the question, if you study the source code of `knitr::kable()` closely enough, you can come up with a more elegant and portable solution. I'm never a fan of xtable... – Yihui Xie Dec 28 '17 at 03:06
  • @YihuiXie, I just came up with this as a second-best (or maybe third-best) approach until @ikashnitsky's `include_table()` idea is implemented. – eipi10 Dec 28 '17 at 06:13
  • @eipi10 Totally understood! Thanks! – Yihui Xie Dec 28 '17 at 06:17
  • @eipi10 can you share the second-best solution as well. I'm not sure if I can implement the outlined `include_table()` quickly enough. Meanwhile, your given solution doesn't seem to work nicely with tufte margin elements. Here is [how it looks](https://habrastorage.org/webt/fk/vc/es/fkvcessojl-mi_vrikdfrjcdn2c.png) – ikashnitsky Dec 28 '17 at 20:02
  • That was a joke. My answer is the only hack I can think of for this issue. I haven't used the Tufte handout template before and I'm not sure why it's not formatting properly. – eipi10 Dec 29 '17 at 18:23
1

I had this same issue and didn't see this question at the time. If you're OK with still having having the horizontal lines at top and bottom, what you can do is make a table that contains nothing but an image of the table you want to display, like this:

```{r echo=F, warning=F}
temp.df <- data.frame(image="![](mytable.png)")
temp.mat <- as.matrix(temp.df)
colnames(temp.mat) <- NULL
knitr::kable(temp.mat, caption="This is my caption")

```

Still a hack but a little less work than the currently accepted answer.

Ben S.
  • 3,415
  • 7
  • 22
  • 43