4.12 高级的替换
相比于 sprintf()
格式化输出字符串的方式替换,它的优势在于提示性,或者说代码的可读性
<- function(param, text) {
glue_data <- gregexpr('\\{[^}]*\\}', text)[[1L]]
idx <- substring(text, idx, idx + attr(idx, 'match.length') - 1L)
keys for (key in keys) {
<- gsub(key, param[[gsub('[{}]', '', key)]], text, fixed = TRUE)
text
}
text
}cat(glue_data(
param = list(table = 'flights', origin = 'JFK'),
text = "
select count(*) as n
from {table}
where origin = '{origin}'
"
))
##
## select count(*) as n
## from flights
## where origin = 'JFK'
##