I would like to execute a command spanning multiple lines and be able to conditionally include or exclude some lines from the code.
Minimal example (Console outputs are marked by starting with -->)
Condition1 <- TRUE
Condition2 <- FALSE
1 +
if(Condition1){2} +
3
--> [1] 6
1 +
if(Condition2){2} +
3
--> numeric(0)
The first calculation works as expected and produces 6 as a result. However for the second, I would hope to get 4 as an answer, but the console prints "numeric0". I thought I could solve it by placing the plus in the curly brackets:
1 +
if(Condition2){2 +}
3
But this unfortunately produces an error. Of course I could do it the following way
1 +
if(Condition2){2} else {0} +
3
---> [1] 4
However the code I want to use it for is more complex and it would be a lot easier if i would not need to specifiy an alternative that makes the calculation possible. Because then with changes in the code, I would also need to specify a useful alternative (simple example: Change the + in the example to *, then the 0 as the else statement is problematic)
Or I could do it the following, but this would make the code more complicated and less efficient.
a <- 1
if(Condition2){a<-a + 2}
(a <- a + 3)
--> [1] 4
So as a goal I would basically like to be able to skip the second line (if the condition is FALSE) and continue the connected command in the next line.
I already searched stackoverflow, but only found this question R Conditional evaluation when using the pipe operator %>%, which is similar but only answers the question for the use of pipe-operators. If possible I would like to not be dependent on pipe-operators for a solution.