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.
Last active
December 30, 2015 06:59
-
-
Save ramnathv/7793167 to your computer and use it in GitHub Desktop.
Spark Blocks in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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