How to display a table from htmlTable package in pdf_document in rmarkdown?

In rmarkdown (in RStudio), I use the htmlTable package to create beautiful tables in my html documents. Now I want to get the same result when rendering a pdf document. Tables are not displayed properly. How can I let rmarkdown generate tables in my pdf as well as in my html?

This is a working example of a .Rmd file with the following table:

---
title: "test"
output: pdf_document
---

```{r results="asis"}
library(htmlTable)
c1 <- c("test1","test1","test2","test2")
c2 <- c(1,2,3,4)
data_object <- as.data.frame(cbind(c1,c2))
names(data_object) <- c("test","test2")
print(htmlTable(data_object))
```

Click the pdf insert in RStudio.

Result in my pdf document:

test
library(htmlTable)
c1 <- c("test1","test1","test2","test2")
c2 <- c(1,2,3,4)
data_object <- as.data.frame(cbind(c1,c2))
names(data_object) <- c("test","test2")
print(htmlTable(data_object))
test
test2
1
1
1
2
1
2
3
2
3
4
2
4
1

The result (parts of the table) should be:

enter image description here

Does anyone have an idea on how to solve this?

+4
source share
1 answer

If you want PDF output, you must convert the R object to

  • LaTeX

, HTML PDF ( ). htmlTable kable pander :

---
title: "test"
output: pdf_document
---

```{r}
library(pander)
data_object <- data.frame(test = paste0('test', 1:4), test2 = 1:4)
pander(data_object)
```

PDF- rmarkdown::render :

enter image description here

+2

Source: https://habr.com/ru/post/1611929/


All Articles