Table placement with stargazer and knitr

痞子三分冷 提交于 2019-12-06 04:20:02

问题


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.


回答1:


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




回答2:


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 " 


来源:https://stackoverflow.com/questions/14937026/table-placement-with-stargazer-and-knitr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!