R Language: dput() and dump()

0

The dput() and dump() functions are used to deparse and parse R objects. This means that they convert R objects into a textual representation that can be saved to a file or passed between R sessions.

The dput() function is used to deparse a single R object. The output of dput() is a string that contains the R code that would recreate the object. For example, the following code creates a data frame and then deparses it using dput():

y <- data.frame(a = 1, b = "a")

dput(y)

The output of dput() is a string that looks like this:

structure(list(a = 1, b = "a"), class = "data.frame", row.names = c(NA, 

-1L))

This string can be saved to a file or passed between R sessions. To read the object back in, you can use the dget() function.

The dump() function is used to deparse multiple R objects at once. The output of dump() is a string that contains the R code that would recreate all of the objects. For example, the following code creates two R objects and then deparses them using dump():

x <- "foo"

y <- data.frame(a = 1L, b = "a")

dump(c("x", "y"), file = "data.R")

The output of dump() is a string that looks like this:

x <- "foo"

y <- structure(list(a = 1L, b = "a"), class = "data.frame", row.names = c(NA, 

-1L))

This string can be saved to a file or passed between R sessions. To read the objects back in, you can use the source() function.

The dput() and dump() functions are useful for sharing R objects or for storing them in a version control system. They can also be used to debug R code by printing out the deparsed representation of an object.

Post a Comment

0Comments
Post a Comment (0)