Reputation: 726
I am using kableExtra
to try and output a LaTeX table but when I knit my code to html
, the table does not output. It just outputs junk that I don't want. Example code is below, and a result of the output also:
```{r, echo=FALSE}
library(dplyr)
library(kableExtra)
library(knitr)
x <- seq(1,9,by = 1)
kable(x,format = 'latex', booktabs = T, caption = "A KnitR Kable")
```
Output in html
file:
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
```
What is going wrong?
Upvotes: 1
Views: 4672
Reputation: 7826
Well, as the author of kableExtra, I feel like I need to do some clarification here. When you are using knitr::kable
alone, what @JonGrub said is absolutely right. format = "markdown"
in kable is the default and you will let pandoc determine the format of your table for you. However, if you are using kableExtra
, which only works on either LaTeX or HTML table, those markdown outputs won't work. That's why in the past, you need to specify format
in your kable call.
A few months ago, I realized that people have the need to generate a table that works in both HTML and LaTeX at the same time, especially with the popularity of bookdown
. As a result, I added something to kableExtra in its 0.9.0 that it will automatically identify if it's used in a LaTeX environment. If so, it will set the default kable
formats to be "latex". Otherwise, you will get a HTML table (if you are using it in a regular R session, you will see its preview in RStudio). This little trick allows you to get rid of those format="xxx"
calls. It allows you to generate customized table output using the same piece of code that works in both HTML and LaTeX.
Upvotes: 2
Reputation: 12420
As some of the comments pointed out the problem here is that you are using format = 'latex'
while you are using R-markdown to knit to a html
report. R-markdown uses the markdown language though instead of Latex when producing html
.
The picture looks different when you produce a PDF since the process then produces a Latex document first which is compiled with latex.
To get the chunk, you will thus have to choose format = 'markdown'
. This will work for both html and PDF. However, I would suggest you don't specify the format at all since rmarkdown
will choose the format for you. The second problem you mention is that dplyr
startup message is shown. You can turn this off using message = FALSE
in your chunk options. So I would suggest:
```{r echo = FALSE, message = FALSE}
library(dplyr)
library(kableExtra)
library(knitr)
x <- seq(1, 9, by = 1)
kable(x, booktabs = TRUE, caption = "A KnitR Kable")
```
Another good tip is to try using the render()
command from rmarkdown
if you have problems using knit from RStudio directly. It often makes errors more explicit. This is the syntax:
rmarkdown::render("C:/path/to/file.Rmd", output_format = "all")
Note, that you can specify the output file format here. All means that all output formats specified in your header will be produced. For example:
---
title: ''
output:
pdf_document: default
html_document: default
---
In this case the above command will output a html as well as PDF file. If you can't output PDF files, most likely you are missing a latex installation. I second @Tung recommendation of tinytex
, but you can also look into Miktex or Texlive.
Edit: Thanks to @camille's comment I realised format = 'latex'
works fine when knitting to PDF and will only misbehave when knitting to html.
Upvotes: 0