Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 1 | #' Helper functions to generate inline sparklines |
| 2 | #' |
| 3 | #' @description These functions helps you quickly generate sets of sparkline |
| 4 | #' style plots using base R plotting system. Currently, we support histogram |
| 5 | #' and boxplot. You can use them together with `column_spec` to |
| 6 | #' generate inline plot in tables. By default, this function will save images |
| 7 | #' in a folder called "kableExtra" and return the address of the file. |
| 8 | #' |
| 9 | #' @param x Vector of values or List of vectors of values. |
| 10 | #' @param width The width of the plot in pixel |
| 11 | #' @param height The height of the plot in pixel |
| 12 | #' @param res The resolution of the plot. Default is 300. |
| 13 | #' @param same_lim T/F. If x is a list of vectors, should all the plots be |
| 14 | #' plotted in the same range? Default is True. |
| 15 | #' @param lim Manually specify plotting range in the form of `c(0, 10)`. |
| 16 | #' @param xaxt On/Off for xaxis text |
| 17 | #' @param yaxt On/Off for yaxis text |
| 18 | #' @param ann On/Off for annotations (titles and axis titles) |
| 19 | #' @param col Color for the fill of the histogram bar/boxplot box. |
| 20 | #' @param border Color for the border. |
| 21 | #' @param dir Directory of where the images will be saved. |
| 22 | #' @param file File name. If not provided, a random name will be used |
| 23 | #' @param file_type Graphic device. Support `png` or `svg`. SVG is recommended |
| 24 | #' for HTML output |
| 25 | #' @param add_label For boxplot. T/F to add labels for min, mean and max. |
| 26 | #' @param label_digits If T for add_label, rounding digits for the label. |
| 27 | #' Default is 2. |
| 28 | #' @param boxlty Boxplot - box boarder type |
| 29 | #' @param medcol Boxplot - median line color |
| 30 | #' @param medlwd Boxplot - median line width |
| 31 | #' |
| 32 | #' @inheritParams graphics::hist |
| 33 | #' @inheritParams graphics::boxplot |
| 34 | #' @export |
| 35 | spec_hist <- function(x, width = 200, height = 50, res = 300, |
| 36 | breaks = "Sturges", |
| 37 | same_lim = TRUE, lim = NULL, |
| 38 | xaxt = 'n', yaxt = 'n', ann = FALSE, |
| 39 | col = "lightgray", border = NULL, |
| 40 | dir = if (is_latex()) rmd_files_dir() else tempdir(), |
| 41 | file = NULL, |
| 42 | file_type = if (is_latex()) "png" else "svg", ...) { |
| 43 | if (is.list(x)) { |
| 44 | if (same_lim & is.null(lim)) { |
| 45 | lim <- base::range(unlist(x)) |
| 46 | } |
| 47 | return(lapply(x, function(x_) {spec_hist( |
| 48 | x = x_, width = width, height = height, |
| 49 | breaks = breaks, same_lim = same_lim, lim = lim, |
| 50 | xaxt = xaxt, yaxt = yaxt, ann = ann, col = col, border = border, |
| 51 | dir = dir, file = file, file_type = file_type, ... |
| 52 | )})) |
| 53 | } |
| 54 | |
| 55 | if (is.null(lim)) { |
| 56 | lim <- base::range(x) |
| 57 | } |
| 58 | |
| 59 | file_type <- match.arg(file_type, c("svg", "png")) |
| 60 | |
| 61 | if (!dir.exists(dir)) { |
| 62 | dir.create(dir) |
| 63 | } |
| 64 | |
| 65 | if (is.null(file)) { |
| 66 | file <- tempfile("hist", dir, paste0('.', file_type)) |
| 67 | } |
| 68 | |
| 69 | if (file_type == "svg") { |
| 70 | grDevices::svg(filename = file, width = width / res, height = height / res, |
| 71 | bg = 'transparent') |
| 72 | } else { |
| 73 | grDevices::png(filename = file, width = width, height = height, res = res, |
| 74 | bg = 'transparent') |
| 75 | } |
| 76 | |
| 77 | graphics::par(mar = c(0, 0, 0.2, 0), lwd=0.5) |
| 78 | graphics::hist(x, breaks = breaks, xlim = lim, border = border, |
| 79 | xaxt = xaxt, yaxt = yaxt, ann = ann, col = col, ...) |
| 80 | grDevices::dev.off() |
| 81 | |
| 82 | if (file_type == "svg") { |
| 83 | svg_xml <- xml2::read_xml(file) |
| 84 | svg_text <- as.character(svg_xml) |
| 85 | unlink(file) |
| 86 | } else { |
| 87 | svg_text <- NULL |
| 88 | } |
| 89 | out <- list(path = file, dev = file_type, type = "hist", |
| 90 | width = width, height = height, res = res, |
| 91 | svg_text = svg_text) |
| 92 | |
| 93 | class(out) <- "kableExtraInlinePlots" |
| 94 | return(out) |
| 95 | } |
| 96 | |
| 97 | #' @rdname spec_hist |
| 98 | #' @export |
| 99 | spec_boxplot <- function(x, width = 200, height = 50, res = 300, |
| 100 | add_label = FALSE, label_digits = 2, |
| 101 | same_lim = TRUE, lim = NULL, |
| 102 | xaxt = 'n', yaxt = 'n', ann = FALSE, |
| 103 | col = "lightgray", border = NULL, |
| 104 | boxlty = 0, medcol = "red", medlwd = 1, |
| 105 | dir = if (is_latex()) rmd_files_dir() else tempdir(), |
| 106 | file = NULL, |
| 107 | file_type = if (is_latex()) "png" else "svg", ...) { |
| 108 | if (is.list(x)) { |
| 109 | if (same_lim & is.null(lim)) { |
| 110 | lim <- base::range(unlist(x)) |
| 111 | } |
| 112 | return(lapply(x, function(x_) {spec_boxplot( |
| 113 | x = x_, width = width, height = height, |
| 114 | add_label = add_label, same_lim = same_lim, lim = lim, |
| 115 | xaxt = xaxt, yaxt = yaxt, ann = ann, |
| 116 | col = col, border = border, |
| 117 | boxlty = boxlty, medcol = medcol, medlwd = medlwd, |
| 118 | dir = dir, file = file, file_type = file_type, ... |
| 119 | )})) |
| 120 | } |
| 121 | |
| 122 | if (is.null(lim)) { |
| 123 | lim <- base::range(x) |
| 124 | lim[1] <- lim[1] - (lim[2] - lim[1]) / 10 |
| 125 | lim[2] <- (lim[2] - lim[1]) / 10 + lim[2] |
| 126 | } |
| 127 | |
| 128 | file_type <- match.arg(file_type, c("svg", "png")) |
| 129 | |
| 130 | if (!dir.exists(dir)) { |
| 131 | dir.create(dir) |
| 132 | } |
| 133 | |
| 134 | if (is.null(file)) { |
| 135 | file <- tempfile("hist", dir, paste0('.', file_type)) |
| 136 | } |
| 137 | |
| 138 | if (file_type == "svg") { |
| 139 | grDevices::svg(filename = file, width = width / res, height = height / res, |
| 140 | bg = 'transparent') |
| 141 | } else { |
| 142 | grDevices::png(filename = file, width = width, height = height, res = res, |
| 143 | bg = 'transparent') |
| 144 | } |
| 145 | |
| 146 | graphics::par(mar = c(0, 0, 0, 0)) |
| 147 | |
| 148 | graphics::boxplot(x, horizontal = TRUE, ann = ann, frame = FALSE, bty = 'n', ylim = lim, |
| 149 | col = col, border = border, |
| 150 | boxlty = boxlty, medcol = medcol, medlwd = medlwd, |
| 151 | axes = FALSE, outcex = 0.2, whisklty = 1, |
| 152 | ...) |
| 153 | if (add_label) { |
| 154 | x_median <- round(median(x, na.rm = T), label_digits) |
| 155 | x_min <- round(min(x, na.rm = T), label_digits) |
| 156 | x_max <- round(max(x, na.rm = T), label_digits) |
| 157 | graphics::text(x_median, y = 1.4, labels = x_median, cex = 0.5) |
| 158 | graphics::text(x_min, y = 0.6, labels = x_min, cex = 0.5) |
| 159 | graphics::text(x_max, y = 0.6, labels = x_max, cex = 0.5) |
| 160 | } |
| 161 | grDevices::dev.off() |
| 162 | |
| 163 | if (file_type == "svg") { |
| 164 | svg_xml <- xml2::read_xml(file) |
| 165 | svg_text <- as.character(svg_xml) |
| 166 | unlink(file) |
| 167 | } else { |
| 168 | svg_text <- NULL |
| 169 | } |
| 170 | out <- list(path = file, dev = file_type, type = "boxplot", |
| 171 | width = width, height = height, res = res, |
| 172 | svg_text = svg_text) |
| 173 | class(out) <- "kableExtraInlinePlots" |
| 174 | return(out) |
| 175 | } |
| 176 | |
| 177 | is_latex <- knitr::is_latex_output |
| 178 | |
| 179 | rmd_files_dir <- function(create = TRUE) { |
| 180 | curr_file_name <- sub("\\.[^\\.]*$", "", knitr::current_input()) |
| 181 | dir_name <- paste0(curr_file_name, "_files") |
| 182 | if (!dir.exists(dir_name) & create) dir.create(dir_name) |
Hao Zhu | 7f3fa85 | 2020-08-26 13:55:38 -0400 | [diff] [blame^] | 183 | fig_dir_name <- file.path(dir_name, "figure-latex/") |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 184 | if (!dir.exists(fig_dir_name) & create) dir.create(fig_dir_name) |
| 185 | return(fig_dir_name) |
| 186 | } |
| 187 | |