用大括号括起来的表达式将被计算为 R 代码。长字符串按行断开并连接在一起。第一行和最后一行的前导空格和空行将被自动修剪。
用法
glue_data(
.x,
...,
.sep = "",
.envir = parent.frame(),
.open = "{",
.close = "}",
.na = "NA",
.null = character(),
.comment = "#",
.literal = FALSE,
.transformer = identity_transformer,
.trim = TRUE
)
glue(
...,
.sep = "",
.envir = parent.frame(),
.open = "{",
.close = "}",
.na = "NA",
.null = character(),
.comment = "#",
.literal = FALSE,
.transformer = identity_transformer,
.trim = TRUE
)
参数
- .x
-
[
listish
]
用于查找值的环境、列表或 DataFrame 。 - ...
-
[
expressions
]
未命名的参数被视为要格式化的表达式字符串。多个输入在格式化之前连接在一起。命名参数被视为可用于替换的临时变量。 - .sep
-
[
character(1)
:‘“”’]
分隔符用于分隔元素。 - .envir
-
[
environment
:parent.frame()
]
计算每个表达式的环境。表达式从左到右计算。如果.x
是一个环境,表达式在该环境中求值并且.envir
被忽略。如果NULL
通过了,就相当于emptyenv()
. - .open
-
[
character(1)
: '\{']
开始分隔符。将完整分隔符加倍即可逃脱它。 - .close
-
[
character(1)
: ‘\}’]
结束分隔符。将完整分隔符加倍即可逃脱它。 - .na
-
[
character(1)
:‘不适用’]
要替换的值NA
值与.如果NULL
缺失值被传播,即NA
结果会导致NA
输出。否则该值将被替换为.na
. - .null
-
[
character(1)
: ‘character()’]
用于替换 NULL 值的值。如果character()
整个输出是character()
.如果NULL
所有 NULL 值都会被删除(如paste0()
)。否则该值将被替换为.null
. - .comment
-
[
character(1)
: ‘#’]
用作注释字符的值。 - .literal
-
[
boolean(1)
: '错误的']
解析表达式字符串时,是否将单引号或双引号、反引号和注释视为常规字符(而不是语法元素)。环境.literal = TRUE
可能只有与定制结合才有意义.transformer
,就像这样glue_col()
。将此论证(尤其是其名称)视为实验性的。 - .transformer
-
[
function]
具有三个参数的函数code
,envir
和data
用于在评估之前、期间或之后转换每个块的输出。例如转换器参见vignette("transformers")
. - .trim
-
[
logical(1)
: '真的']
是否修剪输入模板trim()
或不。
也可以看看
https://www.python.org/dev/peps/pep-0498/ and https://www.python.org/dev/peps/pep-0257/ upon which this is based.
例子
name <- "Fred"
age <- 50
anniversary <- as.Date("1991-10-12")
glue('My name is {name},',
'my age next year is {age + 1},',
'my anniversary is {format(anniversary, "%A, %B %d, %Y")}.')
#> My name is Fred,my age next year is 51,my anniversary is Saturday, October 12, 1991.
# single braces can be inserted by doubling them
glue("My name is {name}, not {{name}}.")
#> My name is Fred, not {name}.
# Named arguments can be used to assign temporary variables.
glue('My name is {name},',
' my age next year is {age + 1},',
' my anniversary is {format(anniversary, "%A, %B %d, %Y")}.',
name = "Joe",
age = 40,
anniversary = as.Date("2001-10-12"))
#> My name is Joe, my age next year is 41, my anniversary is Friday, October 12, 2001.
# `glue()` can also be used in user defined functions
intro <- function(name, profession, country){
glue("My name is {name}, a {profession}, from {country}")
}
intro("Shelmith", "Senior Data Analyst", "Kenya")
#> My name is Shelmith, a Senior Data Analyst, from Kenya
intro("Cate", "Data Scientist", "Kenya")
#> My name is Cate, a Data Scientist, from Kenya
# `glue_data()` is useful in magrittr pipes
if (require(magrittr)) {
mtcars %>% glue_data("{rownames(.)} has {hp} hp")
# Or within dplyr pipelines
if (require(dplyr)) {
head(iris) %>%
mutate(description = glue("This {Species} has a petal length of {Petal.Length}"))
}}
#> Loading required package: magrittr
#> Loading required package: dplyr
#>
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#>
#> filter, lag
#> The following objects are masked from ‘package:base’:
#>
#> intersect, setdiff, setequal, union
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3.0 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> 5 5.0 3.6 1.4 0.2 setosa
#> 6 5.4 3.9 1.7 0.4 setosa
#> description
#> 1 This setosa has a petal length of 1.4
#> 2 This setosa has a petal length of 1.4
#> 3 This setosa has a petal length of 1.3
#> 4 This setosa has a petal length of 1.5
#> 5 This setosa has a petal length of 1.4
#> 6 This setosa has a petal length of 1.7
# Alternative delimiters can also be used if needed
one <- "1"
glue("The value of $e^{2\\pi i}$ is $<<one>>$.", .open = "<<", .close = ">>")
#> The value of $e^{2\pi i}$ is $1$.
相关用法
- R glue glue_col 用颜色构造字符串
- R glue glue_collapse 折叠字符向量
- R glue glue_sql 使用 SQL 转义插入字符串
- R glue glue_safe 安全地插入字符串
- R glue trim 修剪字符向量
- R glue quoting 引用运算符
- R SparkR glm用法及代码示例
- R ggplot2 annotation_logticks 注释:记录刻度线
- R axisTicks 计算漂亮的轴刻度
- R googledrive drive_cp 复制云端硬盘文件
- R legend 将图例添加到绘图中
- R googledrive drive_mime_type 查找 MIME 类型
- R ggplot2 vars 引用分面变量
- R grid.curve 在位置之间绘制曲线
- R googledrive drive_reveal 添加新的云端硬盘文件信息列
- R googledrive drive_rm 从云端硬盘删除文件
- R ggplot2 position_stack 将重叠的对象堆叠在一起
- R ggplot2 geom_qq 分位数-分位数图
- R hcl HCL 颜色规格
- R legendGrob 构建一个图例 Grob
- R quartzFonts 石英字体
- R ggplot2 geom_spoke 由位置、方向和距离参数化的线段
- R googlesheets4 sheet_rename 重命名(工作)表
- R googledrive expose 暴露对象
- R grid.draw 画一个网格
注:本文由纯净天空筛选整理自Jim Hester等大神的英文原创作品 Format and interpolate a string。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。