6
votes

I have a knitr document with a table of regression results as output by stargazer, like so:

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

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

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam eleifend molestie nisi, id scelerisque orci venenatis imperdiet. Fusce dictum congue faucibus. Phasellus mollis bibendum tellus eu interdum. Nam sollicitudin congue fringilla. Donec rhoncus viverra lorem vel molestie. Ut varius facilisis ante, a pretium arcu feugiat in. Maecenas sagittis accumsan massa. Pellentesque sollicitudin odio non odio elementum vel tristique dui mattis. Pellentesque tempus feugiat magna, a pharetra ipsum posuere ac. Donec fringilla ligula nec tellus egestas dictum. Vestibulum sit amet sem elit. Vestibulum nibh purus, pulvinar nec hendrerit sollicitudin, posuere ac mi. Cras mollis lorem ac mauris pellentesque elementum. In venenatis laoreet ligula.

<<echo=FALSE, results='asis', comment=NA>>=
model1 <- lm(mpg ~ gear, data=mtcars)
stargazer(model1)
@
\end{document}

How do I influence the placement of this table within the document or, in other words, how do I pass a position specifier to the tabular environment stargazer generates? I've looked through the manual but came up empty.

2

2 Answers

5
votes

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

2
votes

One way of going about this is by replacing the placement argument using regular expressions.

If you check the stargazer output, you will notice that the default is

[4] "\\begin{table}[htb] \\centering " 

You can find htb and replace it with your argument. Here's one way

x <- stargazer(model1)
gsub("\\[htb\\]", "[h]", x)
 [4] "\\begin{table}[h] \\centering "