## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

## ----prepare------------------------------------------------------------------
library(projectData)
source(system.file("examples", "tinydisk.R", package = "projectData"))
source(system.file("examples", "fixtures.R", package = "projectData"))

lib <- fixture_library()
fixture_package(lib, "diskData", "1.0.0", lazy = FALSE, datasets = list(),
  files = list("extdata/counts.tinymat" = function(path) {
    tinymat_write(matrix(as.double(1:12), 3, 4), path)
  }))

## ----register-loader----------------------------------------------------------
data_loader_add(tinydisk_loader())
data_loaders()

## ----query--------------------------------------------------------------------
data_query("diskData", lib.loc = lib)[, c("object", "loader", "storage", "state")]

## ----attach-------------------------------------------------------------------
data_attach("diskData", lib.loc = lib, suffix = ".disk")
data_catalog()[, c("exposed_name", "object", "resource", "state")]

## ----access-------------------------------------------------------------------
counts <- data_get("counts")
dim(counts)
counts[2, 3]

## ----cache--------------------------------------------------------------------
identical(counts, data_get("counts.disk"))
identical(counts, data_get("counts", collection = "diskData"))
data_catalog()[, c("exposed_name", "state")]

## ----detach-------------------------------------------------------------------
data_detach()
counts[1, 1]

## ----authoring-source---------------------------------------------------------
pkg <- tempfile("pkg-")
dir.create(file.path(pkg, "data"), recursive = TRUE)
dir.create(file.path(pkg, "extdata"), recursive = TRUE)
example_obj <- 1:3
save(example_obj, file = file.path(pkg, "data", "example.rda"))
tinymat_write(matrix(as.double(1:6), 2, 3),
              file.path(pkg, "extdata", "counts.tinymat"))

drafts <- data_manifest_draft(pkg, layout = "standalone")
vapply(drafts, function(d) d$source, "")

## ----authoring-write----------------------------------------------------------
drafts <- lapply(drafts, function(d) {
  d$title <- "Counts"
  d$options <- list(work_copy = "true")
  d$adapter <- list(package = "tinydiskAdapter")
  d
})
manifest <- file.path(pkg, "projectData", "manifest.dcf")
data_manifest_write(drafts, manifest, root = pkg, layout = "standalone")
writeLines(readLines(manifest))

## ----authoring-errors, error = TRUE-------------------------------------------
try({
data_manifest_write(list(resource = "counts", loader = "tinydisk::tinymat",
                         source = "../elsewhere", objects = "counts"),
                    file.path(pkg, "other.dcf"), root = pkg, layout = "standalone")
})

## ----missing-adapter----------------------------------------------------------
data_loader_remove("tinydisk::tinymat")
fixture_package(lib, "mixedData", "1.0.0", lazy = FALSE,
  datasets = list(plain = list(x = 1:3)),
  files = list("extdata/m.tinymat" = function(path) {
    tinymat_write(matrix(1, 2, 2), path)
  }),
  manifest = c("ManifestVersion: 1", "",
               "Resource: m", "Loader: tinydisk::tinymat",
               "Source: extdata/m.tinymat", "Objects: m",
               "AdapterPackage: noSuchAdapter"))
data_attach("mixedData", lib.loc = lib)
data_get("x")

## ----missing-adapter-error, error = TRUE--------------------------------------
try({
data_get("m")
})

## ----missing-adapter-catalog--------------------------------------------------
data_catalog()[, c("exposed_name", "origin", "state")]
data_detach()

