Skip to content

Instantly share code, notes, and snippets.

@ramnathv
Last active December 30, 2015 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramnathv/7793167 to your computer and use it in GitHub Desktop.
Save ramnathv/7793167 to your computer and use it in GitHub Desktop.
Spark Blocks in R

This is an R implemenation of SparkBlocks. I have not yet tested the code, so use with caution. The code is a line-by-line translation of the python code. So there is a lot of scope to make it more R-like using vectorized constructs and built-in functions.

#' SparkBar Generator in R
#'
#' Code based on SparkBlocks in Python
#' https://github.com/1stvamp/py-sparkblocks/blob/master/sparkblocks/__init__.py
#'
#' @example
#' spark(30, 31, 32, 33)
#' "▁▃▅▇"
spark = function(...){
numbers = c(...)
min_value = min(numbers)
max_value = max(numbers)
value_scale = max_value - min_value
nums = c()
for (number in numbers){
if ((number - min_value) != 0 && (value_scale != 0)){
scaled_value = (number - min_value)/value_scale
} else {
scaled_value = 0
}
# Hack because 9604 and 9608 aren't vertically aligned the same as
# other block elements
num = floor(min(6, scaled_value * 7))
if (num == 3){
if ((scaled_value * 7) < 3.5){
num = 2
} else {
num = 4
}
} else if (num == 7){
num = 6
}
nums = c(nums, num)
}
intToUtf8(9601 + nums)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment