• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

R语言学习笔记:glue包实现变量传参

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

glue包介绍

glue包可用于自定义变量,然后通过传参的方式,对字符串部分内容进行自适应修改。
例如:可将日期赋值为:date = as.Date("2019-12-05"),然后通过字符串拼接的形式,实现文件名称自动更新,glue("The day is {date}."

具体用法


## glue包
## 功能:用于将变量传入字符串并解释变量

## 安装
install.packages("glue")
devtools::install_github("tidyverse/glue")

## 使用
library(glue)
name <- "Hider"
glue('My name is {name}.')  ## My name is Hider.

## 多行长字符串也可以连接在一块
name <- "Hider"
age <- 28
anniversary <- as.Date("1992-12-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 Hider, my age next year is 29, my anniversary is 星期六, 十二月 12, 1992.

## 可以把参数变量放到内部
glue('My name is {name},',
     ' my age next year is {age + 1},',
     ' my anniversary is {format(anniversary, "%A, %B %d, %Y")}.',
     name = "Hider",
     age = 28,
     anniversary = as.Date("1992-12-12"))
## My name is Hider, my age next year is 29, my anniversary is 星期六, 十二月 12, 1992.

library(tidyverse)
`%>%` <- magrittr::`%>%`
head(mtcars) %>% 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.

library(dplyr)
head(iris) %>%
  mutate(description = glue("This {Species} has a petal length of {Petal.Length}"))
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species                           description
# 1          5.1         3.5          1.4         0.2  setosa This setosa has a petal length of 1.4
# 2          4.9         3.0          1.4         0.2  setosa This setosa has a petal length of 1.4
# 3          4.7         3.2          1.3         0.2  setosa This setosa has a petal length of 1.3
# 4          4.6         3.1          1.5         0.2  setosa This setosa has a petal length of 1.5
# 5          5.0         3.6          1.4         0.2  setosa This setosa has a petal length of 1.4
# 6          5.4         3.9          1.7         0.4  setosa This setosa has a petal length of 1.7

## 前前后后的空行、空格会自动忽略
glue("
     A Formatted string
     Can have multiple lines
       with addititonal indention preserved.
     ")
# A Formatted string
# Can have multiple lines
# with addititonal indention preserved.

glue("
     
     leading or trailing newlines can be added explicitly
     
     ")
# leading or trailing newlines can be added explicitly

## 使用\\ 不换行
glue("
     A formatted string \\
     can also be on a \\
     single line.
     ")
# A formatted string can also be on a single line.

## 双重大括号将不解释变量
name <- "Hider"
glue("My name is {name}, not {{name}}.")
# My name is Hider, not {name}.

## 可以使用.open和.close指定替代分隔符
one <- "1"
glue("The value of $e^{2\\pi i}$ is $<<one>>$.",
     .open = "<<",
     .close = ">>")
# The value of $e^{2\pi i}$ is $1$.

# 有效的代码都可以使用 双反斜杠
`foo}\`` <- "foo"
glue("{
     {
     '}\\'' # { and } in comments, single quotes
     \"}\\\"\" # or double quotes are ignored
     `foo}\\`` # as are { in backticks
     }
     }")
# foo
# 真心看不懂。。

## glue_sql()构建SQL脚本
con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
colnames(iris) <- gsub("[.]", "_", tolower(colnames(iris)))
DBI::dbWriteTable(con, "iris", iris)
# 这部分待数据库配置好 再测试

## +号
y <- 1
y <- 5
glue("x + y") + " = {x + y}"
# x + y = 6
# x + y = 7
# x + y = 8
# x + y = 9
# x + y = 10
# 搞不懂 为什么会计算5次?

参考链接:tidyverse/glue


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
R语言:ggplot绘图常用方法发布时间:2022-07-18
下一篇:
【R笔记】日期处理R语言学习笔记:日期处理发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap