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

R语言学习笔记:glue包实现变量传参的更多相关文章

  1. C语言学习笔记 (006) - 二维数组传参的三种表现形式

    # include <stdio.h> # include <stdlib.h> # define M # define N int getdate(int (*sp)[M]) ...

  2. R语言学习笔记︱Echarts与R的可视化包——地区地图

    笔者寄语:感谢CDA DSC训练营周末上完课,常老师.曾柯老师加了小课,讲了echart与R结合的函数包recharts的一些基本用法.通过对比谢益辉老师GitHub的说明文档,曾柯老师极大地简化了一 ...

  3. R语言学习笔记

    向量化的函数 向量化的函数 ifelse/which/where/any/all/cumsum/cumprod/对于矩阵而言,可以使用rowSums/colSums.对于“穷举所有组合问题" ...

  4. R语言学习笔记-变量的作用域

    R语言是如何将变量值和变量绑定的 在r语言中,当前的 workspace就是global enviroment,当输入变量名时,首先会在global enviroment中搜索该变量,如有,则将它显示 ...

  5. R语言学习笔记:使用reshape2包实现整合与重构

    R语言中提供了许多用来整合和重塑数据的强大方法. 整合 aggregate 重塑 reshape 在整合数据时,往往将多组观测值替换为根据这些观测计算的描述统计量. 在重塑数据时,则会通过修改数据的结 ...

  6. R语言学习笔记:使用tcltk包显示进度条

    一般在跑耗时较长的程序时,我们不知道程序到底有没有正常跑着,或者在爬虫的时候不知道爬到什么时候断了.因此可以添加进度条来显示当前进度,观察进度是否有进展.当进度条卡住的时候,可以判断程序断线,从而可以 ...

  7. R语言学习笔记之: 论如何正确把EXCEL文件喂给R处理

    博客总目录:http://www.cnblogs.com/weibaar/p/4507801.html ---- 前言: 应用背景兼吐槽 继续延续之前每个月至少一次更新博客,归纳总结学习心得好习惯. ...

  8. R语言学习笔记:基础知识

    1.数据分析金字塔 2.[文件]-[改变工作目录] 3.[程序包]-[设定CRAN镜像] [程序包]-[安装程序包] 4.向量 c() 例:x=c(2,5,8,3,5,9) 例:x=c(1:100) ...

  9. R语言学习笔记:字符串处理

    想在R语言中生成一个图形文件的文件名,前缀是fitbit,后面跟上月份,再加上".jpg",先不百度,试了试其它语言的类似语法,没一个可行的: C#中:"fitbit&q ...

随机推荐

  1. ios 新建app iphone 、 ipad or universal ?

    很久没有关注这个新建app的  时候 选什么的问题了, 因为我们一般在公司 都是 已经建立好的app 直接 在那上面开发. 所以很久不建立新app 遇到新的app需要你自己去创建的时候 可能就会 有突 ...

  2. 使用PHP实现查找附近的人

    https://zhuanlan.zhihu.com/p/31380780 LBS(基于位置的服务) 查找附近的人有个更大的专有名词叫做LBS(基于位置的服务),LBS是指是指通过电信移动运营商的无线 ...

  3. springBoot注解搜集

    一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...

  4. python下multiprocessing和gevent的组合使用

    python下multiprocessing和gevent的组合使用 对于有些人来说Gevent和multiprocessing组合在一起使用算是个又高大上又奇葩的工作模式. Python的多线程受制 ...

  5. ARM 之一 ELF文件、镜像(Image)文件、可执行文件、对象文件 详解

    [转]https://blog.csdn.net/ZCShouCSDN/article/details/100048461 ELF 文件规范   ELF(Executable and Linking ...

  6. 阶段5 3.微服务项目【学成在线】_day17 用户认证 Zuul_13-用户退出-前端

    调试前端的退出 logout方法 找到路由 退出对应的组件页面 这就是退出的组件 退出的方法 把这两个js的引用,从上面复制到下面引用.因为可能存在js的冲突问题. 资料里面给了一个前端 整个覆盖当前 ...

  7. 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_04-用户认证技术方案-SpringSecurityOauth2

    2.3 Spring security Oauth2认证解决方案 本项目采用 Spring security + Oauth2完成用户认证及用户授权,Spring security 是一个强大的和高度 ...

  8. Python 函数返回值、作用域

    函数返回值 多条return语句: def guess(x): if x > 3: return "> 3" else: return "<= 3&qu ...

  9. 普罗米修斯Prometheus监控安装

    普罗米修斯Prometheus监控安装 架构: 服务端:192.168.0.204 客户端:192.168.0.206 环境准备:所有节点安装go 语言环境 rz go1.12.linux-amd64 ...

  10. CSS History

    Preface 如果只是要写程序,那的确是不需要这么麻烦,上来直接看Syntax,动手写上至少300行代码,做上3个应用程序,这门语言你也就差不多会用了,接下来不过就是模式,特殊的地方以及记住一些函数 ...