Inserting Stargazer or Xable Table into Knitr Document

inserting stargazer or xable table into knitr document

Do you mean something like this (results='asis')?

\documentclass[11pt]{article}
\begin{document}

<<setup, echo = FALSE, results= 'hide', message = FALSE>>=
data(mtcars)

library(ggplot2)
qplot(speed, dist, data = cars) + geom_smooth()
@

<<results='asis'>>=
require(stargazer)
stargazer(mtcars, summary = FALSE)
@

\end{document}

Using stargazer with Rstudio and Knitr

Since the topic has gone a bit stale, I'll assume the issue at hand is to somehow use stargazer with knitr, and not per se the conversion of the stargazer objects into HTML.

Being an avid fan of stargazer, I have come up with the following workflow:

  1. Write my code in an .Rmd file.
  2. Knit it into .md. Stargazer tables remain as LaTeX code in the resulting markdown file.
  3. Use pandoc to convert the markdown file to PDF. Pandoc translates the LaTeX code into proper tables. Alternatively, one can use LyX with knitr plugin to get stargazer tables nicely output in PDF format.

If one wants stargazer tables in MS Word, the best way I have found is to use LaTeX2RTF. Although the very top cells are distorted a bit, fixing it is a matter of removing an erroneous empty cell. For the rest the table is preserved and can be pasted/imported into Word.

These two strategies help use stargazer outside LaTeX. Hope it helps.

Table placement with stargazer and knitr

Starting with version 4.0 (available on CRAN now), you can easily adjust the table placement by using the table.placement argument.

Remove Hashes in R Output from R Markdown and Knitr

You can include in your chunk options something like

comment=NA # to remove all hashes

or

comment='%' # to use a different character

More help on knitr available from here: http://yihui.name/knitr/options

If you are using R Markdown as you mentioned, your chunk could look like this:

```{r comment=NA}
summary(cars)
```

If you want to change this globally, you can include a chunk in your document:

```{r include=FALSE}
knitr::opts_chunk$set(comment = NA)
```

Align multiple tables side by side

Just put two data frames in a list, e.g.

t1 <- head(mtcars)[1:3]
t2 <- head(mtcars)[4:6]
knitr::kable(list(t1, t2))

Note this requires knitr >= 1.13.



Related Topics



Leave a reply



Submit