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)
```
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)
```
This is a problem of
pandoc
markdown, or ofhtmlreg
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:Try this in your chunk, still using
result='asis'
: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 frompander
.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 calledindentation = ""
in thehtmlreg
function. It switches indentation off by default. Usingindentation = " "
restores the previous behavior.Edit 1: Please also make sure to use arguments
center = FALSE
andstar.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:For PDF notebooks (which use LaTeX internally), use
texreg
:Edit 2: Also read the help page of
htmlreg
, especially the part where the arguments ofhtmlreg
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 argumentsinline.css = TRUE
,doctype = FALSE
,html.tag = FALSE
,head.tag = FALSE
, andbody.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.