Expressive rounding for table values

table_glue(..., rspec = NULL, .sep = "", .envir = parent.frame())

Arguments

...

strings to round and format. Multiple inputs are concatenated together. Named arguments are not supported.

rspec

a rounding_specification object. If no rspec is given, a default setting will round values to decimal places based on the magnitude of the values.

.sep

Separator used to separate elements

.envir

environment to evaluate each expression in.

Value

a character vector of length equal to the vectors supplied in ...

See also

Other table helpers: table_ester(), table_pvalue(), table_value()

Examples

x <- runif(10) y <- runif(10) table_glue("{x} / {y} = {x/y}")
#> [1] "0.08 / 0.87 = 0.09" "0.83 / 0.17 = 4.8" "0.60 / 0.03 = 18" #> [4] "0.16 / 0.32 = 0.49" "0.01 / 0.40 = 0.02" "0.47 / 0.20 = 2.4" #> [7] "0.50 / 0.40 = 1.2" "0.29 / 0.06 = 4.6" "0.73 / 0.39 = 1.9" #> [10] "0.77 / 0.98 = 0.79"
table_glue("{x}", "({100 * y}%)", .sep = ' ')
#> [1] "0.08 (87%)" "0.83 (17%)" "0.60 (3.4%)" "0.16 (32%)" "0.01 (40%)" #> [6] "0.47 (20%)" "0.50 (40%)" "0.29 (6.4%)" "0.73 (39%)" "0.77 (98%)"
df = data.frame(x = 1:10, y=1:10) table_glue("{x} / {y} = {as.integer(x/y)}", .envir = df)
#> [1] "1 / 1 = 1" "2 / 2 = 1" "3 / 3 = 1" "4 / 4 = 1" "5 / 5 = 1" #> [6] "6 / 6 = 1" "7 / 7 = 1" "8 / 8 = 1" "9 / 9 = 1" "10 / 10 = 1"
table_glue("{x} / {y} = {as.integer(x/y)}")
#> [1] "0.08 / 0.87 = 0" "0.83 / 0.17 = 4" "0.60 / 0.03 = 17" "0.16 / 0.32 = 0" #> [5] "0.01 / 0.40 = 0" "0.47 / 0.20 = 2" "0.50 / 0.40 = 1" "0.29 / 0.06 = 4" #> [9] "0.73 / 0.39 = 1" "0.77 / 0.98 = 0"
with(df, table_glue("{x} / {y} = {as.integer(x/y)}"))
#> [1] "1 / 1 = 1" "2 / 2 = 1" "3 / 3 = 1" "4 / 4 = 1" "5 / 5 = 1" #> [6] "6 / 6 = 1" "7 / 7 = 1" "8 / 8 = 1" "9 / 9 = 1" "10 / 10 = 1"
mtcars$car <- rownames(mtcars) # use the default rounding specification table_glue( "the {car} gets ~{mpg} miles/gallon and weighs ~{wt} thousand lbs", .envir = mtcars[1:3, ] )
#> [1] "the Mazda RX4 gets ~21 miles/gallon and weighs ~2.6 thousand lbs" #> [2] "the Mazda RX4 Wag gets ~21 miles/gallon and weighs ~2.9 thousand lbs" #> [3] "the Datsun 710 gets ~23 miles/gallon and weighs ~2.3 thousand lbs"
# use your own rounding specification rspec <- round_spec() rspec <- round_using_decimal(rspec, digits = 1) table_glue( "the {car} gets ~{mpg} miles/gallon and weighs ~{wt} thousand lbs", rspec = rspec, .envir = mtcars[1:3, ] )
#> [1] "the Mazda RX4 gets ~21.0 miles/gallon and weighs ~2.6 thousand lbs" #> [2] "the Mazda RX4 Wag gets ~21.0 miles/gallon and weighs ~2.9 thousand lbs" #> [3] "the Datsun 710 gets ~22.8 miles/gallon and weighs ~2.3 thousand lbs"