R语言学习笔记:glue包实现变量传参
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包实现变量传参的更多相关文章
- C语言学习笔记 (006) - 二维数组传参的三种表现形式
# include <stdio.h> # include <stdlib.h> # define M # define N int getdate(int (*sp)[M]) ...
- R语言学习笔记︱Echarts与R的可视化包——地区地图
笔者寄语:感谢CDA DSC训练营周末上完课,常老师.曾柯老师加了小课,讲了echart与R结合的函数包recharts的一些基本用法.通过对比谢益辉老师GitHub的说明文档,曾柯老师极大地简化了一 ...
- R语言学习笔记
向量化的函数 向量化的函数 ifelse/which/where/any/all/cumsum/cumprod/对于矩阵而言,可以使用rowSums/colSums.对于“穷举所有组合问题" ...
- R语言学习笔记-变量的作用域
R语言是如何将变量值和变量绑定的 在r语言中,当前的 workspace就是global enviroment,当输入变量名时,首先会在global enviroment中搜索该变量,如有,则将它显示 ...
- R语言学习笔记:使用reshape2包实现整合与重构
R语言中提供了许多用来整合和重塑数据的强大方法. 整合 aggregate 重塑 reshape 在整合数据时,往往将多组观测值替换为根据这些观测计算的描述统计量. 在重塑数据时,则会通过修改数据的结 ...
- R语言学习笔记:使用tcltk包显示进度条
一般在跑耗时较长的程序时,我们不知道程序到底有没有正常跑着,或者在爬虫的时候不知道爬到什么时候断了.因此可以添加进度条来显示当前进度,观察进度是否有进展.当进度条卡住的时候,可以判断程序断线,从而可以 ...
- R语言学习笔记之: 论如何正确把EXCEL文件喂给R处理
博客总目录:http://www.cnblogs.com/weibaar/p/4507801.html ---- 前言: 应用背景兼吐槽 继续延续之前每个月至少一次更新博客,归纳总结学习心得好习惯. ...
- R语言学习笔记:基础知识
1.数据分析金字塔 2.[文件]-[改变工作目录] 3.[程序包]-[设定CRAN镜像] [程序包]-[安装程序包] 4.向量 c() 例:x=c(2,5,8,3,5,9) 例:x=c(1:100) ...
- R语言学习笔记:字符串处理
想在R语言中生成一个图形文件的文件名,前缀是fitbit,后面跟上月份,再加上".jpg",先不百度,试了试其它语言的类似语法,没一个可行的: C#中:"fitbit&q ...
随机推荐
- ORA-00911: invalid character 错误解决集锦
转: ORA-00911: invalid character 错误解决集锦 参考https://www.linuxidc.com/Linux/2017-05/144361.htm ORA-00911 ...
- memcache安装与简单介绍
本文参考自菜鸟教程中的内容. 安装 安装memcache的时候,请切换为root用户 root@centos # wget http://www.memcached.org/files/memcach ...
- React调试——visual studio code
原文链接:Using React in Visual Studio Code 原文链接:Live edit and debug your React apps directly from VS Cod ...
- 本地git仓库推送到服务器自建的git仓库实现目录文件同步教程
首先,先在服务器上安装git,如果有git的话就不用走这一步了 yum安装git [root@iZuf6fazwjb6lb3z82smzoZ ~]# cd src/ [root@iZuf6fazwjb ...
- Linux (Ubuntu)提示ifconfig:找不到命令
使用命令: sudo apt install net-tools 安装一下net-tools
- Lombok子类与父类的@Builder注解冲突
解决方法之一:去掉父类的@builder 缺点:父类不能使用Builder构造器模式 来源:https://www.cnblogs.com/lori/p/10266508.html
- 【ARTS】01_39_左耳听风-201900805~20190811
ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...
- todolist形式的搜索框,分开组件写的,点击上下键时,框内显示当前选中的内容
### 首先 安装react 脚手架 cnpm install create-react-app -g //只需要在电脑上安装一次就好了,以后不用再下载了 ### 创建项目 crea ...
- Spark学习笔记0——简单了解和技术架构
目录 Spark学习笔记0--简单了解和技术架构 什么是Spark 技术架构和软件栈 Spark Core Spark SQL Spark Streaming MLlib GraphX 集群管理器 受 ...
- 三节课MINI计划第三周
第一部分 微信读书的产品分析 一.任务背景 二.做什么 三.TASK8 业务流程图 四.TASK9 五.周会 六.周报