0

Here is code from notebook:

---
title: "R Notebook"
output:
  word_document: default
  html_notebook: default
---

```{r}
data(ToothGrowth)
```


```{r}
library(table1)
table1(reformulate(colnames(ToothGrowth)), data=ToothGrowth)

```

```{r}
library(stargazer)
stargazer(ToothGrowth, type = "html")
```

Word file generated with knitr:

R Notebook
data(ToothGrowth)
library(table1)
## 
## Attaching package: 'table1'
## The following objects are masked from 'package:base':
## 
##     units, units<-
table1(reformulate(colnames(ToothGrowth)), data=ToothGrowth)
## [1] "<table class=\"Rtable1\">\n<thead>\n<tr>\n<th class='rowlabel firstrow lastrow'></th>\n<th class='firstrow lastrow'><span class='stratlabel'>Overall<br><span class='stratn'>(n=60)</span></span></th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td class='rowlabel firstrow'><span class='varlabel'>len</span></td>\n<td class='firstrow'></td>\n</tr>\n<tr>\n<td class='rowlabel'>Mean (SD)</td>\n<td>18.8 (7.65)</td>\n</tr>\n<tr>\n<td class='rowlabel lastrow'>Median [Min, Max]</td>\n<td class='lastrow'>19.3 [4.20, 33.9]</td>\n</tr>\n<tr>\n<td class='rowlabel firstrow'><span class='varlabel'>supp</span></td>\n<td class='firstrow'></td>\n</tr>\n<tr>\n<td class='rowlabel'>OJ</td>\n<td>30 (50.0%)</td>\n</tr>\n<tr>\n<td class='rowlabel lastrow'>VC</td>\n<td class='lastrow'>30 (50.0%)</td>\n</tr>\n<tr>\n<td class='rowlabel firstrow'><span class='varlabel'>dose</span></td>\n<td class='firstrow'></td>\n</tr>\n<tr>\n<td class='rowlabel'>Mean (SD)</td>\n<td>1.17 (0.629)</td>\n</tr>\n<tr>\n<td class='rowlabel lastrow'>Median [Min, Max]</td>\n<td class='lastrow'>1.00 [0.500, 2.00]</td>\n</tr>\n</tbody>\n</table>\n"
library(stargazer)
## 
## Please cite as:
##  Hlavac, Marek (2018). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2.2. https://CRAN.R-project.org/package=stargazer
stargazer(ToothGrowth, type = "html")
## 
## <table style="text-align:center"><tr><td colspan="8" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">Statistic</td><td>N</td><td>Mean</td><td>St. Dev.</td><td>Min</td><td>Pctl(25)</td><td>Pctl(75)</td><td>Max</td></tr>
## <tr><td colspan="8" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">len</td><td>60</td><td>18.813</td><td>7.649</td><td>4.200</td><td>13.075</td><td>25.275</td><td>33.900</td></tr>
## <tr><td style="text-align:left">dose</td><td>60</td><td>1.167</td><td>0.629</td><td>0.500</td><td>0.500</td><td>2.000</td><td>2.000</td></tr>
## <tr><td colspan="8" style="border-bottom: 1px solid black"></td></tr></table>

Log of word file generation:

processing file: asfdgasdg.Rmd
  |...........                                                      |  17%
  ordinary text without R code

  |......................                                           |  33%
label: unnamed-chunk-1
  |................................                                 |  50%
  ordinary text without R code

  |...........................................                      |  67%
label: unnamed-chunk-2
  |......................................................           |  83%
  ordinary text without R code

  |.................................................................| 100%
label: unnamed-chunk-3

"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS asfdgasdg.utf8.md --to docx --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash+smart --output asfdgasdg.docx --highlight-style tango 
output file: asfdgasdg.knit.md


Output created: asfdgasdg.docx

Also after running code above, html output from Stargazer in RStudio notebook is not rendered html content but just a plain html code. table1 html output is rendered in RStudio notebook, so no problem with it.

If I do I put output of packages in html file and add this file to Word through Insert >> select the file (in html format) >> Insert (at the bottom right corner) >> Insert , than Word renders html content of those files as word tables (not puts html as plain text.).

My questions:

  1. How to make Stargazer html output to be rendered, not just a plain html code in RStudio notebook?

  2. How to generate Word file with knitr that will give html output of both packages to be shown as word tables, not just plain html code?

vasili111
  • 6,032
  • 10
  • 50
  • 80
  • While I haven't looked at your question in detail, I can say that many packages that make tables in R don't work particularly seamlessly with Word. I have had good success with package **flextable**, however, although I've never used it with a document that was knit to both Word and HTML. – aosmith Sep 08 '19 at 19:22
  • @aosmith I though maybe knitr has some kind of instruction that I can set that will render output of some code snippet as html in RStudio notebook and other instruction to put html output of the code snippet in generated word file as table and not plain html code. – vasili111 Sep 08 '19 at 19:28
  • One thing that might work for you is to use the Rstudio "knit with HTML" option, then go to the working directory and right-click the html output and select OPEN with Word. The tables will not be as good as the html version though. – markhogue Sep 08 '19 at 19:32
  • @aosmith as I understand `flextable` gives data frame as is in html format. But I want descriptive table of dataframe which are provided by `table1` and `Stargaze` – vasili111 Sep 08 '19 at 19:42
  • @markhogue I tried it and the result is the same as with knitr, `table1` table is rendered correctly but not `stargaze` table. – vasili111 Sep 08 '19 at 19:44
  • Please see the answer below. If it doesn't do what you want, please let me know and I'll delete it. – markhogue Sep 08 '19 at 20:03

0 Answers0