Skip to content

Commit

Permalink
Implement alternative delimiters
Browse files Browse the repository at this point in the history
Fixes #23
  • Loading branch information
jimhester committed May 22, 2017
1 parent b4a07bb commit cbb10ef
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# glue 1.0.0.9000

`glue()` and `glue_data()` can now take alternative delimiters to `{` and `}`.
This is useful if you are writing to a format that uses a lot of braces, such
as LaTeX. (#23)

`collapse()` now returns 0 length output if given 0 length input (#28).

# glue 0.0.0.9000
Expand Down
8 changes: 6 additions & 2 deletions R/glue.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
#' # `glue_data()` is useful in magrittr pipes
#' library(magrittr)
#' mtcars %>% glue_data("{rownames(.)} has {hp} hp")
#'
#' # Alternative delimiters can also be used if needed
#' one <- "1"
#' glue("The value of $e^{2\\pi i}$ is $<<one>>$.", .open = "<<", .close = ">>")
#' @useDynLib glue glue_
#' @name glue
#' @export
Expand Down Expand Up @@ -86,8 +90,8 @@ to_data <- glue_data

#' @export
#' @rdname glue
glue <- function(..., .sep = "", .envir = parent.frame()) {
glue_data(.x = NULL, ..., .sep = .sep, .envir = .envir)
glue <- function(..., .sep = "", .envir = parent.frame(), .open = "{", .close = "}") {
glue_data(.x = NULL, ..., .sep = .sep, .envir = .envir, .open = .open, .close = .close)
}

#' @rdname glue
Expand Down
6 changes: 6 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ name <- "Fred"
glue("My name is {name}, not {{name}}.")
```

##### Alternative delimiters can be specified with `.open` and `.close`.
```{r}
one <- "1"
glue("The value of $e^{2\\pi i}$ is $<<one>>$.", .open = "<<", .close = ">>")
```

##### All valid R code works in expressions, including braces and escaping.
Backslashes do need to be doubled just like in all R strings.
```{r}
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ glue("
")
#>
#> leading or trailing newlines can be added explicitly
#> leading or trailing newlines can be added explicitly
```

##### `\\` at the end of a line continues it without a new line.
Expand All @@ -101,6 +101,14 @@ glue("My name is {name}, not {{name}}.")
#> My name is Fred, not {name}.
```

##### Alternative delimiters can be specified with `.open` and `.close`.

``` r
one <- "1"
glue("The value of $e^{2\\pi i}$ is $<<one>>$.", .open = "<<", .close = ">>")
#> The value of $e^{2\pi i}$ is $1$.
```

##### All valid R code works in expressions, including braces and escaping.

Backslashes do need to be doubled just like in all R strings.
Expand Down
8 changes: 6 additions & 2 deletions man/glue.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cbb10ef

Please sign in to comment.