开发者

"verbatim" function

开发者 https://www.devze.com 2023-03-10 20:24 出处:网络
this might look like a simple and basic question. I\'ve been studying r for a couple of months now and it seems I cannot find a function I\'m looking for. I don\'t even know how to search it up.... ou

this might look like a simple and basic question. I've been studying r for a couple of months now and it seems I cannot find a function I'm looking for. I don't even know how to search it up.... out of search strings ideas.

I know there is a function to ge开发者_C百科t the definition of a variable more than its contents. I explain myself...

> x <- c(4:6,5:9)
> x  # This will return the contents of x...  4,5,6,5,6,7,8,9.

> the.function.i.m.looking.for(x)  # would return:
> c(4:6,5:9)

Anyone remember that function? Thanks.


dput is what you are afer. From the help page: "Writes an ASCII text representation of an R object to a file or connection, or uses one to recreate the object."

> x <- c(4:6,5:9)
dput(x)
c(4L, 5L, 6L, 5L, 6L, 7L, 8L, 9L)

> x2 <-  c(4L, 5L, 6L, 5L, 6L, 7L, 8L, 9L)
> all.equal(x, x2)
[1] TRUE


dput(x) gets you close:

R> dput(x)
c(4L, 5L, 6L, 5L, 6L, 7L, 8L, 9L)

Or, if a character representation will suffice, then the deparse(substitute()) idiom might be sufficient:

foo <- function(x) {
    deparse(substitute(x))
}

but you need to call it like this:

R> foo(c(4:6,5:9))
[1] "c(4:6, 5:9)"

not this

R> foo(x)
[1] "x"


If you want to return the syntax that you need to recreate the object then dput() is your friend:

x <- c(4:6,5:9)
dput(x)
c(4L, 5L, 6L, 5L, 6L, 7L, 8L, 9L)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号