Skip to content

Commit

Permalink
Merge pull request #47 from atorus-research/gh_issue_43
Browse files Browse the repository at this point in the history
Updates from discussion
  • Loading branch information
nicholas-masel authored Sep 5, 2024
2 parents 2dc4c43 + 7d4f4ba commit 2cca514
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 52 deletions.
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

export(dataset_json)
export(read_dataset_json)
export(set_as_reference_data)
export(set_dataset_label)
export(set_dataset_name)
export(set_file_oid)
export(set_item_oid)
export(set_last_modified)
export(set_metadata_ref)
export(set_metadata_version)
export(set_originator)
export(set_reference_data)
export(set_source_system)
export(set_study_oid)
export(validate_dataset_json)
Expand Down
39 changes: 24 additions & 15 deletions R/datasetjson.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#' @param item_oid ID used to label dataset with the itemGroupData parameter.
#' Defined as "Object of Datasets. Key value is a unique identifier for
#' Dataset, corresponding to ItemGroupDef/@OID in Define-XML."
#' @param ref_data Boolean value that is set to "true" when the dataset contains
#' reference data (not subject data). The default value is "false".
#' @param version The DatasetJSON version to use. Currently only 1.1.0 is supported.
#'
#' @return dataset_json object pertaining to the specific Dataset JSON version
Expand All @@ -35,7 +37,8 @@
#' # Create a basic object
#' ds_json <- dataset_json(
#' iris,
#' file_oid = "/some/path"
#' file_oid = "/some/path",
#' last_modified = "2023-02-15T10:23:15",
#' originator = "Some Org",
#' sys = "source system",
#' sys_version = "1.0",
Expand All @@ -50,6 +53,7 @@
#' # Attach attributes directly
#' ds_json_updated <- set_data_type(ds_json, "referenceData")
#' ds_json_updated <- set_file_oid(ds_json_updated, "/some/path")
#' ds_json_updates <- set_last_modified(ds_json_updates, "2023-02-15T10:23:15")
#' ds_json_updated <- set_metadata_ref(ds_json_updated, "some/define.xml")
#' ds_json_updated <- set_metadata_version(ds_json_updated, "MDV.MSGv2.0.SDTMIG.3.3.SDTM.1.7")
#' ds_json_updated <- set_originator(ds_json_updated, "Some Org")
Expand All @@ -58,12 +62,14 @@
#' ds_json_updated <- set_item_oid(ds_json_updated, "IG.IRIS")
#' ds_json_updated <- set_dataset_name(ds_json_updated, "IRIS")
#' ds_json_updated <- set_dataset_label(ds_json_updated, "Iris")
dataset_json <- function(.data, file_oid = NULL, originator=NULL, sys=NULL,
sys_version = NULL, study=NULL, metadata_version=NULL,
metadata_ref=NULL, item_oid=NULL, name=NULL,
dataset_label=NULL, version="1.1.0") {
new_dataset_json(.data, file_oid, originator, sys, sys_version, study, metadata_version,
metadata_ref, item_oid, name, dataset_label, version)
dataset_json <- function(.data, file_oid = NULL, last_modified=NULL,
originator=NULL, sys=NULL, sys_version = NULL,
study=NULL, metadata_version=NULL,metadata_ref=NULL,
item_oid=NULL, name=NULL, dataset_label=NULL, ref_data=FALSE,
version="1.1.0") {
new_dataset_json(.data, file_oid, last_modified, originator, sys, sys_version, study,
metadata_version, metadata_ref, item_oid, name, dataset_label, ref_data,
version)
}

#' Create a base Dataset JSON Container
Expand All @@ -75,9 +81,9 @@ dataset_json <- function(.data, file_oid = NULL, originator=NULL, sys=NULL,
#' @return datasetjson object
#'
#' @noRd
new_dataset_json <- function(.data, file_oid, originator, sys, sys_version, study,
new_dataset_json <- function(.data, file_oid, last_modified, originator, sys, sys_version, study,
metadata_version, metadata_ref, item_oid, name, dataset_label,
version) {
ref_data, version) {

if (!(version %in% c("1.1.0"))) {
stop("Unsupported version specified - currently only version 1.1.0 is supported", call.=FALSE)
Expand All @@ -89,16 +95,18 @@ new_dataset_json <- function(.data, file_oid, originator, sys, sys_version, stud
)

# Extract the function and call it to return the base structure
funcs[[version]](.data, file_oid, originator, sys, sys_version, study,
metadata_version, metadata_ref, item_oid, name, dataset_label)
funcs[[version]](.data, file_oid, last_modified, originator, sys, sys_version, study,
metadata_version, metadata_ref, item_oid, name, dataset_label,
ref_data)
}

#' Dataset JSON v1.1.0 Generator
#'
#' @return datasetjson_v1_1_0 object
#' @noRd
new_dataset_json_v1_1_0 <- function(.data, file_oid, originator, sys, sys_version, study,
metadata_version, metadata_ref, item_oid, name, dataset_label) {
new_dataset_json_v1_1_0 <- function(.data, file_oid, last_modified, originator, sys, sys_version,
study, metadata_version, metadata_ref, item_oid, name,
dataset_label, ref_data) {

if (!inherits(.data, 'data.frame')) {
stop("datasetjson objects must inherit from a data.frame", call.=FALSE)
Expand All @@ -111,16 +119,17 @@ new_dataset_json_v1_1_0 <- function(.data, file_oid, originator, sys, sys_versio
)
}

attr(.data, 'datasetJSONVersion') <- "1.1.0"
attr(.data, 'fileOID') <- file_oid
attr(.data, 'dbLastModifiedDateTime') <- last_modified
attr(.data, 'originator') <- originator
attr(.data, 'studyOID') <- study
attr(.data, 'metaDataVersionOID') <- metadata_version
attr(.data, 'metaDataRef') <- metadata_ref
attr(.data, "itemGroupOID") <- item_oid
attr(.data, 'records') <- nrow(x)
attr(.data, 'name') <- name
attr(.data, 'label') <- dataset_label
attr(.data, 'isReferenceData') <- FALSE
attr(.data, 'isReferenceData') <- ref_data

structure(
.data,
Expand Down
62 changes: 49 additions & 13 deletions R/file_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#' @param item_oid ID used to label dataset with the itemGroupData parameter.
#' Defined as "Object of Datasets. Key value is a unique identifier for
#' Dataset, corresponding to ItemGroupDef/@OID in Define-XML."
#' @param ref_data Boolean value that is set to "true" when the dataset contains
#' reference data (not subject data). The default value is "false".
#' @param name Dataset name
#' @param dataset_label Dataset Label
#'
Expand All @@ -50,6 +52,12 @@
#' ds_json <- set_dataset_label(ds_json, "Iris")
set_source_system <- function(x, sys, sys_version) {
stopifnot_datasetjson(x)
if (!is.character(sys)) {
stop("`sys` must be a character")
}
if (!is.character(sys_version)) {
stop("`sys_version` must be a character")
}
attr(x, 'sourceSystem') <- list(
"name" = sys,
"version" = sys_version
Expand All @@ -62,6 +70,9 @@ set_source_system <- function(x, sys, sys_version) {
#' @rdname dataset_metadata_setters
set_originator <- function(x, originator) {
stopifnot_datasetjson(x)
if (!is.character(originator)) {
stop("`originator` must be a character")
}
attr(x, 'originator') <- originator
x
}
Expand All @@ -71,6 +82,9 @@ set_originator <- function(x, originator) {
#' @rdname dataset_metadata_setters
set_file_oid <- function(x, file_oid) {
stopifnot_datasetjson(x)
if (!is.character(file_oid)) {
stop("`file_oid` must be a character")
}
attr(x, 'fileOID') <- file_oid
x
}
Expand All @@ -80,6 +94,9 @@ set_file_oid <- function(x, file_oid) {
#' @rdname dataset_metadata_setters
set_study_oid <- function(x, study) {
stopifnot_datasetjson(x)
if (!is.character(study)) {
stop("`study` must be a character")
}
attr(x, 'studyOID') <- study
x
}
Expand All @@ -89,6 +106,9 @@ set_study_oid <- function(x, study) {
#' @rdname dataset_metadata_setters
set_metadata_version <- function(x, metadata_version) {
stopifnot_datasetjson(x)
if (!is.character(metadata_version)) {
stop("`metadata_version` must be a character")
}
attr(x, 'metaDataVersionOID') <- metadata_version
x
}
Expand All @@ -98,6 +118,9 @@ set_metadata_version <- function(x, metadata_version) {
#' @rdname dataset_metadata_setters
set_metadata_ref <- function(x, metadata_ref) {
stopifnot_datasetjson(x)
if (!is.character(metadata_ref)) {
stop("`metadata_ref` must be a character")
}
attr(x, 'metaDataRef') <- metadata_ref
x
}
Expand All @@ -107,6 +130,9 @@ set_metadata_ref <- function(x, metadata_ref) {
#' @rdname dataset_metadata_setters
set_item_oid <- function(x, item_oid) {
stopifnot_datasetjson(x)
if (!is.character(item_oid)) {
stop("`item_oid` must be a character")
}
attr(x, "itemGroupOID") <- item_oid
}

Expand All @@ -115,6 +141,9 @@ set_item_oid <- function(x, item_oid) {
#' @rdname dataset_metadata_setters
set_dataset_name <- function(x, name) {
stopifnot_datasetjson(x)
if (!is.character(name)) {
stop("`name` must be a character")
}
attr(x, 'name') <- name
}

Expand All @@ -123,26 +152,33 @@ set_dataset_name <- function(x, name) {
#' @rdname dataset_metadata_setters
set_dataset_label <- function(x, dataset_label) {
stopifnot_datasetjson(x)
if (!is.character(dataset_labelx)) {
stop("`dataset_label` must be a character")
}
attr(x, 'label') <- dataset_label
}

#' Declare as reference data
#'
#' Sets DatasetJSON file to have the isReferenceData attribute set to TRUE
#'
#' @param x datasetjson object
#'
#' @return datasetjson object
#' @export
#'
#' @examples
#' ds_json <- dataset_json(iris)
#' ds_json <- set_as_reference_data(ds_json)
set_as_reference_data <- function(x) {
#' @family Dataset Metadata Setters
#' @rdname dataset_metadata_setters
set_reference_data <- function(x, is_refdata) {
stopifnot_datasetjson(x)
attr(x, 'isReferenceData') <- TRUE
if (!is.logical(is_refdata)) {
stop("`is_refdata` must be a boolean")
}
attr(x, 'isReferenceData') <- is_refdata
}

#' @export
#' @family Dataset Metadata Setters
#' @rdname dataset_metadata_setters
set_last_modified <- function(x, last_modified) {
stopifnot_datasetjson(x)
if (!is.character(last_modified)) {
stop("`last_modified` must be a character")
}
attr(x, 'dbLastModifiedDateTime') <- last_modified
}

#' Create an ISO8601 formatted datetime of the current time
#'
Expand Down
11 changes: 9 additions & 2 deletions man/dataset_json.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions man/dataset_metadata_setters.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 0 additions & 21 deletions man/set_as_reference_data.Rd

This file was deleted.

0 comments on commit 2cca514

Please sign in to comment.