Maximilian
Maximilian

Reputation: 4229

knitr Markdown LateX like table within HTML document

I would like to produce "LaTeX-like" table within an HTM document using knitr markdown (.Rmd) through:

knitr::knit2html(input="D:/...Rmd", output="D:/...report.html")

Here is an example. However, if I decided to produce a report, the LaTeX table would be incorrect:

library(xtable)

xtabl <- xtable(head(CO2))
print(xtabl, type="latex", include.rownames=FALSE)

The above gives:

enter image description here

As suggested here is the result. It was NOT a "LaTeX-like" table!

xtabl <- xtable(head(CO2))
print.xtable(xtabl, type="html", include.rownames=FALSE)

enter image description here

EDIT:

What I mean by "LaTeX-like" table is this:

enter image description here

Upvotes: 5

Views: 1138

Answers (3)

eipi10
eipi10

Reputation: 93761

Here's an example of a basic table with htmlTable:

---
title: "Untitled"
author: "Author"
date: "2/5/2017"
output: html_document
---

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

```{r}
library(htmlTable)
```

```{r, results="asis"}
tab = cbind.data.frame(
  sapply(iris[1:5 , sapply(iris, is.numeric)], function(x) sprintf("%1.1f", x)),
  Species=iris$Species[1:5]
  )

htmlTable(tab, rnames=FALSE, align="rrrrr", align.header="rrrrr",
          css.cell = c(rep("padding-left: 5em", 4), "padding-left: 2em"))
```

enter image description here

Upvotes: 0

Imran Ali
Imran Ali

Reputation: 2279

I have used knitr::kable for producing the desired tables.

mydata <- data.frame(SrNo=c(1,2,3,4), websites=c("stackoverflow", "twitter", "facebook", "google"))
knitr::kable(mydata)

kable function accepts a format argument with possible values latex, html, etc. see the documentation for details

Complete Markdown file

---
title: "kable"
author: "Imran Ali"
date: "February 6, 2017"
output: pdf_document
---

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

```{r, echo=FALSE}
mydata <- data.frame(SrNo=c(1,2,3,4), websites=c("stackoverflow", "twitter", "facebook", "google"))
knitr::kable(mydata)
```

Upvotes: 0

Philipp Merkle
Philipp Merkle

Reputation: 2605

The R Markdown cheat sheet provides a visual comparison of libraries kable, xtable and stargazer. Stargazer could be what you are looking for.

Also have a look into the htmlTable package.

Further customizations could be made with a custom CSS file.

Upvotes: 1

Related Questions