GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — cli/R/spark.R at main · cderv/cli Expand file tree/ Copy pathspark.RMore file actions 109 lines (91 loc) · 2.48 KB / Copy pathspark.RFile metadata and controls109 lines (91 loc) · 2.48 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 # from pillar #' Draw a sparkline bar graph with unicode block characters #' #' Rendered using [block elements](https://en.wikipedia.org/wiki/Block_Elements). #' In most common fixed width fonts these are rendered wider than regular #' characters which means they are not suitable if you need precise alignment. #' #' You might want to avoid sparklines on non-UTF-8 systems, because they #' do not look good. You can use [is_utf8_output()] to test for support #' for them. #' #' @param x A numeric vector between 0 and 1 #' @export #' @seealso [spark_line()] #' @examples #' x <- seq(0, 1, length = 6) #' spark_bar(x) #' spark_bar(sample(x)) #' #' spark_bar(seq(0, 1, length = 8)) #' #' # NAs are left out #' spark_bar(c(0, NA, 0.5, NA, 1)) spark_bar <- function(x) { stopifnot(is.numeric(x)) chars <- spark_bar_chars(x) structure( paste0(chars, collapse = ""), class = c("cli_spark_bar", "cli_spark") ) } spark_bar_chars <- function(x, bars = NULL) { if (is.null(bars)) { if (is_utf8_output()) { bars <- vapply(0x2581:0x2588, intToUtf8, character(1)) } else { bars <- c("_", ",", "*", "#") } } factor <- cut( x, breaks = seq(0, 1, length = length(bars) + 1), labels = bars, include.lowest = TRUE ) chars <- as.character(factor) chars[is.na(chars)] <- " " chars } #' Draw a sparkline line graph with Braille characters. #' #' You might want to avoid sparklines on non-UTF-8 systems, because they #' do not look good. You can use [is_utf8_output()] to test for support #' for them. #' #' @inheritParams spark_bar #' @export #' @seealso [spark_bar()] #' @examples #' x <- seq(0, 1, length = 10) #' spark_line(x) spark_line <- function(x) { stopifnot(is.numeric(x)) if (length(x) %% 2 == 1) { x <- c(x, NA) } if (is_utf8_output()) { y <- findInterval(x, seq(0, 1, length = 5), all.inside = TRUE) ind <- matrix(y, ncol = 2, byrow = TRUE) ind[, 2] <- ind[, 2] + 4 chars <- apply(ind, 1, braille) } else { ind <- matrix(x, ncol = 2, byrow = TRUE) bars <- c("_", ",", "-", "^") chars <- spark_bar_chars(apply(ind, 1, mean), bars) } structure( paste0(chars, collapse = ""), class = c("cli_spark_line", "cli_spark") ) } # https://en.wikipedia.org/wiki/Braille_Patterns braille <- function(x) { # remap to braille sequence x <- c(7L, 3L, 2L, 1L, 8L, 6L, 5L, 4L)[x] raised <- 1:8 %in% x binary <- raised * 2^(0:7) # offset in hex is 2800 val <- 10240 + sum(raised * 2^(0:7)) intToUtf8(val) } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |