3

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)
```
Dieter Menne
  • 10,076
  • 44
  • 67
user25494
  • 1,289
  • 14
  • 27

4 Answers4

2

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.

vpipkt
  • 1,710
  • 14
  • 17
  • Thanks for the link. Adding pander(m) does indeed produce nicely formatted output. However, the texreg package is very flexible and offers the ability to produce tables which combine several models. – user25494 Mar 13 '15 at 15:17
  • Researching the question I found some approaches that give very fine grained control over table outputs, and some that automate `print` in the desired format. My guess is that you may need to investigate deeper as to how texreg produces html and Word output. The question is how to get this integrated properly into the Rmarkdown -> markdown -> word workflow. – vpipkt Mar 13 '15 at 15:29
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.

Philip Leifeld
  • 2,253
  • 15
  • 24
  • Any clues about why the Word document is not correctly formatted? – user25494 Mar 13 '15 at 20:07
  • See "edit 2". Note also that the internal HTML browser displays the tables in a cluttered way. Use another browser (like Firefox or Chrome) to display the tables correctly. – Philip Leifeld Mar 13 '15 at 21:47
0

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>

output of raw html

Dieter Menne
  • 10,076
  • 44
  • 67
0

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)

```

user25494
  • 1,289
  • 14
  • 27