-
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
Handling NULL inside braces #100
Comments
I see that this might be handled with transformers but may be this should be a default? library(glue)
null_transformer <- function(text, envir) {
out <- identity_transformer(text, envir)
if (is.null(out)) {
return("NULL")
}
out
}
world <- NULL
glue("Hello {world}!", .transformer = null_transformer)
#> Hello NULL! Created on 2018-07-24 by the reprex package (v0.2.0). |
Two more ways you can hit this bug: str(glue("hi {}"))
expected return
I encountered this with an empty else in a conditional str(glue("hi {if(FALSE) 'there'}")
This example also breaks reprex somehow, but that is question for another day. |
@echasnovski, I think the |
Yeah, maybe. However, using My feeling is that empty string might be a better default choice, and |
Great point, yes NULL is sometimes desirable. Also agree empty string default + well documented |
do you want to promote this to a PR? If not I'm happy to. @jimhester what do you think? |
you're totally right that the |
You are right this is a design choice, but I also agree that a null transformer should be added to the vignette. If I might suggest an addition, have the null transformer take a string for the text to insert. null_transformer <- function(str = "NULL") {
function(text, envir) {
out <- glue::identity_transformer(text, envir)
if (is.null(out)) {
return(str)
}
out
}
}
glue::glue("hi {}", .transformer = null_transformer("{NULL}"))
#> hi {NULL} Created on 2018-10-05 by the reprex package (v0.2.1) |
this issue is one year old. |
I would also find it very helpful if glue issued a warning when NULLs are present. However, after getting frustrated with tracking down the NULLs in my complex code, I figured out that a transformer can create the warnings I would like. If you do not want to include such warnings into glue, maybe something like this would be helpful in the vignette?
|
For me this is unexpected behavior. I might understand this output if one of glued strings is
NULL
(as after #46) but this is a case of evaluating R code.Created on 2018-07-23 by the reprex package (v0.2.0).
Sorry if this is considered a duplicate of #45 or #84.
The text was updated successfully, but these errors were encountered: