1

I'm trying to knit a stargazer table to pdf in R, and I'm having an issue with an ampersand (&) in one of the cells. I want to keep the ampersand (because it is a business name and affects order).

Here's the code i'm using:

```{r results='asis'}

old_df <-  bus_df %>%
  mutate(char_date = as.character(clean_start_date)) %>% 
  mutate(business_name= str_replace_all(business_name,"&","\\\\&")) %>% 
  select(business_name, char_date) %>%
  slice(1:5) 

old_df %>% 
  stargazer(summary=FALSE, header=FALSE, title="Oldest 5 Busiensses")
```

First row in data:

A & A BUILDING MATERIAL CO | 1921-01-01

Error:

>! Extra alignment tab has been changed to \cr.
><recently read> \endtemplate 
>                             
>l.217 1 & A & A BUILDING MATERIAL CO &
>
>pandoc: Error producing PDF from TeX source
>Error: pandoc document conversion failed with error 43
>Execution halted

I excluded commented out the code that I tried to add escape characters to the ampersand, but any number of slashes (up to 6) returns an error. 1, 3, and 5 breaks the code chunk, while 2, 4, and 6 break the knit. 2 doesn't change the character in the cell (remains &) while 4 and 6 insert the slash ('\&'), but the slash breaks the knit.

With for slashes:

First row in data:

A \& A BUILDING MATERIAL CO | 1921-01-01

Error:

>! Extra alignment tab has been changed to \cr.
><recently read> \endtemplate 
>                             
>l.217 ...\textbackslash & A BUILDING MATERIAL CO &
>
>pandoc: Error producing PDF from TeX source
>Error: pandoc document conversion failed with error 43
>Execution halted

Let me know if you need more detail!

Thanks!

Jonathan
  • 11
  • 1
  • I think see http://stackoverflow.com/questions/29661199/does-stargazer-interpreting-data-frame-data-as-latex-code-constitute-a-bug-or-is – MichaelChirico Nov 02 '16 at 00:57
  • If you aren't married to using `stargazer`, `xtable` will sanitize entries, making the ampersand a non-issue (entry can just be "&", and it will place backslash in LaTeX output). – Tad Dallas Nov 02 '16 at 01:02
  • @MichaelChirico thanks! It's interesting that the example you found successfully output, but with data in the wrong columns. Your response is verified by Tad's: it's not possible. I'm appreciative that you found that post; I tried but was evidently using the wrong search parameters. – Jonathan Nov 03 '16 at 22:05
  • @TadDallas, Thank you for your response. I'm happy to use xtable. – Jonathan Nov 03 '16 at 22:11
  • Cool. I've added it as an answer. Best of luck. – Tad Dallas Nov 04 '16 at 02:29
  • Thank you so much for pointing out this issue with using ampersand (&). I am using kableExtra and my tables would not knit. I was able to use "+" instead in my case, however. – Nova Sep 22 '18 at 16:50

1 Answers1

1

While stargazer is great for formatting model output, the xtable package provides the functionality you want for the formatting of data.frames, etc. The code chunk below should do the trick.

```{r results='asis'}

old_df <-  bus_df %>%
  mutate(char_date = as.character(clean_start_date)) %>% 
  select(business_name, char_date) %>%
  slice(1:5) 

xtable(old_df) 

```
Tad Dallas
  • 1,179
  • 5
  • 13