emit <- function(key, values) {
cat(key, "=", paste(values, collapse = "|"), "\n", sep = "")
}
emit("quotes", c('Lake', "Lake", "site 'north'", 'site "north"', "site \"west\"", 'can\'t'))
emit("paste", c(paste("lake", "east"), paste("lake", "east", sep = "-"),
paste(c("a", "b"), 2:5, sep = "", collapse = "="),
paste(c("a", "b"), 2:3, collapse = "."),
paste("a", 2:3, sep = ""), paste0("site", 1L)))
emit("format", c(format(8.123456, digits = 5), format(c(8, 12.25), scientific = TRUE),
format(8.25, nsmall = 4, scientific = FALSE), format(8),
sprintf("<%s>", format(12.5, width = 7)),
sprintf("<%s>", format("Ridge", width = 7, justify = "left")),
sprintf("<%s>", format("Ridge", width = 9, justify = "centre")),
sprintf("<%s>", format("Ridge", width = 7, justify = "right")),
sprintf("<%s>", format("Ridge", width = 7, justify = "none"))))
word <- intToUtf8(c(0x6e56, 0x6cca))
emit("size", c(nchar("Ridge lake"), nchar(word, type = "chars"), nchar(word, type = "bytes")))
emit("case", c(toupper("Rain Gauge"), tolower("Rain Gauge")))
emit("substring", c(substring("ridge-lake", 2, 5), substring("ridge-lake", 7),
substring("ridge-lake", 30)))
emit("replace", c(gsub(".", "-", "a.b.", fixed = TRUE), gsub("[0-9]+", "N", "lake12 ridge5")))
result <- strsplit(c("ridge, lake", ",field,"), ",", fixed = TRUE)
emit("split", c(typeof(result), length(result), result[[1L]], sprintf("<%s>", result[[2L]])))
本例需在 基础 R。已在项目独立的 Windows x64 R 4.6.2 环境验证。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:基础 R。已在项目独立的 Windows x64 R 4.6.2 环境验证。
入口文件:projects/r-loops-functions-text/strings.R
本地运行步骤快速上手
- 点击上方“保存源码”保存为
strings.R,或下载右侧课程工程包。 - 确认本地已准备 基础 R。已在项目独立的 Windows x64 R 4.6.2 环境验证。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联29章) · 含当前章节或实例 · mbskycom202612a317.zip
课程关联工程包(登记关联3章) · 含当前章节或实例 · mbskycom202612a325.zip