Giorgio Spedicato
Giorgio Spedicato

Reputation: 2503

using texreg with Rmarkdown to get word documents

When I used Sweave, I loved texreg package for pretty printing model results into latex. I moved to RMarkdown to produce word document, and I am facing issues since I am not more able to recycle old texreg code.

Is it any way to amend this chunk to have a pretty print outputting in Word?

Note: upgradesngr.fx is a plm package object.

{r summary,echo=FALSE,results='markup'} htmlreg(list(upgradesngr.fx), star.symbol = "\\*", center = TRUE, doctype = FALSE)

Upvotes: 2

Views: 1374

Answers (1)

Philip Leifeld
Philip Leifeld

Reputation: 2323

In what follows, I assume you are using RStudio to create Word documents. I believe this to be a software-specific issue. In short: you can't embed HTML code when you knit Word documents.

The htmlreg function in the texreg package creates HTML tables (not Markdown tables). In a proper Markdown document, HTML can be embedded as this is part of the Markdown specification. For example, you can just write the following code in your Markdown document, and it will be translated into a table with two columns:

<table>
<tr>
<td>upper left</td>
<td>upper right</td>
</tr>
<tr>
<td>lower left</td>
<td>lower right</td>
</tr>
</table>

E.g., if you use the "Knit to HTML" function in RStudio, or if you use any other Markdown editor/viewer, this will produce the following result:

upper left upper right
lower left lower right

If you try this in a Markdown document in RStudio and create an HTML document from it, it will work. Note that no indentation is permitted by the Pandoc engine that is used under the hood in RStudio.

However, if you try to create a Word document, the document is not interpreted as (proper) Markdown code (in the sense that embedding HTML is not possible), i.e., you get all cells just listed vertically, but not formatted as a table. In other words, using the "Knit to Word" function will not interpret HTML chunks embedded in Markdown. The result will look like this:

upper left
upper right
lower left
lower right

You can try it in your own document in RStudio.

Now, this is precisely why the "Knit to Word" function does not embed tables created by htmlreg properly in a Word file.

Here is a solution that works properly with the "Knit to HTML" function, though, as this function seems to interpret HTML code correctly: First, let's do some preparatory steps. Load the texreg package and estimate a simple linear model for illustration purposes:

```{R echo = FALSE, include = FALSE}
library("texreg")
ctl <- c(4.17, 5.58, 5.18, 6.11, 4.50, 4.61, 5.17, 4.53, 5.33, 5.14)
trt <- c(4.81, 4.17, 4.41, 3.59, 5.87, 3.83, 6.03, 4.89, 4.32, 4.69)
group <- gl(2, 10, 20, labels = c("Ctl", "Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
```

Then create an HTML table as follows:

```{r summary,echo = FALSE, results = "asis"}
htmlreg(lm.D9, star.symbol = "\\*", center = TRUE, doctype = FALSE, caption = "")
```

Note that results = "asis" is necessary here. Also note that the htmlreg function produces a line for the document type by default, as the function can also be used to create complete HTML documents. We need to switch this off to make it work. In your example, you correctly escaped the star symbols. The result will be a proper regression table embedded in an HTML document. If you now use the "Knit to Word" function, this will include the table contents as well, but just like in the HTML example above, it will simply list all table cells as separate lines and is not formatted properly as a table.

It is also possible to use the "Knit to PDF" function in RStudio to create PDF documents through the use of LaTeX. In this case, you will have to replace the HTML table by a LaTeX table, obviously. To do so, use the following code instead of the above:

```{r summary, echo = FALSE, results = "asis"}
texreg(lm.D9, star.symbol = "\\*", center = FALSE, caption = "")
```

Finally, let me note that you can create Word documents using a workaround. It should be possible to create an HTML document and then open or import it in MS Word. At least it is possible to save the output of htmlreg to a file and open that in Word, so I assume the same should hold for a longer HTML document as created using knitr.

Upvotes: 5

Related Questions