使用Rstudio和Knitr的观星者

时间:2021-03-04 06:12:56

I'm struggling to use stargazer output in knitr, using RStudio. For example, I paste the code below into a .Rmd file, then click Knit HTML. The first block between [ and ] is rendered as equations. The second block is from stargazer. It remains as code. When I paste the second block, less [ and ], into a Sweave file and then click compile as PDF, the code renders as a table. I have MikTex installed and version 3 of Stargazer.

我正在努力使用RStudio在knitr中使用stargazer输出。例如,我将下面的代码粘贴到.Rmd文件中,然后单击Knit HTML。 [和]之间的第一个块呈现为方程式。第二个街区来自观星者。它仍然是代码。当我将第二个块(少于[和]]粘贴到Sweave文件中然后单击“编译为PDF”时,代码将呈现为表格。我安装了MikTex和Stargazer的第3版。

The answer inserting stargazer or xable table into knitr document works for me in a Sweave file (Rnw) when clicking compile PDF. In an Rmd file, the tex is not rendered when clicking Knit HTML.

在单击编译PDF时,将stargazer或xable表插入knitr文档的答案适用于Sweave文件(Rnw)。在Rmd文件中,单击Knit HTML时不会呈现tex。

How can I put stargazer output into a Rmd file so that Knit HTML converts the latex code to a table? (I'm new to Latex, and am not sure what code I can delete, so apologise for the long segment.)

如何将stargazer输出放入Rmd文件中,以便Knit HTML将乳胶代码转换为表格? (我是Latex的新手,我不确定我可以删除哪些代码,所以请为长段道歉。)

\[
\begin{aligned}
\dot{x} & = \sigma(y-x) \\
\dot{y} & = \rho x - y - xz \\
\dot{z} & = -\beta z + xy
\end{aligned}
\]

\[
\documentclass{article}

\begin{document}


% Table created by StarGazer v.3.0.1 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Sun, Feb 03, 2013 - 11:34:52 AM
\begin{table}[htb] \centering 
  \caption{} 
  \label{} 
\footnotesize 

\begin{tabular}{@{\extracolsep{5pt}}lc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
 & \multicolumn{1}{c}{\textit{Dependent variable:}} \\ 
\cline{2-2} 
\\[-1.8ex] & Rate \\ 
\hline \\[-1.8ex] 
 pole & $0.071^{***}$ \\ 
  & $(0.020)$ \\ 
  & \\ 
 post & $0.095^{***}$ \\ 
  & $(0.019)$ \\ 
  & \\ 
 Constant & $-5.784^{***}$ \\ 
  & $(1.667)$ \\ 
  & \\ 
\hline \\[-1.8ex] 
Observations & $46$ \\ 
Residual Std. Error & $2.378 (df = 43)$ \\ 
\hline 
\hline \\[-1.8ex] 
\textit{Note:}  & \multicolumn{1}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\ 
\normalsize 
\end{tabular} 
\end{table} 

\end{document}
\]

4 个解决方案

#1


9  

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.

由于主题有点过时,我会假设手头的问题是以某种方式使用带有编织器的观星者,而不是将观星者对象转换为HTML。

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

作为观星者的*粉丝,我提出了以下工作流程:

  1. Write my code in an .Rmd file.
  2. 将我的代码写在.Rmd文件中。
  3. Knit it into .md. Stargazer tables remain as LaTeX code in the resulting markdown file.
  4. 将它编织成.md。 Stargazer表在结果markdown文件中保留为LaTeX代码。
  5. 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.
  6. 使用pandoc将markdown文件转换为PDF。 Pandoc将LaTeX代码转换为适当的表。或者,可以使用带有knitr插件的LyX来获得以PDF格式输出的观星表。

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.

如果想在MS Word中使用stargazer表,我发现的最好方法是使用LaTeX2RTF。虽然最顶部的单元有点扭曲,但修复它是一个删除错误的空单元的问题。对于其余的表,表将被保留,并可以粘贴/导入到Word中。

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

这两种策略有助于在LaTeX之外使用观星者。希望能帮助到你。

#2


16  

Use the following code and you get a working version

使用以下代码,您将获得一个工作版本

{r, results='asis'} stargazer(model)

{r,results ='asis'} stargazer(型号)

When converting to pdf, the following code works perfectly for stargazer 4.0:

转换为pdf时,以下代码适用于stargazer 4.0:

{r, results='asis'} stargazer(model, header=FALSE, type='latex')

{r,results ='asis'} stargazer(model,header = FALSE,type ='latex')

#3


4  

In addition to the previous answer, and maybe as a simpler solution, it is possible for stargazer to output the table in html code so that when the Rmd file is knitted into html, a table is created rather than the tex code. I believe that the stargazer function can now export directly to html by setting type = 'html'.

除了之前的答案,也许作为一个更简单的解决方案,stargazer可以在html代码中输出表格,这样当Rmd文件编织成html时,会创建一个表而不是tex代码。我相信stargazer函数现在可以通过设置type ='html'直接导出到html。

So for example, given model fit lm1, you would use the following code in your Rmd file:

因此,例如,给定模型拟合lm1,您将在Rmd文件中使用以下代码:

stargazer(lm1, type = 'html')

stargazer(lm1,type ='html')

This works whether you want your final output to be html or pdf.

无论您希望最终输出是html还是pdf,这都有效。

#4


4  

Returning to this question.

回到这个问题。

I want to use the same markdown files to produce html and pdf outputs in RStudio with knitr. That is, in RStudio I want to push the knit button, and have the options of either knitting a HTMl output, or a pdf output. I have no great interest, at the moment, in knitting a word/OpenOffice document.

我想使用相同的markdown文件在带有knitr的RStudio中生成html和pdf输出。也就是说,在RStudio中我想推动编织按钮,并且可以选择编织HTMl输出或pdf输出。目前,我没有兴趣编织一个单词/ OpenOffice文档。

I used the amazingly useful stargazer cheatsheet from Jake Russ. This exercises most of stargazer's functions. It is an R MArkdown file, with the chunk option results='asis' set for those chunks producing stargazer output.

我使用了Jake Russ的惊人有用的观星者备忘单。这练习了大多数观星者的功能。它是一个R MArkdown文件,其中chunk选项results ='asis'为那些产生stargazer输出的块设置。

The stargazer command itself has an argument 'type'. The default is type='latex' In Jake Russ's cheatsheet, which is intended to produce a webpage, type='html' is used throughout.

stargazer命令本身有一个参数'type'。默认值为type ='latex'在Jake Russ的cheatsheet中,用于生成网页,始终使用type ='html'。

This does not work at all if you try to knit it into a pdf. Tables come out as long lists, one table cell per line, with no formatting, and occupying many pages, with no formatting.

如果您尝试将其编织为pdf,则根本不起作用。表格作为长列表,每行一个表格单元格,没有格式化,占用很多页面,没有格式化。

The smallest change that I can make to allow me to produce nice pdf's within RStudio is to globally replace all the

我可以做的最小的变化是让我在RStudio中生成漂亮的pdf是全局替换所有的

type='html'

with

type='latex'

(note that both occur in the text of the document, as well as in the stargazer commands, so care is needed!)

(请注意,两者都出现在文档的文本中,以及stargazer命令中,因此需要小心!)

This works! As far as I can see the pdf is a faithful replica of the webpage, which is exactly what I want.

这有效!据我所知,pdf是网页的忠实复制品,这正是我想要的。

Trying to knit OpenOffice documents, if I leave

如果我离开,试图编写OpenOffice文档

type='latex'

Each table in the output is replaced by this text:-

输出中的每个表都被此文本替换: -

% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu % Date and time: Tue, Sep 01, 2015 - 22:22:29

If I restore the

如果我恢复了

type='html'

then each table is written, one cell per line, down the side of the page with no formatting!

然后每个表被写入,每行一个单元格,在页面的一侧没有格式化!

#1


9  

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.

由于主题有点过时,我会假设手头的问题是以某种方式使用带有编织器的观星者,而不是将观星者对象转换为HTML。

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

作为观星者的*粉丝,我提出了以下工作流程:

  1. Write my code in an .Rmd file.
  2. 将我的代码写在.Rmd文件中。
  3. Knit it into .md. Stargazer tables remain as LaTeX code in the resulting markdown file.
  4. 将它编织成.md。 Stargazer表在结果markdown文件中保留为LaTeX代码。
  5. 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.
  6. 使用pandoc将markdown文件转换为PDF。 Pandoc将LaTeX代码转换为适当的表。或者,可以使用带有knitr插件的LyX来获得以PDF格式输出的观星表。

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.

如果想在MS Word中使用stargazer表,我发现的最好方法是使用LaTeX2RTF。虽然最顶部的单元有点扭曲,但修复它是一个删除错误的空单元的问题。对于其余的表,表将被保留,并可以粘贴/导入到Word中。

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

这两种策略有助于在LaTeX之外使用观星者。希望能帮助到你。

#2


16  

Use the following code and you get a working version

使用以下代码,您将获得一个工作版本

{r, results='asis'} stargazer(model)

{r,results ='asis'} stargazer(型号)

When converting to pdf, the following code works perfectly for stargazer 4.0:

转换为pdf时,以下代码适用于stargazer 4.0:

{r, results='asis'} stargazer(model, header=FALSE, type='latex')

{r,results ='asis'} stargazer(model,header = FALSE,type ='latex')

#3


4  

In addition to the previous answer, and maybe as a simpler solution, it is possible for stargazer to output the table in html code so that when the Rmd file is knitted into html, a table is created rather than the tex code. I believe that the stargazer function can now export directly to html by setting type = 'html'.

除了之前的答案,也许作为一个更简单的解决方案,stargazer可以在html代码中输出表格,这样当Rmd文件编织成html时,会创建一个表而不是tex代码。我相信stargazer函数现在可以通过设置type ='html'直接导出到html。

So for example, given model fit lm1, you would use the following code in your Rmd file:

因此,例如,给定模型拟合lm1,您将在Rmd文件中使用以下代码:

stargazer(lm1, type = 'html')

stargazer(lm1,type ='html')

This works whether you want your final output to be html or pdf.

无论您希望最终输出是html还是pdf,这都有效。

#4


4  

Returning to this question.

回到这个问题。

I want to use the same markdown files to produce html and pdf outputs in RStudio with knitr. That is, in RStudio I want to push the knit button, and have the options of either knitting a HTMl output, or a pdf output. I have no great interest, at the moment, in knitting a word/OpenOffice document.

我想使用相同的markdown文件在带有knitr的RStudio中生成html和pdf输出。也就是说,在RStudio中我想推动编织按钮,并且可以选择编织HTMl输出或pdf输出。目前,我没有兴趣编织一个单词/ OpenOffice文档。

I used the amazingly useful stargazer cheatsheet from Jake Russ. This exercises most of stargazer's functions. It is an R MArkdown file, with the chunk option results='asis' set for those chunks producing stargazer output.

我使用了Jake Russ的惊人有用的观星者备忘单。这练习了大多数观星者的功能。它是一个R MArkdown文件,其中chunk选项results ='asis'为那些产生stargazer输出的块设置。

The stargazer command itself has an argument 'type'. The default is type='latex' In Jake Russ's cheatsheet, which is intended to produce a webpage, type='html' is used throughout.

stargazer命令本身有一个参数'type'。默认值为type ='latex'在Jake Russ的cheatsheet中,用于生成网页,始终使用type ='html'。

This does not work at all if you try to knit it into a pdf. Tables come out as long lists, one table cell per line, with no formatting, and occupying many pages, with no formatting.

如果您尝试将其编织为pdf,则根本不起作用。表格作为长列表,每行一个表格单元格,没有格式化,占用很多页面,没有格式化。

The smallest change that I can make to allow me to produce nice pdf's within RStudio is to globally replace all the

我可以做的最小的变化是让我在RStudio中生成漂亮的pdf是全局替换所有的

type='html'

with

type='latex'

(note that both occur in the text of the document, as well as in the stargazer commands, so care is needed!)

(请注意,两者都出现在文档的文本中,以及stargazer命令中,因此需要小心!)

This works! As far as I can see the pdf is a faithful replica of the webpage, which is exactly what I want.

这有效!据我所知,pdf是网页的忠实复制品,这正是我想要的。

Trying to knit OpenOffice documents, if I leave

如果我离开,试图编写OpenOffice文档

type='latex'

Each table in the output is replaced by this text:-

输出中的每个表都被此文本替换: -

% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu % Date and time: Tue, Sep 01, 2015 - 22:22:29

If I restore the

如果我恢复了

type='html'

then each table is written, one cell per line, down the side of the page with no formatting!

然后每个表被写入,每行一个单元格,在页面的一侧没有格式化!