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 |
| 6 | #' @export |
| 7 | spec_color <- function(x, alpha = 1, begin = 0, end = 1, |
| 8 | direction = 1, option = "D", |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 9 | na_color = "#BBBBBB") { |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 10 | x <- round(rescale(x, c(1, 256))) |
| 11 | color_code <- viridisLite::viridis(256, alpha, begin, end, direction, option)[x] |
| 12 | color_code[is.na(color_code)] <- na_color |
| 13 | return(color_code) |
| 14 | } |
| 15 | |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 16 | html_color_ <- function(color) { |
| 17 | if (substr(color, 1, 1) != "#" | nchar(color) != 9) return(color) |
| 18 | rgba_code <- col2rgb(color, alpha = TRUE) |
| 19 | rgba_code[4] <- round(rgba_code[4] / 255, 2) |
| 20 | return(paste0("rgba(", paste(rgba_code, collapse = ", "), ")")) |
| 21 | } |
| 22 | |
| 23 | html_color <- function(colors) { |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 24 | colors <- as.character(colors) |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 25 | sapply(colors, html_color_) |
| 26 | } |
| 27 | |
| 28 | latex_color_ <- function(color) { |
| 29 | if (substr(color, 1, 1) != "#") { |
| 30 | return(paste0("{", color, "}")) |
| 31 | } else { |
| 32 | color <- sub("#", "", color) |
| 33 | if (nchar(color) == 8) color <- substr(color, 1, 6) |
| 34 | return(paste0("[HTML]{", color, "}")) |
| 35 | } |
| 36 | } |
| 37 | latex_color <- function(colors) { |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 38 | colors <- as.character(colors) |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 39 | sapply(colors, latex_color_) |
| 40 | } |
| 41 | |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 42 | #' Generate common font size for continuous values |
| 43 | #' |
| 44 | #' @param x continuous vectors of values |
| 45 | #' @param begin Smalles font size to be used. Default is 10. |
| 46 | #' @param end Largest font size. Default is 20. |
| 47 | #' @param na_font_size font size for NA values |
| 48 | #' @export |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 49 | spec_font_size <- function(x, begin = 8, end = 16, na_font_size = 12) { |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 50 | x <- round(rescale(x, c(begin, end))) |
| 51 | x[is.na(x)] <- na_font_size |
| 52 | return(x) |
| 53 | } |
| 54 | |
| 55 | #' Generate rotation angle for continuous values |
| 56 | #' |
| 57 | #' @param x continuous vectors of values |
| 58 | #' @param begin Smallest degree to rotate. Default is 0 |
| 59 | #' @param end Largest degree to rotate. Default is 359. |
| 60 | #' @export |
Hao Zhu | 6290fdd | 2017-10-24 00:10:32 -0400 | [diff] [blame] | 61 | spec_angle <- function(x, begin, end) { |
| 62 | x <- round(rescale(x, c(begin, end))) |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 63 | x[is.na(x)] <- 0 |
| 64 | return(x) |
| 65 | } |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 66 | |
| 67 | #' Setup bootstrap tooltip |
| 68 | #' |
| 69 | #' @param title text for hovering message |
| 70 | #' @param position How the tooltip should be positioned. Possible values are |
| 71 | #' `right`(default), `top`, `bottom`, `left` & `auto`. |
| 72 | #' |
| 73 | #' @export |
| 74 | spec_tooltip <- function(title, position = "right") { |
| 75 | position <- match.arg(position, c("right", "bottom", "top", "left", "auto")) |
| 76 | tooltip_options <- paste( |
| 77 | 'data-toggle="tooltip"', |
| 78 | paste0('data-placement="', position, '"'), |
| 79 | # ifelse(as_html, 'data-html="true"', NULL), |
| 80 | paste0('title="', title, '"')) |
| 81 | class(tooltip_options) <- "ke_tooltip" |
| 82 | return(tooltip_options) |
| 83 | } |
| 84 | |
| 85 | #' Setup bootstrap popover |
| 86 | #' |
| 87 | #' @param content content for pop-over message |
| 88 | #' @param title title for pop-over message. |
| 89 | #' @param trigger Controls how the pop-over message should be triggered. |
| 90 | #' Possible values include `hover` (default), `click`, `focus` and `manual`. |
| 91 | #' @param position How the tooltip should be positioned. Possible values are |
| 92 | #' `right`(default), `top`, `bottom`, `left` & `auto`. |
| 93 | #' |
| 94 | #' @export |
| 95 | spec_popover <- function(content = NULL, title = NULL, |
| 96 | trigger = "hover", position = "right") { |
| 97 | trigger <- match.arg(trigger, c("hover", "click", "focus", "manual"), |
| 98 | several.ok = TRUE) |
| 99 | position <- match.arg(position, c("bottom", "top", "left", "right", "auto"), |
| 100 | several.ok = TRUE) |
| 101 | popover_options <- paste( |
| 102 | 'data-toggle="popover"', |
| 103 | paste0('data-trigger="', trigger, '"'), |
| 104 | paste0('data-placement="', position, '"'), |
| 105 | ifelse(!is.null(title), paste0('title="', title, '"'), ""), |
| 106 | paste0('data-content="', content, '"')) |
| 107 | class(popover_options) <- "ke_popover" |
| 108 | return(popover_options) |
| 109 | } |