简体   繁体   中英

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 eg:

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))

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)
# }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM