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

和自甴很熟 提交于 2019-12-08 02:11:39

问题


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. Suspect I've missed something simple.

The rmarkdown chunk appended below creates a separate word file 'mytable.doc' with a beautiful table. However, when I click 'Knit Word' in the RStudio IDE, the line htmlreg(m) generates html table code in the MSWord document. What am I doing wrong?

Many thanks! --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)
```

回答1:


Try this in your chunk, still using 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/

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.




回答2:


Can you please try the latest texreg version 1.34.2 (see the .tar.gz file here or in this post)?

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. More precisely, it interprets text that was indented with four spaces as a code block, as in Markdown notation. See here for their problem description.

So in the new texreg version, there is a new argument called indentation = "" in the htmlreg function. It switches indentation off by default. Using indentation = " " restores the previous behavior.

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. Asterisks need to be escaped because they are otherwise interpreted as part of the Markdown syntax:

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

For PDF notebooks (which use LaTeX internally), use 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. 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. 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.

About MS Word: You mentioned in a comment below your question that you wanted to create either HTML or Word documents. The htmlreg function is intended to create HTML files, not Word files (as the name of the function implies). It is possible to load these HTML files in MS Word, though, because Word is able to interpret HTML code. 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). You could, however, try to create HTML notebooks, save them to disk, and then open them in MS Word.




回答3:


This is a problem of pandoc markdown, or of htmlreg not creating the correct indentation. 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

Try a simple .md (not .rmd) file as follows:

<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>




回答4:


The package author has updated texreg to switch-off indentation by default.

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")

The chunk below placed in an rmarkdown (.Rmd) document now produces a beautiful html table when I 'Knit HTML' within RStudio. However, 'Knit Word' still does not produce the expected output.

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

library(texreg)

htmlreg(m, caption="Age at Menarche", caption.above=TRUE, ci.force=TRUE, ci.test=NULL,bold=TRUE)

```



来源:https://stackoverflow.com/questions/29033724/issue-creating-model-summary-table-using-package-texreg-with-rstudio-rmarkdown-k

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