Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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)
```
like image 798
user25494 Avatar asked Jan 26 '26 04:01

user25494


2 Answers

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.

like image 183
vpipkt Avatar answered Jan 27 '26 17:01

vpipkt


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.

like image 37
Philip Leifeld Avatar answered Jan 27 '26 16:01

Philip Leifeld



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!