2

I appreciate that what I'm trying to do will sound silly, but please bear with me. I want to insert an existing image (a PNG) of a table into an RMarkdown document that will be turned into a pdf. Is there any way I can do this and have the image treated as a table for numbering purposes? That is, obviously I could do

![A caption for my table](my_table_as_image.png)

but the problem is that it will be numbered as e.g. Figure X, not Table Y. I would like it to be named/numbered as a table.

Thank you for any advice.

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

1 Answers1

3

Looks like I got to a way to do it with kable with some inspiration...this still has two horizontal lines but that's ok for the moment:

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

```

Edit: as suggested by @HoneyBuddha, when using Bookdown, you must add the flag format="pandoc", i.e. knitr::kable(temp.mat, caption="This is my caption", format="pandoc")

Ben S.
  • 3,415
  • 7
  • 22
  • 43
  • This would be such an amazing solution, but unfortunately, it prints not only the two horizontal lines BUT also the file path in between those horizontal lines - i.e. you see this right above the table image: ![](mytable.png) along with the image – HoneyBuddha Apr 30 '20 at 15:37
  • 1
    Just checked, that does not happen on my machine: https://imgur.com/A50B4v7 – Ben S. May 01 '20 at 18:50
  • 1
    You are correct. It works in a regular knitr document, but it does not appear to work in bookdown, where I get the same output i.e. the filepath, but no table. – HoneyBuddha May 04 '20 at 01:13
  • 1
    If you want it to work in Bookdown, I discovered that you must add the following explict parameter to format in padoc: knitr::kable(temp.mat, caption="This is my caption - Pandoc", format = "pandoc") after this, you can refer to it using the bookdown standard: \@ref(tab:chunklabel) – HoneyBuddha May 04 '20 at 02:41
  • Interesting...nice find. – Ben S. May 05 '20 at 02:04
  • 1
    If it is possible for you to update the answer to include that find, it might save someone heartache, since comments are not usually reviewed as carefully as the responses themselves. Thanks again for the great workaround. – HoneyBuddha May 05 '20 at 03:12