-
Notifications
You must be signed in to change notification settings - Fork 19
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
Support R variables with . and data.frames with $ #11
Conversation
Thanks for your addition! But it is a difficult one:
library(devtools)
test_package("whisker") I suggest that we implement it as an option: Would that be an option? Best |
Ah yes! I see now on the mustache spec for interpolation the using '.' as the context sigil is written into the spec. In this (discussion thread)[https://github.com/mustache/spec/issues/52] there is mention of the difficulty of writing about the context stack in an implementation agnostic way. The gist is that context stack implementation should be left to the implementors. I'll ask at mustache/spec for clarification. In the mean time, why not add |
Got some feedback from other Mustache implementors. Specifically he says
So whisker should free to define interpolation to be the natural usage for R. In fact, other Mustache implementors encourage this! Looking at I've played around with the tests, and using '$' instead of '.' works except for the method chaining part of the spec since R doesn't allow for chaining of data frames. Maybe instead of having options |
To clarify: I'm not a Mustache maintainer. I'm a template engine implementor, like you. |
@groue sorry for the misunderstanding. I've updated to comment to avoid any further confusion. |
@KentonWhite You're welcome. Actually Mustache is no longer maintained: the people who are responsible of the specification repository haven't posted any answer to any comment in at least a year now. |
Interesting thread. I think that this is a problem with R that I have has some grumblings. The allowed use of |
I'm happy to add |
"strict" to render. Setting it to FALSE allows for "." in names and use "$" for splitting which is a more natural R syntax. Thanks to @KentonWhite (issue #11)
Thanks! Closing since changes merged into master. |
I was incorporating whisker into ProjectTemplate at a user request and was having problems handling variables that have '.' in their name as well as referencing columns of a data.frame using the '$' notation.
Sure enough, in the code there was a TODO about this!
The code looks like it is splitting variables on '.' to create nested object method calls. E.g if I pass in
"foo.bar"
the variable name isfoo
and the method name to call onfoo
isbar
. To the best of my knowledge, method calls on objects in R don't use '.'.The common use cases that I'm aware of are using '$' to reference a column of data on a data.frame and using '@' to access a slot on an object. I've updated the code to handle variable names with '.' and to handle the '$' on data.frames. E.g. if I pass in
"foo$bar"
it will take the variable name to befoo
and the column to return asbar
. If I pass"foo.bar"
it will take the variable name to befoo.bar
.I didn't make changes for the slots, but made a TODO note.