-
Notifications
You must be signed in to change notification settings - Fork 10
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
WISH: Support passing name-value pairs via '.args' of mirai() #54
Comments
Yes, I guess something is merited here!
I like this suggestion.
Yes, that is precisely one of my objectives. Also why I construct the environment upfront to detect non-named arguments in Thanks for the insight - much appreciated. I will think on the implementation. Copying @wlandau. Might just make it in time for |
This is implemented in mirai 0.8.2.9037. Completely non-breaking and very lightweight.
There is validation by Your excellent example now works: expr <- quote(2 * a + b)
globals <- list(a = 3, b = 4)
m <- mirai(expr, .args = globals) @wlandau FYI this should allow you to simplify your use of |
Thanks. Not at a computer for a while, but what happens now if you do |
The list is named so it is read as a name = value pair, and it errors when the environment is constructed. The same as it would if you passed It is documented. It is strictly an either / or. |
So, this works: $ R --quiet --vanilla
ee <- quote(1+2); m <- mirai::mirai(ee)
m$data
## [1] 3 but not this: $ R --quiet --vanilla
m <- local({ ee <- quote(1+2); mirai::mirai(ee) })
m$data
## 'miraiError' chr Error in eval(expr = envir[[".expr"]], envir = envir, enclos = NULL): object 'ee' not found It works if you rename FWIW, at least for me, too much NSE:ing tend to bring me down these type of rabbit holes. EDIT: After submitting this comment, I realize it belongs to #49, but since I've already posted it, #49 is closed, and the updates you've done here might have introduced this, I keep it here. |
Thanks so much Henrik! This does indeed belong in #49. The I think it's worth the effort getting these things right - and the process has been shortcut with an expert eye such as yours. |
Thank you. Works like a charm now. |
Picking up on the side-discussion on argument
.args
that started in #49 (comment).As it stands now (mirai 0.8.2.9036), the
.args
argument ofmirai()
takes a vector of symbols as input. The following are all working examples:Issues
Too broad acceptance
?mirai::mirai
says that.args
takes an "(optional) list of objects". The above.args = c(a, b)
example does not use a list. One can also debate whether.args = data.frame(a, b)
should work (although technically it's also a list).No validation of '.args'
There is no up-front validation of what is passed to
.args
. Instead, mis-specification of.args
results in run-time errors from worker failing to evaluate the expression. For example,No support for name-value pairs
I think there will be a lot of users that will expect being able to do:
This will allow users to write more clear code. Currently, anyone who wishes to pass a named list of values has to resort to
do.call()
, which counter to the "neat API" that mirai tries to support. For example, as it stands now, we have to write code like:instead of a much cleaner and less obscure:
Suggestion
Redesign current behavior to support only the
.args = c(a, b)
form.Add support for specifying name-value elements, i.e.
.args = list(a = 3, b = 4)
.I think those will be the most common use cases. Using
c()
for one andlist()
for the other could help distinguish the two. Of course, both forms could be handled usinglist()
too.FWIW, the fact that
c(a, b)
uses symbols has the advantage that static-code inspection (e.g. codetools validation byR CMD check
) can pick up typos. In contrast,.args = c("a", "btypo")
won't be detected until run-time.The text was updated successfully, but these errors were encountered: