Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 1 | #' Generate viridis Color code for continuous values |
| 2 | #' |
| 3 | #' @inheritParams viridisLite::viridis |
| 4 | #' @param x continuous vectors of values |
| 5 | #' @param na_color color code for NA values |
Hao Zhu | 4e557c2 | 2017-12-19 12:49:48 -0500 | [diff] [blame] | 6 | #' @param scale_from input range (vector of length two). If not given, |
| 7 | #' is calculated from the range of x |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 8 | #' @export |
| 9 | spec_color <- function(x, alpha = 1, begin = 0, end = 1, |
| 10 | direction = 1, option = "D", |
Hao Zhu | 3edc85a | 2017-12-19 12:55:44 -0500 | [diff] [blame] | 11 | na_color = "#BBBBBB", scale_from = NULL) { |
| 12 | if (is.null(scale_from)) { |
Hao Zhu | 4e557c2 | 2017-12-19 12:49:48 -0500 | [diff] [blame] | 13 | x <- round(rescale(x, c(1, 256))) |
| 14 | } else { |
| 15 | x <- round(rescale(x, to = c(1, 256), |
| 16 | from = scale_from)) |
| 17 | } |
| 18 | |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 19 | color_code <- viridisLite::viridis(256, alpha, begin, end, direction, option)[x] |
| 20 | color_code[is.na(color_code)] <- na_color |
| 21 | return(color_code) |
| 22 | } |
| 23 | |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 24 | html_color_ <- function(color) { |
| 25 | if (substr(color, 1, 1) != "#" | nchar(color) != 9) return(color) |
| 26 | rgba_code <- col2rgb(color, alpha = TRUE) |
| 27 | rgba_code[4] <- round(rgba_code[4] / 255, 2) |
| 28 | return(paste0("rgba(", paste(rgba_code, collapse = ", "), ")")) |
| 29 | } |
| 30 | |
| 31 | html_color <- function(colors) { |
Hao Zhu | 72917f9 | 2019-03-15 18:41:42 -0400 | [diff] [blame] | 32 | colors <- trimws(gsub("\\!important", "", as.character(colors))) |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 33 | sapply(colors, html_color_) |
| 34 | } |
| 35 | |
| 36 | latex_color_ <- function(color) { |
| 37 | if (substr(color, 1, 1) != "#") { |
Hao Zhu | 33b865f | 2020-08-18 02:10:43 -0400 | [diff] [blame^] | 38 | return(paste0("\\{", color, "\\}")) |
| 39 | } else { |
| 40 | color <- sub("#", "", color) |
| 41 | if (nchar(color) == 8) color <- substr(color, 1, 6) |
| 42 | return(paste0("\\[HTML\\]\\{", color, "\\}")) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | latex_color__ <- function(color) { |
| 47 | if (substr(color, 1, 1) != "#") { |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 48 | return(paste0("{", color, "}")) |
| 49 | } else { |
| 50 | color <- sub("#", "", color) |
| 51 | if (nchar(color) == 8) color <- substr(color, 1, 6) |
| 52 | return(paste0("[HTML]{", color, "}")) |
| 53 | } |
| 54 | } |
Hao Zhu | 33b865f | 2020-08-18 02:10:43 -0400 | [diff] [blame^] | 55 | latex_color <- function(colors, escape = TRUE) { |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 56 | colors <- as.character(colors) |
Hao Zhu | 33b865f | 2020-08-18 02:10:43 -0400 | [diff] [blame^] | 57 | if (escape) { |
| 58 | return(sapply(colors, latex_color_)) |
| 59 | } else { |
| 60 | return(sapply(colors, latex_color__)) |
| 61 | } |
| 62 | |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 63 | } |
| 64 | |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 65 | #' Generate common font size for continuous values |
| 66 | #' |
| 67 | #' @param x continuous vectors of values |
| 68 | #' @param begin Smalles font size to be used. Default is 10. |
| 69 | #' @param end Largest font size. Default is 20. |
| 70 | #' @param na_font_size font size for NA values |
Hao Zhu | 4e557c2 | 2017-12-19 12:49:48 -0500 | [diff] [blame] | 71 | #' @param scale_from input range (vector of length two). If not given, |
| 72 | #' is calculated from the range of x |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 73 | #' @export |
Hao Zhu | 4e557c2 | 2017-12-19 12:49:48 -0500 | [diff] [blame] | 74 | spec_font_size <- function(x, begin = 8, end = 16, na_font_size = 12, |
Hao Zhu | 3edc85a | 2017-12-19 12:55:44 -0500 | [diff] [blame] | 75 | scale_from = NULL) { |
| 76 | if (is.null(scale_from)) { |
Hao Zhu | 4e557c2 | 2017-12-19 12:49:48 -0500 | [diff] [blame] | 77 | x <- round(rescale(x, c(begin, end))) |
| 78 | } else { |
| 79 | x <- round(rescale(x, to = c(begin, end), |
| 80 | from = scale_from)) |
| 81 | } |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 82 | x[is.na(x)] <- na_font_size |
| 83 | return(x) |
| 84 | } |
| 85 | |
| 86 | #' Generate rotation angle for continuous values |
| 87 | #' |
| 88 | #' @param x continuous vectors of values |
| 89 | #' @param begin Smallest degree to rotate. Default is 0 |
| 90 | #' @param end Largest degree to rotate. Default is 359. |
Hao Zhu | 4e557c2 | 2017-12-19 12:49:48 -0500 | [diff] [blame] | 91 | #' @param scale_from input range (vector of length two). If not given, |
| 92 | #' is calculated from the range of x |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 93 | #' @export |
Hao Zhu | 3edc85a | 2017-12-19 12:55:44 -0500 | [diff] [blame] | 94 | spec_angle <- function(x, begin, end, scale_from = NULL) { |
| 95 | if (is.null(scale_from)) { |
Hao Zhu | 4e557c2 | 2017-12-19 12:49:48 -0500 | [diff] [blame] | 96 | x <- round(rescale(x, c(begin, end))) |
| 97 | } else { |
| 98 | x <- round(rescale(x, to = c(begin, end), |
| 99 | from = scale_from)) |
| 100 | } |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 101 | x[is.na(x)] <- 0 |
| 102 | return(x) |
| 103 | } |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 104 | |
| 105 | #' Setup bootstrap tooltip |
| 106 | #' |
| 107 | #' @param title text for hovering message |
| 108 | #' @param position How the tooltip should be positioned. Possible values are |
| 109 | #' `right`(default), `top`, `bottom`, `left` & `auto`. |
| 110 | #' |
| 111 | #' @export |
| 112 | spec_tooltip <- function(title, position = "right") { |
Hao Zhu | 8a69ad1 | 2018-01-10 18:01:41 -0500 | [diff] [blame] | 113 | position <- match.arg(position, c("right", "bottom", "top", "left", "auto"), |
| 114 | several.ok = TRUE) |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 115 | tooltip_options <- paste( |
Hao Zhu | 33b865f | 2020-08-18 02:10:43 -0400 | [diff] [blame^] | 116 | 'data-toggle="tooltip" data-container="body"', |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 117 | paste0('data-placement="', position, '"'), |
| 118 | # ifelse(as_html, 'data-html="true"', NULL), |
| 119 | paste0('title="', title, '"')) |
Hao Zhu | 33b865f | 2020-08-18 02:10:43 -0400 | [diff] [blame^] | 120 | tooltip_options_list <- list( |
| 121 | 'data-toggle' = 'tooltip', |
| 122 | 'data-container' = 'body', |
| 123 | 'data-placement' = position, |
| 124 | 'title' = if(is.null(title)) '' else title |
| 125 | ) |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 126 | class(tooltip_options) <- "ke_tooltip" |
Hao Zhu | 33b865f | 2020-08-18 02:10:43 -0400 | [diff] [blame^] | 127 | attr(tooltip_options, 'list') <- tooltip_options_list |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 128 | return(tooltip_options) |
| 129 | } |
| 130 | |
| 131 | #' Setup bootstrap popover |
| 132 | #' |
| 133 | #' @param content content for pop-over message |
| 134 | #' @param title title for pop-over message. |
| 135 | #' @param trigger Controls how the pop-over message should be triggered. |
| 136 | #' Possible values include `hover` (default), `click`, `focus` and `manual`. |
| 137 | #' @param position How the tooltip should be positioned. Possible values are |
| 138 | #' `right`(default), `top`, `bottom`, `left` & `auto`. |
| 139 | #' |
| 140 | #' @export |
| 141 | spec_popover <- function(content = NULL, title = NULL, |
| 142 | trigger = "hover", position = "right") { |
| 143 | trigger <- match.arg(trigger, c("hover", "click", "focus", "manual"), |
| 144 | several.ok = TRUE) |
| 145 | position <- match.arg(position, c("bottom", "top", "left", "right", "auto"), |
| 146 | several.ok = TRUE) |
| 147 | popover_options <- paste( |
Hao Zhu | 33b865f | 2020-08-18 02:10:43 -0400 | [diff] [blame^] | 148 | 'data-toggle="popover" data-container="body"', |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 149 | paste0('data-trigger="', trigger, '"'), |
| 150 | paste0('data-placement="', position, '"'), |
| 151 | ifelse(!is.null(title), paste0('title="', title, '"'), ""), |
| 152 | paste0('data-content="', content, '"')) |
Hao Zhu | 33b865f | 2020-08-18 02:10:43 -0400 | [diff] [blame^] | 153 | popover_options_list <- list( |
| 154 | 'data-toggle' = 'popover', |
| 155 | 'data-container' = 'body', |
| 156 | 'data-trigger' = trigger, |
| 157 | 'data-placement' = position, |
| 158 | 'data-content' = content |
| 159 | ) |
| 160 | if (!is.null(title)) { |
| 161 | popover_options_list['title'] <- title |
| 162 | } |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 163 | class(popover_options) <- "ke_popover" |
Hao Zhu | 33b865f | 2020-08-18 02:10:43 -0400 | [diff] [blame^] | 164 | attr(popover_options, 'list') <- popover_options_list |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 165 | return(popover_options) |
| 166 | } |