Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to write glue() wrappers #281

Open
daattali opened this issue Nov 3, 2022 · 6 comments
Open

Document how to write glue() wrappers #281

daattali opened this issue Nov 3, 2022 · 6 comments

Comments

@daattali
Copy link

daattali commented Nov 3, 2022

I'm developing a package that uses a lot of glue. In each usage I want to use .trim = FALSE. It's very troublesome to add .trim = FALSE parameter to every glue call.

It would be wonderful to be able to set a global option for that, or for any other parameters (I also use .open = "{{" in every call).

@jimhester
Copy link
Collaborator

Write a wrapper function that does what you want?

@daattali
Copy link
Author

I did try that but unfortunately it doesn't work, at least not trivially, because of the environments in which it's called.

@daattali
Copy link
Author

For example:

myglue <- function(...) {
  glue::glue(..., .open = "{{", .close = "}}")
}

# this works
x <- 5
message(myglue("x: {{x}}"))

fxn <- function() {
  y <- 7
  # this doesn't work
  message(myglue("y: {{y}}"))
}
fxn()

@jennybc
Copy link
Member

jennybc commented Dec 22, 2022

You need to wire up .envir in a wrapper function. Like so:

myglue <- function(..., .envir = parent.frame()) {
  glue::glue(..., .open = "{{", .close = "}}", .envir = .envir)
}

# this works
x <- 5
message(myglue("x: {{x}}"))
#> x: 5

fxn <- function() {
  y <- 7
  # this also works
  message(myglue("y: {{y}}"))
}
fxn()
#> y: 7

Created on 2022-12-22 with reprex v2.0.2.9000

@DavisVaughan
Copy link
Member

@jennybc I feel like an example of this pattern would be useful in the docs

@hadley hadley changed the title Support an option to globally set glue parameters such as .trim or .open Document how to write glue() wrappers Jan 25, 2023
@hadley
Copy link
Member

hadley commented Jan 25, 2023

And improve the documentation for the .envir argument.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants