用大括號括起來的表達式將被計算為 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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。