1

I am looking for an R package that can create publication-quality tables in an rmarkdown word format report such that the tables contain variable labels. I would like a one-way table with the label "Flower species" like the one generated from Stata:

tab Species

     Flower |
    species |      Freq.     Percent        Cum.
------------+-----------------------------------
     setosa |         50       33.33       33.33
 versicolor |         50       33.33       66.67
  virginica |         50       33.33      100.00
------------+-----------------------------------
      Total |        150      100.00

I have my variable labels set via the help of label {Hmisc} function:

library(Hmisc)
label(iris$Species) <- 'Flower species'
label(iris$Species)
[1] "Flower species"

Now I would like to tabulate species with the label 'Flower species' and here are my options:

Option 1:

library(catspec)
ctab(iris$Species)
            Count Total %
Var1                     
setosa      50.00   33.33
versicolor  50.00   33.33
virginica   50.00   33.33

Problem is that the label displayed is 'Var1' instead of 'Flower species'

Option 2:

library(sjPlot) 
sjt.frq(iris$Species)

enter image description here

This displays the variable label as required but this function only generates a HTML output table and cannot be exported into rmarkdown word format output while retaining the clean table format.

Option 3:

From this page:

source("http://pcwww.liv.ac.uk/~william/R/crosstab.r")
crosstab(iris, row.vars='Species')

Species       Count Total %
  setosa      50.00   33.33
  versicolor  50.00   33.33
  virginica   50.00   33.33
  Sum        150.00  100.00

This function does not display the label 'Flower species' and I am not sure if there are options to specify how to display labels.

amo
  • 3,030
  • 4
  • 25
  • 42
  • Do you mean to add dynamic caption to a table? Maybe see [here](http://stackoverflow.com/questions/25867689) and [here](http://stackoverflow.com/questions/33965560)? – zx8754 Mar 13 '17 at 13:09
  • No, the caption might be good to have but I am more concerned with the variable label – amo Mar 13 '17 at 13:10
  • Does `knitr::kable(ctab(iris$Species)$ctab, caption = 'Flower Species')` suit you? – Martin Schmelzer Mar 13 '17 at 13:47
  • This approach produces a table with the required label. However, I would like the label defined by `label(iris$Species) <- 'Flower species'` picked automatically without having to specify `caption = 'Flower Species'`. Moreover, the following does not work: `knitr::kable(ctab(iris$Species)$ctab, caption = label(iris$Species))` – amo Mar 13 '17 at 14:13
  • Well I can't get you any further. But `knitr::kable(ctab(iris$Species)$ctab, caption = label(iris$Species))` does work for me. – Martin Schmelzer Mar 13 '17 at 14:25
  • @martin. I get the table in the rmarkdown word output using `knitr::kable(ctab(iris$Species)$ctab, caption = label(iris$Species))` but it has no caption. – amo Mar 13 '17 at 14:39
  • Neither do I see the label in the HTML output if I run that command solely in the console. But the docx output does show the label, if it is set correctly. – Martin Schmelzer Mar 13 '17 at 14:43
  • @martin I had left out the `label(iris$Species) <- 'Flower species'` in my rmarkdown. It works now – amo Mar 13 '17 at 14:51
  • You can import a HTML-file in word-processors, so simply generate a HTML-document with knitr, and open the HTML-file in Word/Writer/... See also [the pkg-vignette](https://cran.r-project.org/web/packages/sjPlot/vignettes/sjtbasic.html). – Daniel Mar 13 '17 at 14:55
  • @martin Though the `knitr::kable` approach works, it does not respond to `ctab` options like decimal places specification. `knitr::kable(ctab(iris$Species, dec.places=1)$ctab, caption = label(iris$Species))` This still gives the default 5 decimal places – amo Mar 13 '17 at 15:06
  • `knitr::kable(ctab(iris$Species)$ctab, caption = label(iris$Species), digits = 1)` – Martin Schmelzer Mar 13 '17 at 15:07
  • @MartinSchmelzer It works, thanks. `knitr::kable(ctab(iris$Species)$ctab, caption = label(iris$Species), digits = 1)` – amo Mar 14 '17 at 04:51

1 Answers1

2

I recommend the arsenal package. Try this:

library(arsenal)
library(rmarkdown)

t1<-tableby(~Species,data = iris)
labels(t1)<- c(Species='Flower species')

write2word(t1,"d:/test.doc")

This produces the following table. See the arsenal docmentation for further help, they have very good vignettes

enter image description here

ckx
  • 88
  • 7