Skip to contents

Objectives

  1. Read in a dataset-JSON DM file
  2. Add a new variable to the dataset
  3. Update metadata within JSON file
  4. Write out the dataset-JSON file

Read in a dataset-JSON DM file

Below we use the read_dataset_json to read into our R session a Demographics dataset-JSON file via a url.

library(dplyr)
library(jsonlite)
library(xportr)
library(xportrjson)

dm <- read_dataset_json(url("https://raw.githubusercontent.com/lexjansen/sas-papers/master/pharmasug-2022/json/sdtm/dm.json"))
#> Rows: 18
#> Columns: 27
#> $ ITEMGROUPDATASEQ <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16…
#> $ STUDYID          <chr> "CDISCPILOT01", "CDISCPILOT01", "CDISCPILOT01", "CDIS…
#> $ DOMAIN           <chr> "DM", "DM", "DM", "DM", "DM", "DM", "DM", "DM", "DM",…
#> $ USUBJID          <chr> "CDISC001", "CDISC002", "CDISC003", "CDISC004", "CDIS…
#> $ SUBJID           <chr> "1115", "1211", "1302", "1345", "1383", "1429", "1444…
#> $ RFSTDTC          <chr> "2012-11-30", "2012-11-15", "2013-08-29", "2013-10-08…
#> $ RFENDTC          <chr> "2013-01-23", "2013-01-14", "2013-11-05", "2014-03-18…
#> $ RFXSTDTC         <chr> "2012-11-30", "2012-11-15", "2013-08-29", "2013-10-08…
#> $ RFXENDTC         <chr> "2013-01-23", "2013-01-12", "2013-11-05", "2014-03-18…
#> $ RFICDTC          <chr> "2012-11-23", "2012-10-30", "2013-08-20", "2013-10-01…
#> $ RFPENDTC         <chr> "2013-05-20", "2013-01-14", "2014-02-13", "2014-03-18…
#> $ DTHDTC           <chr> "", "2013-01-14", "", "", "", "", "", "2014-11-01", "…
#> $ DTHFL            <chr> "", "Y", "", "", "", "", "", "Y", "", "", "", "", "Y"…
#> $ SITEID           <chr> "701", "701", "701", "701", "701", "701", "701", "704…
#> $ BRTHDTC          <chr> "1928", "1936", "1951", "1950", "1941", "1929", "1949…
#> $ AGE              <int> 84, 76, 61, 63, 72, 84, 63, 75, 74, 86, 73, 67, 89, 6…
#> $ AGEU             <chr> "YEARS", "YEARS", "YEARS", "YEARS", "YEARS", "YEARS",…
#> $ SEX              <chr> "M", "F", "M", "F", "F", "F", "M", "M", "F", "F", "M"…
#> $ RACE             <chr> "WHITE", "WHITE", "WHITE", "WHITE", "WHITE", "WHITE",…
#> $ ETHNIC           <chr> "NOT HISPANIC OR LATINO", "NOT HISPANIC OR LATINO", "…
#> $ ARMCD            <chr> "ZAN_LOW", "ZAN_LOW", "ZAN_HIGH", "PLACEBO", "ZAN_HIG…
#> $ ARM              <chr> "Zanomaline Low Dose (54 mg)", "Zanomaline Low Dose (…
#> $ ACTARMCD         <chr> "ZAN_LOW", "ZAN_LOW", "ZAN_HIGH", "PLACEBO", "ZAN_HIG…
#> $ ACTARM           <chr> "Zanomaline Low Dose (54 mg)", "Zanomaline Low Dose (…
#> $ ARMNRS           <chr> "", "", "", "", "", "", "", "", "", "", "", "", "", "…
#> $ ACTARMUD         <chr> "", "", "", "", "", "", "", "", "", "", "", "", "", "…
#> $ COUNTRY          <chr> "USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA…

The read_dataset_json also reads in the metadata from the JSON file and applies it to the dataframe. For example, the variable RFXSTDTC has the label Date/Time of First Study Treatment applied to it.

attributes(dm$RFXSTDTC)
#> $label
#> [1] "Date/Time of First Study Treatment"

To get a better idea of what is in the JSON metadata file for the DM data, lets take a quick look.

raw_dm <- jsonlite::fromJSON(url("https://raw.githubusercontent.com/lexjansen/sas-papers/master/pharmasug-2022/json/sdtm/dm.json"), simplifyVector = TRUE)

head(raw_dm$clinicalData$itemGroupData[[1]]$items, 5)
#>                OID             name                            label    type
#> 1 ITEMGROUPDATASEQ ITEMGROUPDATASEQ                Record Identifier integer
#> 2    IT.DM.STUDYID          STUDYID                 Study Identifier  string
#> 3     IT.DM.DOMAIN           DOMAIN              Domain Abbreviation  string
#> 4    IT.DM.USUBJID          USUBJID        Unique Subject Identifier  string
#> 5     IT.DM.SUBJID           SUBJID Subject Identifier for the Study  string
#>   length
#> 1     NA
#> 2     12
#> 3      2
#> 4      8
#> 5      4

Update metadata within JSON file

The JSON file has the metadata associated with the JSON dataset. If we would like to add variables to the JSON file while using R we will need to do three tasks.

  1. Pull out current metadata for DM dataset
  2. Add new variable metadata

Pull out current metadata for DM dataset

metadata_current <- raw_dm$clinicalData$itemGroupData[[1]]$items

head(metadata_current, 5)
#>                OID             name                            label    type
#> 1 ITEMGROUPDATASEQ ITEMGROUPDATASEQ                Record Identifier integer
#> 2    IT.DM.STUDYID          STUDYID                 Study Identifier  string
#> 3     IT.DM.DOMAIN           DOMAIN              Domain Abbreviation  string
#> 4    IT.DM.USUBJID          USUBJID        Unique Subject Identifier  string
#> 5     IT.DM.SUBJID           SUBJID Subject Identifier for the Study  string
#>   length
#> 1     NA
#> 2     12
#> 3      2
#> 4      8
#> 5      4

Add new variable metadata

metadata_updated <- data.frame(
  name = c("TRT01P", "TRT01A"),
  OID = c("IT.ADSL.TRT01P", "IT.ADSL.TRT01A"),
  length = c(28, 28),
  type = c("string", "string"),
  label = c("Planned Treatment for Period 01", "Actual Treatment for Period 01")
) %>% 
  bind_rows(metadata_current) %>% 
  rename(variable=name)

Add a new variable to the dataset

Now we would like to add a new variable and update the metadata associated with this variable on the R dataframe. We will use the xportr functions to apply the length and label to the R dataframe.

adsl <- dm %>%
  mutate(TRT01P = ARM, TRT01A = ACTARM) %>% 
  xportr_length(metadata_updated) %>% 
  xportr_label(metadata_updated)

attributes(adsl$TRT01P)
#> $label
#> [1] "Planned Treatment for Period 01"
#> 
#> $width
#> [1] 28

Write out an updated JSON (To finish)

TODO: Figure out how to put together updated metadata and data into one JSON file TODO: Write out and archive somewhere accessible.

write_item_data(adsl)
#> "itemData":[[1,"CDISCPILOT01","DM","CDISC001","1115","2012-11-30","2013-01-23","2012-11-30","2013-01-23","2012-11-23","2013-05-20","","","701","1928",84,"YEARS","M","WHITE","NOT HISPANIC OR LATINO","ZAN_LOW","Zanomaline Low Dose (54 mg)","ZAN_LOW","Zanomaline Low Dose (54 mg)","","","USA","Zanomaline Low Dose (54 mg)","Zanomaline Low Dose (54 mg)"][2,"CDISCPILOT01","DM","CDISC002","1211","2012-11-15","2013-01-14","2012-11-15","2013-01-12","2012-10-30","2013-01-14","2013-01-14","Y","701","1936",76,"YEARS","F","WHITE","NOT HISPANIC OR LATINO","ZAN_LOW","Zanomaline Low Dose (54 mg)","ZAN_LOW","Zanomaline Low Dose (54 mg)","","","USA","Zanomaline Low Dose (54 mg)","Zanomaline Low Dose (54 mg)"][3,"CDISCPILOT01","DM","CDISC003","1302","2013-08-29","2013-11-05","2013-08-29","2013-11-05","2013-08-20","2014-02-13","","","701","1951",61,"YEARS","M","WHITE","NOT HISPANIC OR LATINO","ZAN_HIGH","Zanomaline High Dose (81 mg)","ZAN_HIGH","Zanomaline High Dose (81 mg)","","","USA","Zanomaline High Dose (81 mg)","Zanomaline High Dose (81 mg)"][4,"CDISCPILOT01","DM","CDISC004","1345","2013-10-08","2014-03-18","2013-10-08","2014-03-18","2013-10-01","2014-03-18","","","701","1950",63,"YEARS","F","WHITE","NOT HISPANIC OR LATINO","PLACEBO","Placebo","PLACEBO","Placebo","","","USA","Placebo","Placebo"][5,"CDISCPILOT01","DM","CDISC005","1383","2013-02-04","2013-08-06","2013-02-04","2013-08-06","2013-01-22","2013-08-06","","","701","1941",72,"YEARS","F","WHITE","NOT HISPANIC OR LATINO","ZAN_HIGH","Zanomaline High Dose (81 mg)","ZAN_HIGH","Zanomaline High Dose (81 mg)","","","USA","Zanomaline High Dose (81 mg)","Zanomaline High Dose (81 mg)"][6,"CDISCPILOT01","DM","CDISC006","1429","2013-03-19","2013-04-30","2013-03-19","2013-04-30","2013-02-25","2013-04-30","","","701","1929",84,"YEARS","F","WHITE","NOT HISPANIC OR LATINO","ZAN_LOW","Zanomaline Low Dose (54 mg)","ZAN_LOW","Zanomaline Low Dose (54 mg)","","","USA","Zanomaline Low Dose (54 mg)","Zanomaline Low Dose (54 mg)"][7,"CDISCPILOT01","DM","CDISC007","1444","2013-01-05","2013-02-13","2013-01-05","2013-02-12","2012-12-31","2013-06-20","","","701","1949",63,"YEARS","M","WHITE","HISPANIC OR LATINO","ZAN_HIGH","Zanomaline High Dose (81 mg)","ZAN_HIGH","Zanomaline High Dose (81 mg)","","","USA","Zanomaline High Dose (81 mg)","Zanomaline High Dose (81 mg)"][8,"CDISCPILOT01","DM","CDISC008","1445","2014-05-11","2014-11-01","2014-05-11","2014-11-01","2014-05-01","2014-11-01","2014-11-01","Y","704","1939",75,"YEARS","M","MULTIPLE","NOT HISPANIC OR LATINO","PLACEBO","Placebo","PLACEBO","Placebo","","","USA","Placebo","Placebo"][9,"CDISCPILOT01","DM","CDISC009","1087","2012-10-22","2013-04-28","2012-10-22","2013-04-28","2012-10-06","2013-04-28","","","708","1938",74,"YEARS","F","WHITE","NOT HISPANIC OR LATINO","PLACEBO","Placebo","PLACEBO","Placebo","","","USA","Placebo","Placebo"][10,"CDISCPILOT01","DM","CDISC010","1236","2013-09-21","2013-09-26","2013-09-21","2013-09-21","2013-09-08","2013-09-26","","","708","1927",86,"YEARS","F","WHITE","NOT HISPANIC OR LATINO","ZAN_HIGH","Zanomaline High Dose (81 mg)","ZAN_HIGH","Zanomaline High Dose (81 mg)","","","USA","Zanomaline High Dose (81 mg)","Zanomaline High Dose (81 mg)"][11,"CDISCPILOT01","DM","CDISC011","1336","2012-12-07","2013-06-05","2012-12-07","2013-06-05","2012-11-21","2013-07-05","","","708","1939",73,"YEARS","M","WHITE","NOT HISPANIC OR LATINO","ZAN_HIGH","Zanomaline High Dose (81 mg)","ZAN_HIGH","Zanomaline High Dose (81 mg)","","","USA","Zanomaline High Dose (81 mg)","Zanomaline High Dose (81 mg)"][12,"CDISCPILOT01","DM","CDISC012","1378","2013-09-03","2014-01-28","2013-09-03","2014-01-28","2013-08-24","2014-01-28","","","708","1946",67,"YEARS","M","BLACK OR AFRICAN AMERICAN","NOT HISPANIC OR LATINO","PLACEBO","Placebo","PLACEBO","Placebo","","","USA","Placebo","Placebo"][13,"CDISCPILOT01","DM","CDISC013","1083","2013-07-22","2013-08-03","2013-07-22","2013-08-01","2013-07-09","2013-08-03","2013-08-03","Y","710","1924",89,"YEARS","F","WHITE","NOT HISPANIC OR LATINO","PLACEBO","Placebo","PLACEBO","Placebo","","","USA","Placebo","Placebo"][14,"CDISCPILOT01","DM","CDISC014","1012","2013-04-03","2013-05-02","2013-04-03","2013-04-29","2013-03-20","2013-09-18","","","711","1945",67,"YEARS","F","WHITE","NOT HISPANIC OR LATINO","ZAN_HIGH","Zanomaline High Dose (81 mg)","ZAN_HIGH","Zanomaline High Dose (81 mg)","","","USA","Zanomaline High Dose (81 mg)","Zanomaline High Dose (81 mg)"][15,"CDISCPILOT01","DM","CDISC015","1022","","","","","2014-03-17","2014-03-17","","","711","1928",86,"YEARS","F","WHITE","NOT HISPANIC OR LATINO","","","","","SCREEN FAILURE","","USA","",""][16,"CDISCPILOT01","DM","CDISC016","1143","2013-04-03","2013-06-01","2013-04-03","2013-05-30","2013-03-30","2013-09-22","","","711","1936",76,"YEARS","F","WHITE","NOT HISPANIC OR LATINO","ZAN_LOW","Zanomaline Low Dose (54 mg)","ZAN_LOW","Zanomaline Low Dose (54 mg)","","","USA","Zanomaline Low Dose (54 mg)","Zanomaline Low Dose (54 mg)"][17,"CDISCPILOT01","DM","CDISC017","1250","2013-09-21","2014-02-08","2013-09-21","2014-01-31","2013-08-21","2014-03-08","","","718","1931",82,"YEARS","F","WHITE","HISPANIC OR LATINO","ZAN_LOW","Zanomaline Low Dose (54 mg)","ZAN_LOW","Zanomaline Low Dose (54 mg)","","","USA","Zanomaline Low Dose (54 mg)","Zanomaline Low Dose (54 mg)"][18,"CDISCPILOT01","DM","CDISC018","1427","2012-12-17","2013-02-18","2012-12-17","2013-02-11","2012-12-13","2013-06-03","","","718","1938",74,"YEARS","F","BLACK OR AFRICAN AMERICAN","NOT HISPANIC OR LATINO","ZAN_HIGH","Zanomaline High Dose (81 mg)","ZAN_HIGH","Zanomaline High Dose (81 mg)","","","USA","Zanomaline High Dose (81 mg)","Zanomaline High Dose (81 mg)"]]