What is the difference between sym() and parse_expr() in the rlang package?

前端 未结 2 824
感动是毒
感动是毒 2021-01-19 15:55

Using the rlang package, I wonder what is the difference between sym() and parse_expr(). Consider for example the following expressions:

         


        
相关标签:
2条回答
  • 2021-01-19 16:41

    Referencing my answer to this question:

    A symbol is a way to refer to an R object, basically the "name" of an object. So sym is similar to as.name in base R. parse_expr on the other hand transforms some text into R expressions. This is similar to parse in base R.

    Expressions can be any R code, not just code that references R objects. So you can parse the code that references an R object, but you can't turn some random code into sym if the object that the code references does not exist.

    In general, you will use sym when your string refers to an object (although parse_expr would also work), and use parse_expr when you are trying to parse any other R code for further evaluation.

    For your first example, a can be both a name that references an object AND an expression, so turning it into either a sym or parse_expr would practically mean the same thing. In fact, R implicitly converts the expression to a sym when it can, as shown in your first example.

    For your last example however, a - b is really intended to be an expression (unless you have an R object that is weirdly named a - b). By printing the following, you will see that using sym vs parse_expr for R code that is intended to be an expression, not an R object produces two different results:

    > quo(!!sym('a - b'))
    <quosure: global>
    ~`a - b`
    
    > quo(!!parse_expr('a-b'))
    <quosure: global>
    ~a - b
    

    Here, sym turns a - b into a name/symbol of an object, hence the back ticks around a - b, while parse_expr turns it into an expression as expected.

    0 讨论(0)
  • 2021-01-19 16:53

    To augment the previous answer, note that ex5 and ex6 are not in fact identical.

    a <- 5
    b <- 3
    eval_tidy(ex6)
    # [1] 2
    eval_tidy(ex5)
    # Error in eval_tidy(ex5) : object 'a - b' not found
    `a - b` <- pi
    eval_tidy(ex5)
    # [1] 3.141593
    
    0 讨论(0)
提交回复
热议问题