0

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.

Dom
  • 17
  • 4
  • instead of `if` you can try to do it multiplicatively like `(Condition2 == TRUE)*2`. Did you try that? – Taufi Apr 21 '21 at 16:01
  • Hey, this would lead to a problem if there would the code use a * instead of a + `1* (Condition2 == TRUE)*2 *3` Because then the output would be 0 and not 3 as expected, but yes for addition your answer would work – Dom Apr 21 '21 at 16:04
  • The error you get is because `if` returns `NULL` if it doesn't execute. So yeah, you have to have the `else` in there or use multiplication. Also, bluntly, you are writing ugly and hard-to-read code in the pursuit of "efficiency"... but I doubt if you will actually achieve a serious speedup this way. (Have you checked, using `System.time` or `bench::mark()`?) I'd suggest focusing on clean and easy-to-read code. – dash2 Apr 21 '21 at 16:07
  • Yep you are right, the code above is not written nicely, this should just be a minimal example of my more complex code - therefore I tried to show the structure of it – Dom Apr 21 '21 at 16:09
  • So then doing it in a single statement will be even messier... and the speedup will be even smaller relative to what else you are doing, because all you really save is the time of a single assignment. – dash2 Apr 21 '21 at 16:10
  • The problem is that doing it with assignment each time would not work, because there are lines I would like to ignore, that are within a function. So doing it stepwise would not work in this case :( and using an additional assignment within the function might not work easily in some cases – Dom Apr 21 '21 at 16:14

0 Answers0