I wrote a binary operator function for R (i.e. one with a name like %X%
so that instead of having to type %X%(a,b)
I can use the more convenient syntax a %X% b
. My goal is to have a wrapper for <-
that does things like log what was done to objects in that environment and check for a 'protected' attribute which would warn the user before overwriting that object.
All of this works, except that if I try do do something like a %X% b + c
inside the function all you see is a %X% b
_and that is also all it does; a gets assigned the value of b and c is completely ignored. a %X% (b + c)
works and so does %X%(a, b + c)
but the whole point of writing this as a binary operator is to avoid parentheses.
If I overwrite <-
, its sys.call()
sees everything to the left and right of it. Why does mine only see only the adjacent names from the command line?
Here is code that replicates this problem:
`%X%` <- function(...){
print(deparse(sys.call()));
}
a %X% 3 + 1:10;
The desired result is "a %X% 3 + 1:10" The observed result is "a %X% 3"
Thanks.
The options() function in R is used to set and examine different global options that affects the way in which R determines and returns its results.
Generally speaking, the $ operator is used to extract or subset a specific part of a data object in R. For instance, this can be a data frame object or a list. In this example, I'll explain how to extract the values in a data frame columns using the $ operator.
To run an R command, put the cursor on the line of the command and then click the Run button at the top of the file window. Or just press CTRL-Enter.
In R, you can pass a function as an argument. You can also pass function code to an argument. Then, you can assign the complete code of a function to a new object.
This has to do with operator precedence, see ?Syntax
for more help. It says that special operators (%any%
) like the one you are trying to define have higher precedence than the binary +
, so there is no other way (AFAIK) for you than to use parenthesis and do a %X% (b + c)
.
I suppose there is no way to lower the precedence of a custom operator. However, I think I found a cheesy workaround: I can name my operator <<-
I don't really understand how <-
dispatching works, and there must be dozens for different object-specific methods of <-
(none of which show up in methods(
<-)
. All I know is that when I override <-
all kinds of stuff breaks.
However, <<-
has a precedence close to that of <-
, and it's not particularly useful because for the occasional case where you really do need to write a global variable from inside a function or a loop, you can just use .GlobalEnv[['foo']] <- bar
or assign('foo',bar,env=.GlobalEnv)
.
Besides, since I'm only attempting to log and write-protect objects in .GlobalEnv in the first place, the new meaning I'm assigning to <<-
is even somewhat consistent with the original one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With