#' Decode Value
#'
#' Takes a product from `encode_value()` and returns a vector of decoded values
#' using a key as generated by `make_encoding_key()`.
#'
#' @param values_to_decode A product of prime numbers multiplied using
#' `encode_value()`.
#' @param prime_key A key as generated by `make_encoding_key()`
#'
#' @usage decode_value(values_to_decode, prime_key)
#'
#' @return A vector of decoded values.
#' @export
#' @seealso [primr::encode_value()]
#' @seealso [primr::make_encoding_key()]
#' @md
#'
#' @examples
#' # Return a vector containing "Mammal" and "Bird".
#' decoded_values <- decode_value(15, c(Fish = 2, Mammal = 3, Bird = 5))
decode_value <- function(values_to_decode, prime_key) {
# Check whether values_to_decode is empty, i.e. equal to 1.
if (values_to_decode == 1) {
warning("Argument values_to_decode equal to 1, i.e. value is empty. NA returned.")
return(NA)
}
# Check whether values_to_decode is numeric.
if (!is.numeric(values_to_decode)) {
stop("Argument value_to_decode needs to be a numeric.")
}
# Check whether values_to_decode contains value that cannot be factorized using prime key.
check_values <- values_to_decode
for (i in prime_key[values_to_decode %% prime_key == 0]) {
check_values <- check_values / i
}
if (check_values != 1) {
stop("Argument value_to_encode cannot be factorized using values in argument prime_key. Please ensure that correct key is used.")
}
# Factorize values_to_decode using prime key.
decoded_values <- names(prime_key)[values_to_decode %% prime_key == 0]
return(decoded_values)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.