2

Is it possible to add an up or down arrow in a kable cell, with a pdf_output? Has anyone had luck adding any special figures into a kable cell?

My header:

---
title: "markdownTest"
header-includes:
- \usepackage{booktabs}
- \usepackage{longtable}
- \usepackage{array}
- \usepackage{multirow}
- \usepackage{wrapfig}
- \usepackage{float}
- \usepackage{colortbl}
- \usepackage{pdflscape}
- \usepackage{tabu}
- \usepackage{threeparttable}
- \usepackage{threeparttablex}
- \usepackage[normalem]{ulem}
- \usepackage{makecell}
- \usepackage{xcolor}
- \usepackage{fontspec}
- \setmainfont{Calibri}
output: 
  pdf_document:
    latex_engine: xelatex
---
David
  • 432
  • 3
  • 10

1 Answers1

2

You can run the following statements:

---
title: "kable arrow"
author: "Alessio Benedetti"
date: "17/11/2019"
output: html_document
---

```{r}
library(knitr)
df <- data.frame(
  stock = c('Apple','Amazon','Microsoft','Tesla'),
  value = c(300,200,150,250),
  difference = c(50,-10,20,-15)
)
df$trend <- ifelse(df$difference >0,
                   '![](https://www.picgifs.com/glitter-gifs/a/arrows/picgifs-arrows-110130.gif)',
                   '![](https://www.picgifs.com/glitter-gifs/a/arrows/picgifs-arrows-30184.gif)'
                  )

kable(df)
```

enter image description here

EDIT: for knitting in pdf you need to use png files (eg: https://images.app.goo.gl/1Qt85stUQggdPRwx7):

---
title: "kable arrow"
author: "Alessio Benedetti"
date: "17/11/2019"
output:
  pdf_document: default
  html_document: default
---

```{r}
library(knitr)
df <- data.frame(
  stock = c('Apple','Amazon','Microsoft','Tesla'),
  value = c(300,200,150,250),
  difference = c(50,-10,20,-15)
)
df$trend <- ifelse(df$difference >0,
                   '![](arrow_up.png)',
                   '![](arrow_down.png)'
                  )

kable(df)
```

enter image description here

Alessio
  • 910
  • 7
  • 16
  • Thanks a ton for this response. I should have mentioned I need it to be a pdf_output. If I try running this with pdf_output in my header and with 'latex' in the kable function, I get the picgifs url as a text result in the trend column. Is there a way to include these types of graphics with a pdf_output? – David Nov 18 '19 at 19:19
  • 1
    Gifs do not work in latex, you need to use for instance png pics (I tried by downloading this arrow https://images.app.goo.gl/1Qt85stUQggdPRwx7, resizing it, and creating two distinct files arrow_up.png and arrow_down_png. With the edited portion of the code, I was able to knit the pdf. – Alessio Nov 18 '19 at 22:16
  • I'm trying to run this but am getting this error: Error: '\[' is an unrecognized escape in character string starting "'!\[". I tried adding escape = F but it still produces the same error. I also removed the \, as shown in your code output but then it just prints the code as text. What am I missing here, apologies as I'm still a beginner as you can probably tell. – David Nov 18 '19 at 22:50
  • 1
    sorry, guess a bad copy/paste. Can you try again? Just edited the code. – Alessio Nov 18 '19 at 23:52
  • When I copy your header and run the code it works, however when I use my header (which is now edited to be in the original post), it outputs the code as text in the file. Any idea why this is? – David Nov 19 '19 at 00:04
  • can you check your tinytex installation? (https://yihui.org/tinytex/) – Alessio Nov 19 '19 at 00:12
  • I followed these instructions when I first installed, I'm going to try to reinstall to see if that helps. I found that my header works if I remove "- \usepackage{fontspec} - \setmainfont{Calibri}", "latex_engine: xelatex", and add pdf_output: default. Hmm, maybe setting a main font prohibits adding pngs? I'll continue to investigate, but thanks a lot for all of your help. – David Nov 19 '19 at 00:20
  • @alessio for me your code works fine in a normal LaTex file, but not in a PDF generated with Bookdown or [huskydown](https://github.com/benmarwick/huskydown). It simply prints the PNG paths in the table, like `![](arrow_up.png)` Would you have any advice? – mavericks Jan 09 '20 at 18:20
  • @mavericks maybe you can use `knitr::include_graphics("arrow_down.png")` statement? (§2.4 of https://bookdown.org/yihui/bookdown/figures.html) – Alessio Jan 14 '20 at 13:09
  • @alessio Unfortunately this is not working for me, see also my Q here: https://stackoverflow.com/questions/60058692/table-in-bookdown-huskydown-with-several-features-citation-caption-url-png-f/60058693#60058693 – mavericks Feb 04 '20 at 14:00