简体   繁体   English

使用带有RStudio / rmarkdown / knitr的texreg软件包发布创建模型汇总表的问题

[英]Issue creating model summary table using package texreg with RStudio/rmarkdown/knitr

I'm using the RStudio IDE (v 0.99.323) with rmarkdown and am attempting to produce model tables via knitr using htmlreg to produce MSWord output. 我正在将RStudio IDE(v.0.99.323)与rmarkdown一起rmarkdown并尝试通过使用htmlreg knitr产生模型表来产生MSWord输出。 Suspect I've missed something simple. 怀疑我错过了一些简单的事情。

The rmarkdown chunk appended below creates a separate word file 'mytable.doc' with a beautiful table. 下面附加的rmarkdown块创建一个带有漂亮表的单独的Word文件'mytable.doc'。 However, when I click 'Knit Word' in the RStudio IDE, the line htmlreg(m) generates html table code in the MSWord document. 但是,当我在RStudio IDE中单击“编织词”时,htmlreg(m)行在MSWord文档中生成html表代码。 What am I doing wrong? 我究竟做错了什么?

Many thanks! 非常感谢! --Dale --Dale

```{r, results='asis'}
library(MASS)
library(texreg)
data(menarche)
m <- glm(cbind(Menarche, Total-Menarche) ~ Age, family=binomial(logit), data=menarche)

htmlreg(m, file = "mytable.doc", caption="Age at Menarche", inline.css = TRUE, doctype = TRUE, html.tag = TRUE,                                               head.tag = TRUE, body.tag = TRUE, ci.force=TRUE, ci.test=NULL,bold=TRUE)

htmlreg(m)
```

Try this in your chunk, still using result='asis' : 仍然使用result='asis'在您的块中尝试:

library(pander)
pander(m)

Hat tip to http://www.r-statistics.com/2013/03/write-ms-word-document-using-r-with-as-little-overhead-as-possible/ 前往http://www.r-statistics.com/2013/03/write-ms-word-document-using-r-with-as-little-overhead-as-possible/的技巧

They also suggest a nice way to clarify code chunks so that you can just call print(m) and the output in markdown will call the appropriate function from pander . 他们还提出了一种澄清代码块的好方法,这样您就可以调用print(m) ,而markdown中的输出将调用pander的相应函数。

Can you please try the latest texreg version 1.34.2 (see the .tar.gz file here or in this post)? 你可以请升级至最新版本texreg 1.34.2(见.tar.gz文件在这里或在职位)?

According to the RStudio developers, the problem is that they switched to a newer version of Pandoc, which does not work with indented HTML code anymore. 根据RStudio开发人员的说法,问题在于他们切换到了Pandoc的较新版本,该版本不再适用于缩进的HTML代码。 More precisely, it interprets text that was indented with four spaces as a code block, as in Markdown notation. 更准确地说,它会将缩进四个空格的文本解释为代码块,如Markdown表示法。 See here for their problem description. 有关问题的说明,请参见此处

So in the new texreg version, there is a new argument called indentation = "" in the htmlreg function. 因此,在新的texreg版本中, htmlreg函数中有一个称为indentation = ""的新参数。 It switches indentation off by default. 默认情况下,它会关闭缩进。 Using indentation = " " restores the previous behavior. 使用indentation = " "可恢复以前的行为。

Edit 1 : Please also make sure to use arguments center = FALSE and star.symbol = "\\\\*" for alignment on the left and for displaying significance stars correctly. 编辑1 :还请确保使用参数center = FALSEstar.symbol = "\\\\*"进行左侧对齐并正确显示重要星号。 Asterisks need to be escaped because they are otherwise interpreted as part of the Markdown syntax: 星号需要转义,因为否则会将其解释为Markdown语法的一部分:

```{r, results = 'asis'}
htmlreg(m, center = FALSE, star.symbol = "\\*")
```{r}

For PDF notebooks (which use LaTeX internally), use texreg : 对于PDF笔记本(内部使用LaTeX),请使用texreg

```{r, results = 'asis'}
texreg(m, float.pos = "h")
```{r}

Edit 2 : Also read the help page of htmlreg , especially the part where the arguments of htmlreg are described. 编辑2 :另请阅读htmlreg的帮助页面,尤其是描述htmlreg参数的部分。 They contain some useful information on how to make the documents as compatible as possible with Markdown, which is used by RStudio, Pandoc, and knitr to create HTML documents. 它们包含一些有关如何使文档与Markdown尽可能兼容的有用信息,RStudio,Pandoc和knitr使用它们来创建HTML文档。 In particular, use arguments inline.css = TRUE , doctype = FALSE , html.tag = FALSE , head.tag = FALSE , and body.tag = FALSE when you do not intend to create a full-fledged HTML document. 特别是当您不打算创建完整的HTML文档时,请使用参数inline.css = TRUEdoctype = FALSEhtml.tag = FALSEhead.tag = FALSEbody.tag = FALSE

About MS Word : You mentioned in a comment below your question that you wanted to create either HTML or Word documents. 关于MS Word :您在问题下方的评论中提到要创建HTML或Word文档。 The htmlreg function is intended to create HTML files, not Word files (as the name of the function implies). htmlreg函数旨在创建HTML文件,而不是Word文件(正如该函数的名称所暗示的那样)。 It is possible to load these HTML files in MS Word, though, because Word is able to interpret HTML code. 但是,可以将这些HTML文件加载到MS Word中,因为Word能够解释HTML代码。 However, knitr creates binary Word documents, and embedding HTML code directly in these binary Word documents is not possible, as far as I know (but I may be wrong because I don't know how knitr creates the Word files internally). 但是,据我所知,knitr创建二进制Word文档,并且无法将HTML代码直接嵌入这些二进制Word文档中(但是我可能错了,因为我不知道knitr如何在内部创建Word文件)。 You could, however, try to create HTML notebooks, save them to disk, and then open them in MS Word. 但是,您可以尝试创建HTML笔记本,将它们保存到磁盘,然后在MS Word中打开它们。

This is a problem of pandoc markdown, or of htmlreg not creating the correct indentation. 这是pandoc markdown或htmlreg无法创建正确缩进的问题。 I do not fully understand if this is a bug or a feature because of the cryptic: 由于神秘原因,我不完全了解这是错误还是功能:

http://rmarkdown.rstudio.com/authoring_pandoc_markdown.html#raw-html http://rmarkdown.rstudio.com/authoring_pandoc_markdown.html#raw-html

Try a simple .md (not .rmd ) file as follows: 请尝试如下所示的简单.md (不是.rmd )文件:

<h1>Works</h1>

<table border="8">
  <tr>
  <td>111</td>
  <td>222</td>
  <td>444</td>
  </tr>
</table>

<h1>Not what you want</h1>

<table border="8">
  <tr>
    <td>111</td>
    <td>222</td>
    <td>444</td>
  </tr>
</table>

<h1>Works too (not in screenshot)</h1>

<table border="8">
  <tr><td>111</td><td>222</td><td>444</td></tr>
</table>

原始html的输出

The package author has updated texreg to switch-off indentation by default. 软件包作者已将texreg更新为默认关闭缩进。

See: http://rmarkdown.rstudio.com/authoring_migrating_from_v1.html#preserving-generated-html after updating the package via: install.packages("texreg", repos = " http://R-Forge.R-project.org ") 通过以下方式更新软件包后,请参见: http ://rmarkdown.rstudio.com/authoring_migrating_from_v1.html#preserving-generation-html:install.packages(“ texreg”,repos =“ http://R-Forge.R-project。 org “)

The chunk below placed in an rmarkdown (.Rmd) document now produces a beautiful html table when I 'Knit HTML' within RStudio. 现在,当我在RStudio中“编织HTML”时,放在rmarkdown(.Rmd)文档中的下面的块会生成漂亮的html表。 However, 'Knit Word' still does not produce the expected output. 但是,“编织词”仍然无法产生预期的输出。

```{r, results='asis'} ```{r,results ='asis'}

library(texreg) 库(texreg)

htmlreg(m, caption="Age at Menarche", caption.above=TRUE, ci.force=TRUE, ci.test=NULL,bold=TRUE) htmlreg(m,caption =“年龄达到初潮年龄”,caption.above = TRUE,ci.force = TRUE,ci.test = NULL,bold = TRUE)

``` ```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM