How to obtain all the variables in an R code expression?

前端 未结 2 1737
余生分开走
余生分开走 2021-01-14 09:07

How can an expression in R be decoded to get all the variables involved?

For example if you have:

z<-x+y;

get_all_variables(z);

[1] \'x\' \'y\'
         


        
相关标签:
2条回答
  • 2021-01-14 09:26

    You can use all.vars, but you need to quote your expression:

    all.vars(quote(x + y))
    # [1] "x" "y"
    

    You can't just use z as you describe it contains an evaluated expression (i.e. the result of the expression), not the expression itself. You can write a function that removes one step:

    get_all_variables <- function(expr) all.vars(substitute(expr))
    get_all_variables(x + y)
    # [1] "x" "y"
    

    But you will not be able to recover the expression from z, unless you create z by z <- quote(x + y) or some such.

    If you have the expression in a string, then you can use @sunny's technique combined with all.vars:

    all.vars(parse(text="z <- x + y"))
    # [1] "z" "x" "y"
    

    Though obviously you get z as well. As always, don't evaluate arbitrary text with parse in case someone Bobby Tables you.

    0 讨论(0)
  • 2021-01-14 09:36

    So far as I know, you would need to store the expression in a string. Then you can parse it with getParseData:

    txt <- "z<-x+y"
    
    sf <- srcfile("txt")   
    df = getParseData(sf)    
    df$text[df$token=="SYMBOL"][2:3]
    

    For further edification, here is what the result of getParseData looks like in its entirety:

       line1 col1 line2 col2 id parent       token terminal text
    11     1    1     1    6 11      0        expr    FALSE     
    1      1    1     1    1  1      3      SYMBOL     TRUE    z
    3      1    1     1    1  3     11        expr    FALSE     
    2      1    2     1    3  2     11 LEFT_ASSIGN     TRUE   <-
    10     1    4     1    6 10     11        expr    FALSE     
    4      1    4     1    4  4      6      SYMBOL     TRUE    x
    6      1    4     1    4  6     10        expr    FALSE     
    5      1    5     1    5  5     10         '+'     TRUE    +
    7      1    6     1    6  7      9      SYMBOL     TRUE    y
    9      1    6     1    6  9     10        expr    FALSE    
    
    0 讨论(0)
提交回复
热议问题