-
Notifications
You must be signed in to change notification settings - Fork 64
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
Init an article about wrapping glue #338
Conversation
vignettes/wrappers.Rmd
Outdated
Spoiler alert: here's the correct way to write such a wrapper: | ||
|
||
```{r} | ||
myglue <- function(..., .envir = parent.frame()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we find a more compelling example based on one of our glue wrapper? I think it's pretty unusual to want to change the delimiters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on my own experience and, now, from GitHub searches, I don't have any better ideas. People really do write wrappers to fiddle with the delimiters and I think other examples are probably too exotic.
But if there's a concrete alternative, I'm happy to have a go.
vignettes/wrappers.Rmd
Outdated
Let's make new artificial versions of our functions that make it easy to tell where the inner `glue()` call is getting its values. | ||
|
||
```{r} | ||
myglue1 <- function(...) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would be easier to explain first, before you introduce all the complexities of a real example. Maybe something like this:
x <- 0
myglue1 <- function(...) {
x <- 1
myglue2(...)
}
myglue2 <- function(...) {
x <- 2
glue::glue(...)
}
myglue2("{x}")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe:
x <- 0
y <- 0
z <- 0
myglue1 <- function(...) {
x <- 1
y <- 1
myglue2(...)
}
myglue2 <- function(...) {
x <- 2
glue::glue(...)
}
myglue2("{x} {y} {z}")
OK I tried to respond to the feedback. I'm afraid it might now be in some weird intermediate state between how I would approach writing this and how you would. But see what you think. |
|
||
myglue0(fn_def, NAME = "one_plus_one", BODY = "1 + 1") | ||
named_list_to_items <- function(x) { | ||
my_glue("\\item{<@names(x)@>}{<@x@>}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that I think of it, is there a reason to not do just <
and >
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would work in this exact example, but I don't want to model that in a vignette. I know some folks will copy exactly what they see here and I think <
and >
are too likely to collide with text in the template that should not get evaluated. Generally speaking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about ((
or [[
? I'm just thinking that <@
looks hard to type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well all of those come up a lot in real R code (like, the code you might want to evaluate) so again I shied away from them. It really sucks that you can't use <<
. You can't even triple them <<<
because that also matches knitr's regex (unintentionally, I think).
Here's an abbreviated excerpt of the roxygen comment that generates the documentation for the starwars dataset in dplyr: | ||
|
||
```r | ||
#' \describe{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example is much better!
@@ -25,7 +25,7 @@ Therefore, you'd prefer to use `<<` and `>>` as the opening and closing delimite | |||
Spoiler alert: here's the correct way to write such a wrapper: | |||
|
|||
```{r} | |||
myglue <- function(..., .envir = parent.frame()) { | |||
my_glue <- function(..., .envir = parent.frame()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth a footnote pointing out that this is the same pattern you use in abort()
/cli_abort()
wrappers? And in defer()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking the same! I added a sentence in the intro. I think it's worthy to point this out.
Closes #281