The R Project SVN R

Rev

Rev 88581 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/base/man/Syntax.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
67313 maechler 3
% Copyright 1995-2015 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
59616 ripley 6
% = is treated specially in the parser: see PR#14595
7
 
18286 ripley 8
\name{Syntax}
56186 murdoch 9
\alias{Syntax}
34966 maechler 10
\title{Operator Syntax and Precedence}
18286 ripley 11
\description{
59616 ripley 12
  Outlines \R syntax and gives the precedence of operators.
18286 ripley 13
}
14
\details{
15
  The following unary and binary operators are defined.  They are listed
16
  in precedence groups, from highest to lowest.
17
  \tabular{ll}{
56382 murdoch 18
    \code{:: :::}\tab access variables in a namespace\cr
21048 ripley 19
    \code{$ @}\tab component / slot extraction\cr
51163 murdoch 20
    \code{[ [[}\tab indexing\cr
18286 ripley 21
    \code{^}\tab exponentiation (right to left)\cr
22
    \code{- +}\tab unary minus and plus\cr
23
    \code{:}\tab sequence operator\cr
80353 luke 24
    \code{\%any\% |>}\tab special operators (including \code{\%\%} and \code{\%/\%})\cr
18286 ripley 25
    \code{* /}\tab multiply, divide\cr
18347 ripley 26
    \code{+ -}\tab (binary) add, subtract\cr
18286 ripley 27
    \code{< > <= >= == !=}\tab ordering and comparison\cr
28
    \code{!}\tab negation\cr
29
    \code{&  &&}\tab and\cr
30
    \code{| ||}\tab or\cr
31
    \code{~}\tab as in formulae\cr
32
    \code{-> ->>}\tab rightwards assignment\cr
59616 ripley 33
    \code{<- <<-}\tab assignment (right to left)\cr
18286 ripley 34
    \code{=}\tab assignment (right to left)\cr
35
    \code{?}\tab help (unary and binary)\cr
36
  }
37
  Within an expression operators of equal precedence are evaluated
59616 ripley 38
  from left to right except where indicated.  (Note that \code{=} is not
39
  necessarily an operator.)
57685 maechler 40
 
51163 murdoch 41
  The binary operators \code{::}, \code{:::}, \code{$} and \code{@} require
42
  names or string constants on the right hand side, and the first two
43
  also require them on the left.
18286 ripley 44
 
42950 ripley 45
  The links in the \bold{See Also} section cover most other aspects of
18286 ripley 46
  the basic syntax.
47
}
48
\note{
49
  There are substantial precedence differences between \R and S.  In
35579 ripley 50
  particular, in S \code{?} has the same precedence as (binary) \code{+ -}
51
  and \code{& && | ||} have equal precedence.
18286 ripley 52
}
24300 ripley 53
\references{
88581 hornik 54
  \bibshow{R:Becker+Chambers+Wilks:1988}
24300 ripley 55
}
18286 ripley 56
\seealso{
57
  \code{\link{Arithmetic}}, \code{\link{Comparison}}, \code{\link{Control}},
34712 ripley 58
  \code{\link{Extract}}, \code{\link{Logic}},
42950 ripley 59
  \code{\link{NumericConstants}}, \code{\link{Paren}},
60
  \code{\link{Quotes}}, \code{\link{Reserved}}.
18286 ripley 61
 
88890 smeyer 62
  Section \manual{R-lang}{Expressions}.
18286 ripley 63
}
67313 maechler 64
\examples{
65
## Logical AND ("&&") has higher precedence than OR ("||"):
66
TRUE || TRUE && FALSE   # is the same as
67
TRUE || (TRUE && FALSE) # and different from
68
(TRUE || TRUE) && FALSE
69
 
70
## Special operators have higher precedence than "!" (logical NOT).
71
## You can use this for \%in\% :
72
! 1:10 \%in\% c(2, 3, 5, 7) # same as !(1:10 \%in\% c(2, 3, 5, 7))
73
## but we strongly advise to use the "!( ... )" form in this case!
74
 
75
 
76
## '=' has lower precedence than '<-' ... so you should not mix them
77
##     (and '<-' is considered better style anyway):
82591 smeyer 78
\dontrun{## Consequently, this gives a ("non-catchable") error
67324 maechler 79
 x <- y = 5  #->  Error in (x <- y) = 5 : ....
67313 maechler 80
}
67324 maechler 81
}
18286 ripley 82
\keyword{documentation}
43772 maechler 83
\keyword{programming}