library(gridify)
library(magrittr)
library(ggplot2)
# Create a gridify object using a ggplot and a custom layout:
# Set text elements on various cells:
gridify_obj <- gridify(
object = ggplot2::ggplot(data = mtcars, ggplot2::aes(x = mpg, y = wt)) +
ggplot2::geom_line(),
layout = pharma_layout_base(
margin = grid::unit(c(0.5, 0.5, 0.5, 0.5), "inches"),
global_gpar = grid::gpar(fontfamily = "serif", fontsize = 10)
)
) %>%
set_cell("header_left_1", "My Company") %>%
set_cell("header_left_2", " / ") %>%
set_cell("header_left_3", "") %>%
set_cell("header_right_1", "CONFIDENTIAL") %>%
set_cell("header_right_2", "") %>%
set_cell("header_right_3", "Data Cut-off: YYYY-MM-DD") %>%
set_cell("output_num", " xx.xx.xx") %>%
set_cell("title_1", "") %>%
set_cell("title_2", "<Title 2>") %>%
set_cell("title_3", "<Optional Title 3>") %>%
set_cell("by_line", "By: <GROUP>, <optionally: Demographic parameters>") %>%
set_cell("note", "<Note or Footnotes>") %>%
set_cell("references", "<References:>") %>%
set_cell("footer_left", "Program: <PROGRAM NAME>, YYYY-MM-DD at HH:MM") %>%
set_cell("footer_right", "Page xx of nn") %>%
set_cell("watermark", "DRAFT")
# Export a result to different file types
# Different file export formats require specific capabilities in your R installation.
# Use capabilities() to check which formats are supported in your R build.
# PNG
temp_png_default <- tempfile(fileext = ".png")
export_to(
gridify_obj,
to = temp_png_default
)
temp_png_custom <- tempfile(fileext = ".png")
export_to(
gridify_obj,
to = temp_png_custom,
width = 2400,
height = 1800,
res = 300
)
# JPEG
temp_jpeg_default <- tempfile(fileext = ".jpeg")
export_to(
gridify_obj,
to = temp_jpeg_default
)
temp_jpeg_custom <- tempfile(fileext = ".jpeg")
export_to(
gridify_obj,
to = temp_jpeg_custom,
width = 2400,
height = 1800,
res = 300
)
# TIFF
temp_tiff_default <- tempfile(fileext = ".tiff")
export_to(
gridify_obj,
to = temp_tiff_default
)
temp_tiff_custom <- tempfile(fileext = ".tiff")
export_to(
gridify_obj,
to = temp_tiff_custom,
width = 2400,
height = 1800,
res = 300
)
# PDF
temp_pdf_A4 <- tempfile(fileext = ".pdf")
export_to(
gridify_obj,
to = temp_pdf_A4
)
temp_pdf_A4long <- tempfile(fileext = ".pdf")
export_to(
gridify_obj,
to = temp_pdf_A4long,
width = 8.3,
height = 11.7
)
# Use different pdf device - cairo_pdf
temp_pdf_A4long_cairo <- tempfile(fileext = ".pdf")
export_to(
gridify_obj,
to = temp_pdf_A4long_cairo,
device = grDevices::cairo_pdf,
width = 8.3,
height = 11.7
)
# Multiple Objects - a list
gridify_list <- list(gridify_obj, gridify_obj)
temp_pdf_multipageA4 <- tempfile(fileext = ".pdf")
export_to(
gridify_list,
to = temp_pdf_multipageA4
)
temp_pdf_multipageA4long <- tempfile(fileext = ".pdf")
export_to(
gridify_list,
to = temp_pdf_multipageA4long,
width = 8.3,
height = 11.7
)
temp_png_multi <- c(tempfile(fileext = ".png"), tempfile(fileext = ".png"))
export_to(
gridify_list,
to = temp_png_multi
)
temp_png_multi_custom <- c(tempfile(fileext = ".png"), tempfile(fileext = ".png"))
export_to(
gridify_list,
to = temp_png_multi_custom,
width = 800,
height = 600,
res = 96
)
Run the code above in your browser using DataLab