Search code examples
rdead-code

Identifying if/else statements in R and extracting expressions from them


I would like to extract expr_a, expr_b, expr_c from the if/else block in the following function:

test <- function() {

    result <- if (expr_a)
                  expr_b
              else
                  expr_c

    return(result)
}

for the purpose of dead-code elimination (I've seen the rco library but it currently doesn't support all the cases that I'm working with).

I would like to be able to then replace the entire loop with expr_b if eval(expr_a) == TRUE or expr_c otherwise.

The issue with regex-based approaches to this problem is that there are so many possible variations on this, and I'm not sure how to capture them all. For example, the code above is valid R code for one line expressions. Multi-line expressions surrounded by brackets can also be written e.g.:

else
{
    # ...
}

Is there any way to identify and capture every possible if/else statement programmatically in R?


An example of what my real starting point looks like:


test <- function() {
    my_var <- list(my_bool = FALSE)
    my_result <- if(my_var$my_bool) 1
                 else my_var$my_bool + 2
    return(my_result)
}

The ideal output would be: list(expression(my_var$my_bool), expression(1), expression(my_var$my_bool + 2))


Solution

  • Figured it out! as.list can be used to break up calls into the syntax tree.

    For example:

    
    example <- quote(
        if (object) {
            print(result_true)
        } else {
            print(result_false)
        }
    )
    
    as.list(example)
    # [[1]]
    # `if`
    # 
    # [[2]]
    # object
    # 
    # [[3]]
    # {
    #     print(result_true)
    # }
    # 
    # [[4]]
    # {
    #     print(result_false)
    # }