3

Some days ago I found table1 library to get nice tables.

The only one problem (for me), its that output is a HTML table. I am using rtf library to export R table to word, but I dont know how export this output table (HTML) to word .

I wonder if exist some posibilty of get a different output. Or a different way to convert to R table. I am no using R-studio.

Thanks in advance.

library(table1)

table1(~mpg| carb*am,data = mtcars)
Rodrigo_BC
  • 161
  • 11
  • From the package [github repo](https://github.com/benjaminrich/table1): *"An R package for generating tables of descriptive statistics in HTML"*. If you want something else, perhaps you can add your vote to issues [#1](https://github.com/benjaminrich/table1/issues/1) (Jan 2017) and [#17](https://github.com/benjaminrich/table1/issues/17) (Jun 2019). – r2evans May 05 '20 at 20:23
  • As an alternative, [`gt`](https://gt.rstudio.com/) *"supports HTML output, with LaTeX and RTF planned for the future"* (from the website). (It isn't stable yet with PDF, but some have been able to still use it in PDF without problem, so *"for the future"* suggests stability and robust/consistent behavior, not that it does not work at all at the moment.) – r2evans May 05 '20 at 20:25
  • 1
    Thanks I get the info! – Rodrigo_BC May 05 '20 at 20:42

2 Answers2

3

Thanks to @r2evans for the information, I could get a R table, maybe I lost a little bit the format but is ok when I export to word with rtf library:

library(rvest)
library(table1)

tbl_1=table1(~mpg| carb*am,data = mtcars)
as.data.frame(read_html(tbl_1) %>% html_table(fill=TRUE))
Rodrigo_BC
  • 161
  • 11
  • 1
    Note that since version 1.4 of table1 you don't need rvest anymore. This will work: `as.data.frame(tbl_1)` – mrbrich Jan 19 '22 at 15:16
0

Note that you can get a lot more control over the output with some other packages. In the example below I'm using Tplyr and reporter. Tplyr generates the statistics and reporter will create the RTF. It takes a lot more work than table1. But you gain a lot more types of statistics and reports. You could basically produce any safety report.

library(Tplyr)
library(reporter)

dt <- tplyr_table(mtcars, am) %>% 
  add_layer(group_count(cyl)) %>% 
  add_layer(group_desc(mpg)) %>% 
  build() 


tbl <- create_table(dt, show_cols = c("ord_layer_index", "row_label1", 
                                      "var1_0", "var1_1")) %>% 
  stub(c("ord_layer_index", "row_label1"), label = "Variables") %>% 
  define(ord_layer_index, label = "Variable", label_row = TRUE,
         format = c("1" = "Cylinders",
                    "2" = "Miles Per Gallon"), 
         dedupe = TRUE, blank_after = TRUE) %>% 
  define(row_label1, label = "", indent = .25) %>% 
  define(var1_0, label = "Automatic", align = "center", n = 19) %>% 
  define(var1_1, label = "Manual", align = "center", n = 13) 

pth <- file.path(tempdir(), "test1.rtf")

rpt <- create_report(pth, 
                     output_type = "RTF",
                     orientation = "portrait") %>% 
  titles("Table 1.0", 
         "Characteristics of MTCars by Transmission Type",
         "Population: All Cars") %>% 
  set_margins(top = 1, bottom = 1) %>% 
  add_content(tbl)

write_report(rpt)

file.show(pth)

Here is the RTF output: enter image description here

David J. Bosak
  • 1,386
  • 12
  • 22