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

## ----setup--------------------------------------------------------------------
library(rocrateR)

## ----example------------------------------------------------------------------
# library(rocrateR)
my_first_ro_crate <- rocrateR::rocrate()

## -----------------------------------------------------------------------------
my_first_ro_crate

## ----eval = FALSE-------------------------------------------------------------
# my_first_ro_crate |>
#   rocrateR::write_rocrate("/path/to/ro-crate/ro-crate-metadata.json")

## -----------------------------------------------------------------------------
tmp <- file.path(tempdir(), "ro-crate-metadata.json")
my_first_ro_crate |>
  rocrateR::write_rocrate(tmp)

# load lines / flat file
readLines(tmp)

# delete temporary file
unlink(tmp)

## -----------------------------------------------------------------------------
# create entity for an organisation
organisation_uol <- rocrateR::entity(
  id = "https://ror.org/04xs57h96",
  type = "Organization",
  name = "University of Liverpool",
  url = "http://www.liv.ac.uk"
)

# create an entity for a person
person_rvd <- rocrateR::entity(
  id = "https://orcid.org/0000-0001-5036-8661",
  type = "Person",
  name = "Roberto Villegas-Diaz"
)

## -----------------------------------------------------------------------------
my_second_ro_crate <- rocrateR::rocrate() |>
  rocrateR::add_entity(person_rvd) |>
  rocrateR::add_entity_value(
    id = "./", 
    key = "author", 
    value = list(`@id` = person_rvd$`@id`)
  ) |>
  rocrateR::add_entity(organisation_uol) |>
  rocrateR::add_entity_value(
    id = "https://orcid.org/0000-0001-5036-8661",
    key = "affiliation",
    value = list(`@id` = organisation_uol$`@id`)
  )

## ----eval = FALSE-------------------------------------------------------------
# my_second_ro_crate <- rocrateR::rocrate(person_rvd, organisation_uol) |>
#   rocrateR::add_entity_value(id = "./", key = "author", value = list(`@id` = person_rvd$`@id`))

## -----------------------------------------------------------------------------
my_second_ro_crate

## -----------------------------------------------------------------------------
# create basic RO-Crate
basic_ro_crate <- rocrateR::rocrate()

# create some entities for a project and datasets
dataset_entities <- seq_len(2) |>
  lapply(\(x) rocrateR::entity(x, type = "Dataset", name = paste0("Data ", x)))
project_entity <- rocrateR::entity(
  "#proj101", 
  type = "Project", 
  name = "Project 101",
  hasPart = dataset_entities |>
      lapply(\(x) list(`@id` = x[["@id"]]))
  )

# add project and entities to the RO-Crate
basic_ro_crate <- basic_ro_crate |>
  rocrateR::add_entity(project_entity) |>
  # note that here we are using `rocrateR::add_entities` and `rocrateR::add_entity`
  rocrateR::add_entities(dataset_entities)

basic_ro_crate

## -----------------------------------------------------------------------------
basic_ro_crate_project <- basic_ro_crate |>
  rocrateR::get_entity(id = "#proj101")

basic_ro_crate_project

## -----------------------------------------------------------------------------
basic_ro_crate_datasets <- basic_ro_crate |>
  rocrateR::get_entity(type = "Dataset")

basic_ro_crate_datasets

## -----------------------------------------------------------------------------
basic_ro_crate_dataset_root <- basic_ro_crate |>
  rocrateR::get_entity(id = "./", type = "Dataset")

basic_ro_crate_dataset_root

## -----------------------------------------------------------------------------
basic_ro_crate_alt <- basic_ro_crate |>
  rocrateR::remove_entity("#proj101")

## -----------------------------------------------------------------------------
basic_ro_crate_alt <- basic_ro_crate |>
  rocrateR::remove_entity(project_entity)

## -----------------------------------------------------------------------------
basic_ro_crate_alt <- basic_ro_crate |>
  rocrateR::remove_entity(dataset_entities)

## -----------------------------------------------------------------------------
# create basic RO-Crate
basic_ro_crate <- rocrateR::rocrate()

# create temporary directory
tmp_dir <- file.path(tempdir(), paste0("rocrate-", digest::digest(basename(tempfile()))))
dir.create(tmp_dir, showWarnings = FALSE, recursive = TRUE)

# then, we can create the RO-Crate bag
path_to_rocrate_bag <- basic_ro_crate |>
  rocrateR::bag_rocrate(path = tmp_dir)

## -----------------------------------------------------------------------------
path_to_rocrate_bag |>
  rocrateR::is_rocrate_bag()

## -----------------------------------------------------------------------------
path_to_rocrate_bag |>
  rocrateR::load_rocrate()

## ----echo=FALSE, eval=FALSE---------------------------------------------------
# # list files without unzipping
# unzip(path_to_rocrate_bag, list = TRUE)

## -----------------------------------------------------------------------------
# extract files in temporary directory
path_to_rocrate_bag_contents <- path_to_rocrate_bag |>
  rocrateR::unbag_rocrate(output = file.path(tmp_dir, "ROC"))

# create tree with the files
fs::dir_tree(path_to_rocrate_bag_contents)

## -----------------------------------------------------------------------------
# delete temporary directory
unlink(tmp_dir, recursive = TRUE, force = TRUE)

## ----eval = interactive()-----------------------------------------------------
# basic_ro_crate <- rocrateR::rocrate()
# 
# # store crate inside temporary directory
# tmp <- file.path(tempdir(), "ro-crate-metadata.json")
# basic_ro_crate |>
#   rocrateR::write_rocrate(tmp)
# # wrap crate into zip file (expected by validator)
# tmp_zip <- paste(tmp, ".zip")
# zip(tmp_zip, tmp)
# 
# # validate (note the name of the module: rocrate_validator)
# reticulate::use_virtualenv("rocrateR")
# rocrate_validator <- reticulate::import("rocrate_validator")
# status <- rocrate_validator$utils$validate_rocrate_uri(tmp_zip)
# 
# if (status) {
#   message("RO-Crate is valid!")
# } else {
#   message("RO-Crate is invalid!")
# }
# 
# # delete temporary files
# unlink(tmp)
# unlink(tmp_zip)

