library(reporter)
library(magrittr)
# Create temp file name
tmp <- file.path(tempdir(), "mtcars.txt")
# Prepare data
dat <- mtcars[1:10, ]
dat <- data.frame(vehicle = rownames(dat), dat, stringsAsFactors = FALSE)
# Define table
tbl <- create_table(dat, show_cols = 1:8) %>%
column_defaults(from = mpg, to = qsec, width = .5, format = "%.1f") %>%
define(vehicle, label = "Vehicle", width = 1.5, align = "left") %>%
define(c(cyl, hp), format = "%.0f")
# Create the report
rpt <- create_report(tmp, orientation = "portrait") %>%
titles("Table 2.5", "MTCARS Sample Report") %>%
add_content(tbl)
# Write the report
write_report(rpt)
# Send report to console for viewing
writeLines(readLines(tmp, encoding = "UTF-8"))
# Table 2.5
# MTCARS Sample Report
#
# Vehicle mpg cyl disp hp drat wt qsec
# -------------------------------------------------------------------
# Mazda RX4 21.0 6 160.0 110 3.9 2.6 16.5
# Mazda RX4 Wag 21.0 6 160.0 110 3.9 2.9 17.0
# Datsun 710 22.8 4 108.0 93 3.8 2.3 18.6
# Hornet 4 Drive 21.4 6 258.0 110 3.1 3.2 19.4
# Hornet Sportabout 18.7 8 360.0 175 3.1 3.4 17.0
# Valiant 18.1 6 225.0 105 2.8 3.5 20.2
# Duster 360 14.3 8 360.0 245 3.2 3.6 15.8
# Merc 240D 24.4 4 146.7 62 3.7 3.2 20.0
# Merc 230 22.8 4 140.8 95 3.9 3.1 22.9
# Merc 280 19.2 6 167.6 123 3.9 3.4 18.3
#
Run the code above in your browser using DataLab