I am looking for a function in R that converts an object into code that can be used to create a replica of that object. Something like this:
> myObject=c(1, 2, 3)
> magicFunction(myObject)
[1] "c(1,2,3)"
I think this function exists, but I cannot find it. Would greatly appreciate your help.
You can also use dput
, as this retains the structure of the object as opposed to a character string representation of it.
From ?dput
:
Writes an ASCII text representation of an R object to a file or connection, or uses one to recreate the object.
For example
myObject=c(1, 2, 3)
dput(myObject)
# c(1, 2, 3)
identical(myObject, dput(myObject))
# c(1, 2, 3)
# [1] TRUE
## whereas
identical(myObject, deparse(myObject))
# [1] FALSE
We can use deparse
deparse(myObject)
#[1] "c(1, 2, 3)"
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