Rich Scriven
Rich Scriven

Reputation: 99351

Recalling an expression that was created via a sample

When I want to view structures as they were called, I can usually do it with enquote.

For an arbitrary list d this would be

> d <- list(a = 1, b = 2)
> enquote(d)
# quote(list(a = 1, b = 2))

But for an object created via a sample, it's different. sample does not show up in the quoted call.

> m <- matrix(sample(2))
> enquote(m)
# quote(c(2L, 1L))

Is there a way to show the call/expression that created m, so that sample shows up? So that the result would be something like

quote(matrix(sample(2))

Update: Simon's answer below is great, but I'd really like to see if I can get an answer that doesn't require I use substitute to create the matrix m.

Upvotes: 3

Views: 77

Answers (1)

SimonG
SimonG

Reputation: 4871

I'm not a 100% sure if this serves your purpose, but you could try defining an expression with substitute before evaluating it to create m (no quote though...):

xpr <- substitute(matrix(sample(2)))
m <- eval(xpr)

Result:

> m
     [,1]
[1,]    2
[2,]    1
> xpr
matrix(sample(2))

Cheers!

Upvotes: 2

Related Questions