這些函數是 glue::glue()
和 glue::glue_data()
的包裝器,它們提供了強大而優雅的語法,用於使用 {}
插入字符串。
這些包裝器提供了一小部分完整選項。直接從glue 中使用glue()
和glue_data()
進行更多控製。
用法
str_glue(..., .sep = "", .envir = parent.frame())
str_glue_data(.x, ..., .sep = "", .envir = parent.frame(), .na = "NA")
參數
- ...
-
[
expressions
]
未命名的參數被視為要格式化的表達式字符串。多個輸入在格式化之前連接在一起。命名參數被視為可用於替換的臨時變量。 - .sep
-
[
character(1)
:‘“”’]
分隔符用於分隔元素。 - .envir
-
[
environment
:parent.frame()
]
計算每個表達式的環境。表達式從左到右計算。如果.x
是一個環境,表達式在該環境中求值並且.envir
被忽略。如果NULL
通過了,就相當於emptyenv()
. - .x
-
[
listish
]
用於查找值的環境、列表或 DataFrame 。 - .na
-
[
character(1)
:‘不適用’]
要替換的值NA
值與.如果NULL
缺失值被傳播,即NA
結果會導致NA
輸出。否則該值將被替換為.na
.
例子
name <- "Fred"
age <- 50
anniversary <- as.Date("1991-10-12")
str_glue(
"My name is {name}, ",
"my age next year is {age + 1}, ",
"and my anniversary is {format(anniversary, '%A, %B %d, %Y')}."
)
#> My name is Fred, my age next year is 51, and my anniversary is Saturday, October 12, 1991.
# single braces can be inserted by doubling them
str_glue("My name is {name}, not {{name}}.")
#> My name is Fred, not {name}.
# You can also used named arguments
str_glue(
"My name is {name}, ",
"and my age next year is {age + 1}.",
name = "Joe",
age = 40
)
#> My name is Joe, and my age next year is 41.
# `str_glue_data()` is useful in data pipelines
mtcars %>% str_glue_data("{rownames(.)} has {hp} hp")
#> Mazda RX4 has 110 hp
#> Mazda RX4 Wag has 110 hp
#> Datsun 710 has 93 hp
#> Hornet 4 Drive has 110 hp
#> Hornet Sportabout has 175 hp
#> Valiant has 105 hp
#> Duster 360 has 245 hp
#> Merc 240D has 62 hp
#> Merc 230 has 95 hp
#> Merc 280 has 123 hp
#> Merc 280C has 123 hp
#> Merc 450SE has 180 hp
#> Merc 450SL has 180 hp
#> Merc 450SLC has 180 hp
#> Cadillac Fleetwood has 205 hp
#> Lincoln Continental has 215 hp
#> Chrysler Imperial has 230 hp
#> Fiat 128 has 66 hp
#> Honda Civic has 52 hp
#> Toyota Corolla has 65 hp
#> Toyota Corona has 97 hp
#> Dodge Challenger has 150 hp
#> AMC Javelin has 150 hp
#> Camaro Z28 has 245 hp
#> Pontiac Firebird has 175 hp
#> Fiat X1-9 has 66 hp
#> Porsche 914-2 has 91 hp
#> Lotus Europa has 113 hp
#> Ford Pantera L has 264 hp
#> Ferrari Dino has 175 hp
#> Maserati Bora has 335 hp
#> Volvo 142E has 109 hp
相關用法
- R stringr str_which 查找匹配索引
- R stringr str_extract 提取完整的匹配項
- R stringr str_subset 查找匹配元素
- R stringr str_escape 轉義正則表達式元字符
- R stringr str_trim 刪除空格
- R stringr str_sub 使用子字符串的位置獲取和設置子字符串
- R stringr str_replace_na 把NA變成“NA”
- R stringr str_trunc 將字符串截斷至最大寬度
- R stringr str_match 從匹配中提取組件(捕獲組)
- R stringr str_like 以與 SQL 的 LIKE 運算符相同的方式檢測模式
- R stringr str_length 計算長度/寬度
- R stringr str_detect 檢測是否存在匹配
- R stringr str_count 計算匹配次數
- R stringr str_split 將字符串分成幾段
- R stringr str_unique 刪除重複的字符串
- R stringr str_remove 刪除匹配的模式
- R stringr str_pad 將字符串填充到最小寬度
- R stringr str_equal 判斷兩個字符串是否相等
- R stringr str_view 查看字符串和匹配項
- R stringr str_conv 指定字符串的編碼
- R stringr str_order 對字符向量進行排序、排名或排序
- R stringr str_starts 檢測開始/結束時是否存在匹配
- R stringr str_c 將多個字符串連接成一個字符串
- R stringr str_wrap 將單詞包裝成格式良好的段落
- R stringr str_dup 複製字符串
注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Interpolation with glue。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。