job::export() to control what is returned from a job. The documentation for
job::export() includes code examples and may be useful to read first. In this article, we’ll discuss situations where it definitely makes sense to deviate from this default. Check out the related article on controlling imports to a job.
export(c(main, results)) to keep the export small and uncluttered. This is useful if you have many or large intermediate objects. For example, here we do some data preprocessing, but only return the final dataset and the model fit:
Sometimes, a text output is the most interesting, e.g., when your job contains something like
print(summary(fit)). In these cases, just call
job::export("none") on the last line of the code chunk to prevent the job from returning intermediate objects to the main session (or a file).
If you just want to output to a file, I recommend using
saveRDS() for single objects or
export(..., file = "filename.RData") for multiple objects. You can use relative paths for the file name because the job runs in the same working directory as your RStudio session. Example:
When you use the
job::export(..., file) argument, it implicitly calls
job::export("none") after saving, i.e., returns nothing to the main session. To load the objects into your main session (or another job), simply: