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

Using glue() with pipes - double printing #201

Closed
lmeninato opened this issue Sep 19, 2020 · 2 comments
Closed

Using glue() with pipes - double printing #201

lmeninato opened this issue Sep 19, 2020 · 2 comments

Comments

@lmeninato
Copy link

So usually in a pipe we use "." to not pass the left-hand side of the pipe to the first parameter.
For instance:

1:5 %>% c(1:3, . , 1:3)
 # [1] 1 2 3 1 2 3 4 5 1 2 3

But it doesn't seem to work properly with glue:

"bob" %>% glue("hello {.}")
 # [1] bobhello bob
# I'd expect "hello bob" ...

I'm sure there's a simple fix/workaround out there, but I couldn't find anything online regarding this issue. The pipe is pretty central to the tidyverse, but I understand if the glue wasn't meant to be used with "unnamed" variables (such as the pipe's (.)), so the solution might just be some documentation/awareness around this, rather than changing glue to work with the above issue.

Using R 3.6.2 and glue 1.4.2

@lmeninato
Copy link
Author

lmeninato commented Sep 19, 2020

Actually looks like this more of an issue with how the pipe handles using the placeholder (.) when it appears in a nested expression.

It is straightforward to use the placeholder several times in a right-hand side expression. However, when the placeholder only appears in a nested expressions magrittr will still apply the first-argument rule. The reason is that in most cases this results more clean code.
...
The behavior can be overruled by enclosing the right-hand side in braces:

Magrittr docs

We can actually solve the problem by just adding brackets around the glue command in the pipe:

"bob" %>% 
  {glue("hello {.}")}
# [1] hello bob

Would you guys be interested in a pull request mentioning this in the documentation? Might not be necessary since it's referenced indirectly in the Magrittr documentation.

@jimhester
Copy link
Collaborator

Sure adding an example to the glue documentation about this seems worthwhile.

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

2 participants