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. Ionic4.x Javascript 扩展 ActionSheet Alert Toast Loading 以及 ionic 手势相 关事件

    1.ActionSheet 官方文档:https://ionicframework.com/docs/api/action-sheet <ion-header> <ion-toolb ...

  2. C语言中的异常处理

    一 前言: 异常处理,对于做面向对象开发的开发者来说是再熟悉不过了,例如在C#中有 try { ... } catch( Exception e){...} finally{ ..... } 在C++ ...

  3. 017-mac下project

    下载安装使用 http://www.3322.cc/soft/35716.html

  4. C/C++程序所占用内存区域

    C/C++编译的程序所占用内存区域一般分为以下5个部分: 栈区(stack):由编译器自动分配和释放,用来存放函数的参数.局部变量等.其操作方式类似于数据结构中的栈. 堆区(heap):一般由程序员分 ...

  5. 123457123456#0#-----com.threeapp.BabyLeaningEnglish01----精品儿童学英语

    com.threeapp.BabyLeaningEnglish01----精品儿童学英语

  6. Python SciPy库——插值与拟合

    插值与拟合 原文链接:https://zhuanlan.zhihu.com/p/28149195 1.最小二乘拟合 实例1 # -*- coding: utf-8 -*- import numpy a ...

  7. Kafka管理与监控——broker宕机后无法消费问题

    背景 因磁盘满了,导致kafka所有的服务器全部宕机了,然后重启kafka集群,服务是启动成功了,但有一些报错: broker1: broker2: broker3:一直在刷以下错误信息 虽然报了这些 ...

  8. 异步回收fork出的子进程(僵尸进程)

    #include <stdio.h> #include <stdlib.h> #include <signal.h> #include <unistd.h&g ...

  9. SQL Server 2008中的代码安全===主密钥

    在SQL Server中的加密由层次结构形式进行处理以提供多级别的安全.SQL Server包含两个用于加密数据的密钥类型.如下图: 1.服务器主密钥(Service Master Key),位于层次 ...

  10. topK问题

    概述 在N个乱序数字中查找第K大的数字,时间复杂度可以减小至O(N). 可能存在的限制条件: 要求时间和空间消耗最小.海量数据.待排序的数据可能是浮点型等. 方法 方法一 对所有元素进行排序,之后取出 ...