# returns the expression supplied to the first argument
get_expr <- function(x) as.expression(substitute(x))
identical(expression(a + 1), get_expr(a + 1))
# TRUE
(x <- get_expr({a<-1;b<-2}))
# expression({
# a <- 1
# b <- 2
# })
(y <- expression({a<-1;b<-2}))
# expression({
# a <- 1
# b <- 2
# })
str(x)
# expression({ a <- 1 b <- 2 })
str(y)
# expression({ a <- 1 b <- 2 })
all.equal(x, y)
# TRUE
identical(x, y)
# FALSE
Changing any of the default args to identical
does not affect the result.
These are not identical because the attributes are different.
> str(x[[1]])
length 3 { a <- 1; b <- 2 }
- attr(*, "srcref")=List of 3
..$ :Class 'srcref' atomic [1:8] 1 15 1 15 15 15 1 1
.. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x31b4f90>
..$ :Class 'srcref' atomic [1:8] 1 16 1 19 16 19 1 1
.. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x31b4f90>
..$ :Class 'srcref' atomic [1:8] 1 21 1 24 21 24 1 1
.. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x31b4f90>
- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x31b4f90>
- attr(*, "wholeSrcref")=Class 'srcref' atomic [1:8] 1 0 1 25 0 25 1 1
.. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x31b4f90>
> str(y[[1]])
length 3 { a <- 1; b <- 2 }
- attr(*, "srcref")=List of 3
..$ :Class 'srcref' atomic [1:8] 1 17 1 17 17 17 1 1
.. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x31b6ac0>
..$ :Class 'srcref' atomic [1:8] 1 18 1 21 18 21 1 1
.. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x31b6ac0>
..$ :Class 'srcref' atomic [1:8] 1 23 1 26 23 26 1 1
.. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x31b6ac0>
- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x31b6ac0>
- attr(*, "wholeSrcref")=Class 'srcref' atomic [1:8] 1 0 1 27 0 27 1 1
.. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x31b6ac0>
This is due to the use of {
, as you can see with a simplified example:
> str(quote(a))
symbol a
> str(quote({a}))
length 2 { a }
- attr(*, "srcref")=List of 2
..$ :Class 'srcref' atomic [1:8] 1 11 1 11 11 11 1 1
.. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x1e940c8>
..$ :Class 'srcref' atomic [1:8] 1 12 1 12 12 12 1 1
.. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x1e940c8>
- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x1e940c8>
- attr(*, "wholeSrcref")=Class 'srcref' atomic [1:8] 1 0 1 13 0 13 1 1
.. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x1e940c8>
?Paren
also provides some guidance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With