11% File src / library / base / man / Control.Rd
22% Part of the R package , https : // www.R - project.org
3- % Copyright 1995 - 2022 R Core Team
3+ % Copyright 1995 - 2023 R Core Team
44% Distributed under GPL 2 or later
55
66\name {Control }
7+ \title {Control Flow }
78\alias {Control }
89\alias {if }
910\alias {else }
1314\alias {repeat }
1415\alias {break }
1516\alias {next }
16- \title { Control Flow }
17+ \alias {\% || \% }
1718\description {
1819 These are the basic control - flow constructs of the \R language. They
1920 function in much the same way as control statements in any Algol - like
@@ -28,18 +29,20 @@ while(cond) expr
2829repeat expr
2930break
3031next
32+
33+ x \%|| \% y
3134}
3235\arguments {
3336 \item {cond }{A length - one logical vector that is not \code {NA }.
3437 Other types are coerced to logical if possible , ignoring any class.
35- (Conditions of length greater than one are an error. )
38+ (Conditions of length greater than one are an error. )
3639 }
3740 \item {var }{A syntactical name for a variable. }
3841 \item {seq }{An expression evaluating to a vector (including a list and
3942 an \link {expression }) or to a \link {pairlist } or \code {NULL }. A
4043 factor value will be coerced to a character vector. This can be a
4144 long vector. }
42- \item {expr , cons.expr , alt.expr }{
45+ \item {expr , cons.expr , alt.expr , x , y }{
4346 An \emph {expression } in a formal sense. This is either a
4447 simple expression or a so - called \emph {compound expression }, usually
4548 of the form \code {\{ expr1 ; expr2 \}}.
6871 but this will not affect the next iteration. When the loop terminates ,
6972 \code {var } remains as a variable containing its latest value.
7073
74+ \code {x \%|| \% y } is a simple 1 - line function , an idiomatic way to call
75+ \preformatted {
76+ if (is.null(x )) y else x
77+ # or equivalently, of course,
78+ if (! is.null(x )) x else y }
79+ Inspired by Ruby , it was first proposed by Hadley Wickham.
7180}
7281\value {
7382 \code {if } returns the value of the expression evaluated , or
@@ -101,6 +110,13 @@ for(n in c(2,5,10,20,50)) {
101110}
102111f <- factor (sample(letters [1 : 5 ], 10 , replace = TRUE ))
103112for (i in unique(f )) print(i )
113+
114+ res <- {}
115+ res \%|| \% " alternative result"
116+ x <- head(x ) \%|| \% stop(" parsed, but *not* evaluated.." )
117+
118+ res <- if (sum(x ) > 7.5 ) mean(x ) # may be NULL
119+ res \%|| \% " sum(x) <= 7.5"
104120}
105121\keyword {programming }
106122\keyword {iteration }
0 commit comments