load_object: load R object by name

load_objectR Documentation

load R object by name

Description

load R object by name

Usage

load_object(
  object,
  save_date = NULL,
  objects_path = jamsession_paths()$objects,
  envir = globalenv(),
  verbose = TRUE,
  ...
)

Arguments

object

character string indicating the name of the R object to load, or data.frame output from grep_objects() which contains colnames ("object", "object_path", "object_file").

save_date

optional character string with a specific date to use when loading an object. This string is matched with the "save_date" returned by list_objects(). When save_date is NULL, the most recent object is loaded, which is the default behavior.

objects_path

character vector of one or more file paths to search for saved R objects. When objects_path=NULL, it uses the output from jamsession_paths()$objects.

envir

optional R environment inside which to load the R object. Note that if an R object already exists in this environment, it will overwritten without warning. This behavior is intended by load_object().

verbose

logical whether to print verbose output

...

additional arguments are ignored.

Details

This function loads the most recently saved R object by name, from a given file path. This mechanism is intended to help share useful R objects across R sessions, especially where it may be helpful to discover R objects by name, and where R objects may continue to be useful over time.

Typically this function is called with the name of one or more saved objects, and loads the most recent version of each. However, the argument save_date can be supplied as a vector, which is recycled for each saved object, as a method to load a specific version of an object. This behavior is not intended for routine use, but is available in rare cases that you may want to inspect previous versions of an object.

Note that envir can be used to load the object into a separate environment, if needed.

Value

invisible name of the R object loaded. It also loads the R object into the environment specified, by default the active global environment .GlobalEnv.

See Also

Other jamsession objects: grep_objects(), list_objects(), save_object()

Examples

my_df <- data.frame(A=LETTERS[1:5], a=letters[1:5]);
print(my_df);

save_object("my_df");

grep_objects("my_df");

rm(my_df);

load_object("my_df");
print(my_df);

load_object(grep_objects("my_df"))
print(my_df);

######################################
# Load into a separate environment
rm(my_df);
sep_env <- new.env();
load_object("my_df", envir=sep_env);

# this object is not in the current workspace
find("my_df");

# you can use get() to see this object
print(get("my_df", envir=sep_env));

# or you can attach the environment to use objects inside
find("my_df");
attach(sep_env);
find("my_df");

# detach when no longer using this environment
detach("sep_env");
find("my_df");

######################################
# example showing two objects stored together
my_df2 <- data.frame(B=LETTERS[11:15], b=letters[11:15]);
load_object("my_df");
save_object(c("my_df", "my_df2"));

grep_objects("my_df2");
loaded <- load_object(grep_objects("my_df2"));

# note that it loads two objects
print(loaded);



jmw86069/jamsession documentation built on June 8, 2022, 8:47 p.m.