I will rewrite part of my comments as an answer to the question
How the order of operations in R works?
@Danielfalbel, what you said has to do with the concept of "precedence" and is defined in ?Syntax, which shows the precedence of all R operators. Below I copy the R help text, showing the hierarchy of operators (the first is the one with the highest precedence):
:: ::: access variables in a namespace
$ @ component / slot extraction
[ [[ indexing
^ exponentiation (right to left)
- + unary minus and plus
: sequence operator
%any% special operators (including %% and %/%)
* / multiply, divide
+ - (binary) add, subtract
< > <= >= == != ordering and comparison
! negation
& && and
| || or
~ as in formulae
-> ->> rightwards assignment
<- <<- assignment (right to left)
= assignment (right to left)
? help (unary and binary)
Note that %any%
(that is, any operator defined as % %
) is one of them. That is, when defining an operator %<%
he has greater precedence than +
, but less than ^
(note that in your example exp %<% 2 ^ 2
gives the result you wanted).
The problem (problem) is trying to change the precedence of your function. A trivial solution would be to use parentheses,
exp %<% (1+1)
but from what you said you wanted to avoid them.
Taking a look at the operator definitions, I think that if possible this task can be quite difficult (you will probably need to define classes that work alongside primitive operators explicitly), but I could be wrong. Behold ?groupGeneric and ?S4groupgeneric for some details on how primitive operators are implemented.
You don’t want to do
exp %<% (1+1)
, right?– Julio Trecenti
exact, did not want to use parentheses
– Daniel Falbel
I do not know if there is how to do this, even because it would be a mess if this was something customizable, making it very difficult to understand/read codes, besides causing others to stop working as expected.
– Rcoster
I agree, it can create confusion. But there are other functions that do this kind of thing... For example the operator
<-
, when I dox <- 1 + 1
,1 + 1
is calculated before<-
be actually used.– Daniel Falbel
Daniel, what did you say has to do with ? Syntax (https://stat.ethz.ch/R-manual/R-devel/library/base/html/Syntax.html), showing the precedence of all operators. Note that
%any%
is one of them. That is, when defining an operator%<%
he has less precedence than+
, but greater than*
(note that in your exampleexp %<% 2 * 2
gives the result you wanted). The problem (problem) is trying to change the precedence of your function. I think if it is possible this task is difficult (you will need to define classes), but I can be wrong. Veja ? groupGeneric e ? S4groupgeneric– Julio Trecenti
Vixe, I said nonsense. It also does not work with
*
, for the precedence of%any%
is bigger. But it works with:
, for example.exp %<% 1:10
results in a vector with the results. Orexp %<% 2^2
, who also gives you what you wanted.– Julio Trecenti
@Juliotrecenti is that, like
+
(binary) takes precedence to<-
, then inx <- 1 + 1
the sum is made before assigning. In this case, Daniel’s operator%<%
is a special operator who takes precedence over+
(binary), then the%<%
will run first unless parentheses are placed. I think you should put this as a response.– Carlos Cinelli
Daniel, +1 good question. I don’t think you can change the precedence of a binary operator for the reasons @Rcoster listed.
– Carlos Cinelli
@Carloscinelli I didn’t put as an answer because I don’t have a solution and I can’t prove that it’s impossible (or extremely difficult) to make this change of precedence, so it’s not really an answer. Makes sense? I still don’t know how to use the OS right.
– Julio Trecenti
@Juliotrecenti Yes, no problem, Daniel asked two things
"Como funciona a ordem das operações no R? E como posso alterá-las?"
you can explain the order of operations, then you will be answering a part of the question, and when the second part has no problem to say that you do not know.– Carlos Cinelli
@Carloscinelli +1 for the tip. Convinced me, thank you.
– Julio Trecenti