Skip to contents

This function is a wrapper around dput that allows you to write an R object as part of a knitr document as an output for later use. It is designed to be used in a code block. The file name, if not specified, will be the label of the code block. Use the standard dget function to read the file back into an R session.

Usage

write_obj(obj, file = NULL, target_dir = NULL, ...)

Arguments

obj

The R object to be written.

file

The name of the file to be written. If not specified, the label of the code block will be used.

target_dir

The directory where the file will be written. If not specified, the current working directory will be used.

...

Additional arguments to be passed to dput.

Value

The path of the written file.

Examples

if (FALSE) { # \dontrun{
obj_dir <- file.path(tempdir(), "obj")

# Write a data frame as a file
write_obj(
 obj = mtcars,
 file = "mtcars_data",
 target_dir = obj_dir)

# Read the file back into an R session
my_mtcars <- dget(file.path(obj_dir, "mtcars_data"))

unlink(obj_dir)
} # }