生成数据报告是数据分析过程的关键环节。R 内置paste() 函数可以实现,本文介绍glue包实现字符串模板,可以更加直观生成消息。
示例1
首先我们看一个简单示例,统计数据集行和列数,以及空值数量:
library(glue)
df <- mtcars
df[df$mpg<16, 2] <- NA
df
msg <- 'Dataframe Info: \n\n This dataset has {nrow(df)} rows and {ncol(df)} columns. \n There {ifelse(sum(is.na(df))>0, "are", "is")} {sum(is.na(df))} Missing Value'
glue(msg)
# Dataframe Info:
# This dataset has 32 rows and 11 columns.
# There are 10 Missing Value
name <- "Fred"
age <- 24
anniversary <- as.Date("1991-10-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 Fred, my age next year is 25, my anniversary is 星期六, 十月 12, 1991.
上面使用glue函数,参数为字符串表达式。其中变量后表达式使用{}进行包裹,整个过程比较直观。但要注意使用单引号包裹字符串msg内容,因为msg里面已经使用双引号。
示例2
我们在看一个示例,循环展示数据框中的内容。使用glue_data函数。
library(magrittr)
head(mtcars) %>% glue_data("* {rownames(.)} has {cyl} cylinders and {hp}")
# 执行结果
# * Mazda RX4 has 6 cylinders and 110
# * Mazda RX4 Wag has 6 cylinders and 110
# * Datsun 710 has 4 cylinders and 93
# * Hornet 4 Drive has 6 cylinders and 110
# * Hornet Sportabout has 8 cylinders and 175
# * Valiant has 6 cylinders and 105
上面使用数据管道,传输数据给glue_data,rownames(.) 依次获取前面传过来的记录。
示例3
下面示例展示换行格式,glue会按照我们给定的方式进行排版输出。
glue("
A formatted string
Can have multiple lines
with additional indention preserved
")
# Show in New Window
# A formatted string
# Can have multiple lines
# with additional 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 <- "Fred"
glue("My name is {name}, not {
{name}}.")
# My name is Fred, not {name}.
上面示例还展示了其他一些字面符号的输出方式。
总结
本文通过示例展示glue包提供的一些函数,用于实现字符串模板方式输出消息。还有其他功能强大的函数,如glue_sql可以安全拼接SQL,读者需要时可查阅官方文档。
本文参考链接:https://blog.csdn.net/neweastsun/article/details/121230516