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.31 / 0.70 = 0.44" "0.66 / 0.57 = 1.2"  "0.20 / 0.71 = 0.28"
#>  [4] "0.68 / 0.34 = 2.0"  "0.99 / 0.12 = 8.5"  "0.14 / 0.87 = 0.16"
#>  [7] "0.72 / 0.26 = 2.8"  "0.02 / 0.68 = 0.03" "0.87 / 0.74 = 1.2" 
#> [10] "0.93 / 0.50 = 1.9" 

table_glue("{x}", "({100 * y}%)", .sep = ' ')
#>  [1] "0.31 (70%)" "0.66 (57%)" "0.20 (71%)" "0.68 (34%)" "0.99 (12%)"
#>  [6] "0.14 (87%)" "0.72 (26%)" "0.02 (68%)" "0.87 (74%)" "0.93 (50%)"

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.31 / 0.70 = 0" "0.66 / 0.57 = 1" "0.20 / 0.71 = 0" "0.68 / 0.34 = 1"
#>  [5] "0.99 / 0.12 = 8" "0.14 / 0.87 = 0" "0.72 / 0.26 = 2" "0.02 / 0.68 = 0"
#>  [9] "0.87 / 0.74 = 1" "0.93 / 0.50 = 1"

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"