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

Init an article about wrapping glue #338

Merged
merged 7 commits into from
Aug 30, 2024
Merged

Init an article about wrapping glue #338

merged 7 commits into from
Aug 30, 2024

Conversation

jennybc
Copy link
Member

@jennybc jennybc commented Aug 29, 2024

Closes #281

vignettes/wrappers.Rmd Outdated Show resolved Hide resolved
vignettes/wrappers.Rmd Outdated Show resolved Hide resolved
@jennybc jennybc requested a review from hadley August 29, 2024 04:59
Spoiler alert: here's the correct way to write such a wrapper:

```{r}
myglue <- function(..., .envir = parent.frame()) {
Copy link
Member

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.

Copy link
Member Author

@jennybc jennybc Aug 29, 2024

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.

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(...) {
Copy link
Member

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}")

Copy link
Member

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}")

vignettes/wrappers.Rmd Outdated Show resolved Hide resolved
@jennybc
Copy link
Member Author

jennybc commented Aug 29, 2024

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.

vignettes/wrappers.Rmd Outdated Show resolved Hide resolved

myglue0(fn_def, NAME = "one_plus_one", BODY = "1 + 1")
named_list_to_items <- function(x) {
my_glue("\\item{<@names(x)@>}{<@x@>}")
Copy link
Member

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 >?

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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{
Copy link
Member

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!

vignettes/wrappers.Rmd Outdated Show resolved Hide resolved
@@ -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()) {
Copy link
Member

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()?

Copy link
Member Author

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.

@jennybc jennybc merged commit 432e264 into main Aug 30, 2024
13 checks passed
@jennybc jennybc deleted the how-to-wrap-glue branch August 30, 2024 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document how to write glue() wrappers
2 participants