Simple Manual Rmarkdown Tables That Look Good in HTML, PDF and Docx

Simple manual RMarkdown tables that look good in HTML, PDF and DOCX

Inspired by daroczig's comments, especially the clue that pander translates to pandoc's pipe syntax, I took a closer look at the pander documentation and found reference to cat. After some experimentation, I found the winner:

```{r table2, echo=FALSE, message=FALSE, warnings=FALSE, results='asis'}
tabl <- "
| Tables | Are | Cool |
|---------------|:-------------:|------:|
| col 3 is | right-aligned | $1600 |
| col 2 is | centered | $12 |
| zebra stripes | are neat | $1 |
"
cat(tabl) # output the table in a format good for HTML/PDF/docx conversion
```

This produces uniformly good looking tables in HTML, PDF and docx in my tests. Now I'm off to upvote daroczig on some other questions to thank him for getting me to the solution.

If you need a caption for your table... then you'll need to do it a bit differently. Note that the caption will only be visible in the PDF, not in the HTML:

```{r table-simple, echo=FALSE, message=FALSE, warnings=FALSE, results='asis'}
require(pander)
panderOptions('table.split.table', Inf)
set.caption("My great data")
my.data <- " # replace the text below with your table data
Tables | Are | Cool
col 3 is | right-aligned | $1600
col 2 is | centered | $12
zebra stripes | are neat | $1"
df <- read.delim(textConnection(my.data),header=FALSE,sep="|",strip.white=TRUE,stringsAsFactors=FALSE)
names(df) <- unname(as.list(df[1,])) # put headers on
df <- df[-1,] # remove first row
row.names(df)<-NULL
pander(df, style = 'rmarkdown')
```

Regression Tables in R Markdown / rmarkdown (html/pdf)

Here is a proposition: make a function that checks the output format and then uses either stargazer or texreg depending on this. We use opts_knit$get("rmarkdown.pandoc.to") to check the output format.

---
output: html_document
---

```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(echo = TRUE)
rmd_format <- opts_knit$get("rmarkdown.pandoc.to")
## returns "html" or "latex"

```

```{r}

report_regression <- function(model, format, ...){
if(format == "html"){
require(texreg)
htmlreg(model, custom.note="%stars. htmlreg", ...)
} else if(format == "latex"){
require(stargazer)
stargazer(model, notes="stargazer html", ...)
} else {
print("This only works with latex and html output")
}
}
```

```{r table, results = "asis"}
library(car)
lm1 <- lm(prestige ~ income + education, data=Duncan)

report_regression(lm1, format = rmd_format)
```

How to make PDF output and code output look right in Rmarkdown

If I understood correctly, you can use paste0.

caption = paste0("The mode and 95% HPD intervals are the dot", 
" and horizontal line at the bottom, respectively.")

Running pandoc/rmarkdown with openout_any=a

For anyone interested in the future:

It turned out to be as simple as running openout_any=a + your compilation statement. For example:

openout_any=a Rscript -e "library(rmarkdown); rmarkdown::render('.hello.Rmd', 'pdf_document')"

Hope it helps.



Related Topics



Leave a reply



Submit