5

I have a table that I'm trying to put into a pdf with R Markdown.

dt <- c(name = c("a", "b", "c"),
        money = c("$1", "$10", "$100")

dt %>%
kable(format = "latex") %>%
row_spec(1, background = "blue")

The above code does not work, producing the following error:

Error in stri_replace_first_regex(string, pattern, fix_replacement(replacement), : Trying to access the index that is out of bounds. (U_INDEX_OUTOFBOUNDS_ERROR) Calls: ... row_spec_latex -> str_replace -> stri_replace_first_regex ->. Call

If I remove the row_spec portion, it works. If I remove the dollar signs, it works. If I change the row to be row 0 instead of row 1, it works. But I cannot change the colors of rows with dollar signs in them. I know that $ is a reserved character in latex, but as an experiment I looked up all the reserved characters and tried it with them instead of the dollar sign and everything worked. Dollar sign is the only one that is giving me trouble.

Is there some way to make this work or am I doomed to having a stark white table?

C.garner
  • 53
  • 4
  • 1
    `$` has a meaning and you should "escape" it with a backslash. Try `\$` or `\\$` or even more backslashes. – jay.sf Feb 02 '18 at 21:30
  • I have tried that and it didn't change much. I believe `kable` automatically puts the \ in to escape it. – C.garner Feb 02 '18 at 21:32
  • Perhaps using HTML code works. Try `&dollar;` or `$` instead of `$` – jay.sf Feb 02 '18 at 21:33
  • Both of those just displayed as `&dollar;` and `$` and didn't get changed to `$` – C.garner Feb 02 '18 at 21:38
  • What about `kable(format = "latex", escape = TRUE)` ? – jay.sf Feb 02 '18 at 21:49
  • I tried both `escape = TRUE` and `escape = FALSE`. Both produced the same error. – C.garner Feb 02 '18 at 21:52
  • I just tried running your code with `\\$` and specifying `escape=F` and it worked for me. Did you try both of those suggestions together? – Grace Mahoney Feb 02 '18 at 22:34
  • I just tried your suggestion and I'm still getting the same error. You have it displaying a dollar sign in a row with a blue background? – C.garner Feb 02 '18 at 22:43
  • @GraceMahoney the `kableExtra::row_spec` seems to dislike the `$` somehow though it is escaped. `\textdollar` worked finally. See my anywer. – jay.sf Feb 02 '18 at 23:55

1 Answers1

2

We can use the latex code \textdollar for the dollar sign and escape it. Additional using kable() option escape=FALSE.

---
title: "Untitled"
author: "C.garner"
date: "2 Februar 2018"
output: 
  pdf_document: 
    keep_tex: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r table1, message=FALSE}
library(dplyr)
library(kableExtra)
library(knitr)
dt <- c(name = c("a", "b", "c"),
        money = c("\\textdollar 1", "\\textdollar 10", "\\textdollar 100"))

dt %>%
kable(format = "latex", escape = FALSE) %>%
row_spec(4, background = "blue")
```

enter image description here

jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • The problem isn't that I can't get the table to print, it's that I can't get it to print with color. I need the ability to highlight rows different colors for a report. As stated in the problem, if I remove the `row_spec` line, the table prints just fine. – C.garner Feb 02 '18 at 22:16
  • In my code I have `dt <- data.frame` where as you use `dt <- c`. If you change your `row_spec` number to 4, you will get the same error I was getting. I can highlight rows that do not have a `$` in them, but not ones with. – C.garner Feb 02 '18 at 22:37