Imagine I have the following function which has a lot of arguments but for the sake of simplicity I only list two here:
library(glue)
glue("{a} + {b} is equal to 3", a = 1, b = 2)
Now I have a data.table with all the arguments/values that I want to pass to my glue function:
data.table:
library(data.table)
DT <- data.table(varnames = c("a", "b"), values = c(1, 2))
How could I directly pass the arguments/values from my DT onto my glue function?
You can use setNames and call glue_data:
glue_data(setNames(DT$values, DT$varnames), "{a} + {b} is equal to 3")
#1 + 2 is equal to 3
or you use do.call:
do.call(glue, as.list(c("{a} + {b} is equal to 3",
setNames(DT$values, DT$varnames))))
#1 + 2 is equal to 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With