forked from rstudio/reticulate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpickle.R
27 lines (24 loc) · 772 Bytes
/
pickle.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#' Save and load Python objects with pickle
#'
#' @param object Object to save
#' @param filename File name
#' @param pickle The implementation of pickle to use (defaults to "pickle" but
#' could e.g. also be "cPickle")
#'
#' @export
py_save_object <- function(object, filename, pickle = "pickle") {
builtins <- import_builtins()
pickle <- import(pickle)
handle <- builtins$open(filename, "wb")
on.exit(handle$close(), add = TRUE)
pickle$dump(object, handle, protocol = pickle$HIGHEST_PROTOCOL)
}
#' @rdname py_save_object
#' @export
py_load_object <- function(filename, pickle = "pickle") {
builtins <- import_builtins()
pickle <- import(pickle)
handle <- builtins$open(filename, "rb")
on.exit(handle$close(), add = TRUE)
pickle$load(handle)
}