-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathglue.R
52 lines (51 loc) · 1.39 KB
/
glue.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#' Interpolation with glue
#'
#' @description
#' These functions are wrappers around [glue::glue()] and [glue::glue_data()],
#' which provide a powerful and elegant syntax for interpolating strings
#' with `{}`.
#'
#' These wrappers provide a small set of the full options. Use `glue()` and
#' `glue_data()` directly from glue for more control.
#'
#' @inheritParams glue::glue
#' @return A character vector with same length as the longest input.
#' @export
#' @examples
#' name <- "Fred"
#' age <- 50
#' anniversary <- as.Date("1991-10-12")
#' str_glue(
#' "My name is {name}, ",
#' "my age next year is {age + 1}, ",
#' "and my anniversary is {format(anniversary, '%A, %B %d, %Y')}."
#' )
#'
#' # single braces can be inserted by doubling them
#' str_glue("My name is {name}, not {{name}}.")
#'
#' # You can also used named arguments
#' str_glue(
#' "My name is {name}, ",
#' "and my age next year is {age + 1}.",
#' name = "Joe",
#' age = 40
#' )
#'
#' # `str_glue_data()` is useful in data pipelines
#' mtcars %>% str_glue_data("{rownames(.)} has {hp} hp")
str_glue <- function(..., .sep = "", .envir = parent.frame()) {
glue::glue(..., .sep = .sep, .envir = .envir)
}
#' @export
#' @rdname str_glue
str_glue_data <- function(.x, ..., .sep = "", .envir = parent.frame(),
.na = "NA") {
glue::glue_data(
.x,
...,
.sep = .sep,
.envir = .envir,
.na = .na
)
}