diff --git a/R/util.r b/R/util.r index 4230451..ef75489 100644 --- a/R/util.r +++ b/R/util.r @@ -32,6 +32,8 @@ fill_template = function(template, values, required=c()) { paste(setdiff(required, keys), collapse=", ")) upd = keys %in% names(values) + is_num = sapply(values, is.numeric) + values[is_num] = format(values[is_num], scientific=FALSE, trim=TRUE) vals[upd] = unlist(values)[keys[upd]] if (any(is.na(vals))) stop("Template values required but not provided: ", diff --git a/tests/testthat/test-0-util.r b/tests/testthat/test-0-util.r index 03b721f..7d7b05e 100644 --- a/tests/testthat/test-0-util.r +++ b/tests/testthat/test-0-util.r @@ -27,3 +27,24 @@ test_that("template required key", { expect_error(fill_template(tmpl, values, required="missing")) }) + +test_that("template filling works with vectors", { + tmpl = "{{ var1 }} and {{ var2 }}" + values = c(var1=1, var2=2) + + expect_equal(fill_template(tmpl, values), "1 and 2") +}) + +test_that("template numbers are not converted to sci format", { + tmpl = "this is my {{ template }}" + values = list(template = 100000) + + expect_equal(fill_template(tmpl, values), "this is my 100000") +}) + +test_that("no sci format when passing vectors", { + tmpl = "{{ var1 }} and {{ var2 }}" + values = c(var1=1, var2=1e6) + + expect_equal(fill_template(tmpl, values), "1 and 1000000") +})