Initializes object storage backed by a provider.
The provider must supply methods for putting, getting, checking, deleting, and clearing stored values.
# S3 method for class 'object'
storage.init(storage.type = storage.object.class, provider, ...)List containing characteristics perticular to the storage implementation, including:
$type - the storage type
library(magrittr)
# initialize default memory storage
my.storage <- storage.init()
# set a value into storage
storage.set(my.storage, "name", "Roy Wetherall")
# .. and some more
my.storage %>%
storage.set("age", 45) %>%
storage.set("alive", TRUE) %>%
storage.set("children", c("Peter", "Grace", "Lucy"))
# check a key has been set
if (storage.has(my.storage, "name")) print("I know your name!")
#> [1] "I know your name!"
# .. and that a key hasn't been set
if (!storage.has(my.storage, "address")) print("I don't know where you live!")
#> [1] "I don't know where you live!"
# get some values from storage
sprintf(
"%s is %i years old.",
storage.get(my.storage, "name"),
storage.get(my.storage, "age"))
#> [1] "Roy Wetherall is 45 years old."
# remove a value from storage
storage.unset(my.storage, "children")
# .. and show it's not there anymore
if (!storage.has(my.storage, "address")) print("I don't know who your children are!")
#> [1] "I don't know who your children are!"
# clear all values from storage
storage.clear(my.storage)
# .. and everything is gone
if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
#> [1] "I know nothing!"
# initialize file storage (direct backend; simplest local-only path)
base.dir <- file.path(tempdir(), "memofunc-storage")
file.storage <- storage.init("file", base.dir = base.dir)
# set and retrieve a value from file storage (direct backend)
storage.set(file.storage, "name", "Roy Wetherall")
storage.get(file.storage, "name")
#> [1] "Roy Wetherall"
# initialize object storage using a provider name (provider-backed interface)
# this uses the same local file backend now, but lets you swap to azure.blob/s3 later
object.storage <- storage.init("object", provider = "file", base.dir = base.dir)
storage.set(object.storage, "name", "Roy Wetherall")
storage.get(object.storage, "name")
#> [1] "Roy Wetherall"
# set a default provider via options
old.options <- options(memofunc.storage.provider = list(name = "file", base.dir = base.dir))
on.exit(options(old.options), add = TRUE)
default.storage <- storage.init()
storage.get(default.storage, "name")
#> NULL
# Azure Blob provider example (requires AzureStor + credentials)
if (requireNamespace("AzureStor", quietly = TRUE)) {
account <- Sys.getenv("AZURE_STORAGE_ACCOUNT")
container <- Sys.getenv("AZURE_STORAGE_CONTAINER")
key <- Sys.getenv("AZURE_STORAGE_KEY")
token <- Sys.getenv("AZURE_STORAGE_TOKEN")
if (account != "" && container != "" && (key != "" || token != "")) {
azure.storage <- storage.init(
"object",
provider = "azure.blob",
account = account,
container = container,
key = if (key == "") NULL else key,
token = if (token == "") NULL else token,
prefix = "memofunc-example"
)
storage.set(azure.storage, "name", "Roy Wetherall")
storage.get(azure.storage, "name")
}
}