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

Passing named arguments using assigned objects #89

Closed
dchiu911 opened this issue Apr 18, 2018 · 4 comments
Closed

Passing named arguments using assigned objects #89

dchiu911 opened this issue Apr 18, 2018 · 4 comments

Comments

@dchiu911
Copy link

I can't seem to pass named arguments to glue when the values are assigned to objects with the same name as the interpolations:

# Example from ?glue
glue('My name is {name},',
     ' my age next year is {age + 1},',
     ' my anniversary is {format(anniversary, "%A, %B %d, %Y")}.',
     name = "Joe",
     age = 40,
     anniversary = as.Date("2001-10-12"))
# My name is Joe, my age next year is 41, my anniversary is Friday, October 12, 2001.

# Passing named arguments using different names
nm <- "Joe"
ag <- 40
an <- as.Date("2001-10-12")
glue('My name is {name},',
     ' my age next year is {age + 1},',
     ' my anniversary is {format(anniversary, "%A, %B %d, %Y")}.',
     name = nm,
     age = ag,
     anniversary = an)
# My name is Joe, my age next year is 41, my anniversary is Friday, October 12, 2001.

# Passing named arguments using same names
name <- "Joe"
age <- 40
anniversary <- as.Date("2001-10-12")
glue('My name is {name},',
     ' my age next year is {age + 1},',
     ' my anniversary is {format(anniversary, "%A, %B %d, %Y")}.',
     name = name,
     age = age,
     anniversary = anniversary)
# Error in eval(parse(text = text, keep.source = FALSE), envir) : 
#   promise already under evaluation: recursive default argument reference or earlier problems?
@egnha
Copy link
Contributor

egnha commented Apr 19, 2018

Thanks for catching this! Invariably, the error ”promise already under evaluation” means that a promise is stepping on its toes, i.e., trying to resolve to itself, e.g., (function(x = x) x)(). I've proposed a fix in #90, by staggering the promises.

@jimhester
Copy link
Collaborator

I don't think this is a problem, you don't need to use named arguments if the name is identical, you just omit them entirely.

@egnha
Copy link
Contributor

egnha commented Apr 19, 2018

@jimhester Good point about the example. Do you think something like glue(“Date: {date}”, date = localize_date(date)) ought to work, where date is in the current env? (#90 would enable this.)

@jimhester
Copy link
Collaborator

@egnha, that is a good point

x <- 1
glue::glue("{x}", x = x + 1)

Should definitely work, and currently does not, so we will need to do something like #90.

jimhester pushed a commit that referenced this issue May 1, 2018
* Interpolation variables can have same names as their values

Fixes #89
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

No branches or pull requests

3 participants