Rodrigo_BC
Rodrigo_BC

Reputation: 161

R: table1 output

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)

Upvotes: 3

Views: 3038

Answers (2)

David J. Bosak
David J. Bosak

Reputation: 1624

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

Upvotes: 0

Rodrigo_BC
Rodrigo_BC

Reputation: 161

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))

Upvotes: 3

Related Questions