---
title: "Getting Started"
output: 
  rmarkdown::html_vignette:
    toc: true
    check_title: TRUE
vignette: >
  %\VignetteIndexEntry{Getting Started}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = " "
)

library(DT)
```

```{r, include=FALSE}
local({
  hook_output <- knitr::knit_hooks$get('output')
  knitr::knit_hooks$set(output = function(x, options) {
    if (!is.null(options$max.height)) options$attr.output <- c(
      options$attr.output,
      sprintf('style="max-height: %s;"', options$max.height)
    )
    hook_output(x, options)
  })
})
```

# Getting Started with xportr

The demo will make use of a small `ADSL` data set that is apart of the  [`{admiral}`](https://pharmaverse.github.io/admiral/index.html) package. The script that generates this `ADSL` dataset can be created by using this command `admiral::use_ad_template("adsl")`.  

The `ADSL` has the following features:

* 306 observations 
* 48 variables
* Data types other than character and numeric
* Missing labels on variables
* Missing label for data set
* Order of varibles not following specification file
* Formats missing

To create a fully compliant v5 xpt `ADSL` dataset, that was developed using R, we will need to apply the 6 main functions within the `xportr` package:

* `xportr_type()`
* `xportr_length()`
* `xportr_order()`
* `xportr_format()`
* `xportr_label()`
* `xportr_write()`


```{r, eval = TRUE, message = FALSE, warning = FALSE}
# Loading packages
library(dplyr)
library(labelled)
library(xportr)
library(admiral)

# Loading in our example data
adsl <- admiral::admiral_adsl
```

<br>

```{r, echo = FALSE}
DT::datatable(adsl, options = list(
  autoWidth = FALSE, scrollX = TRUE, pageLength = 5,
  lengthMenu = c(5, 10, 15, 20)
))
```
<br>

**NOTE:** Dataset can be created by using this command `admiral::use_ad_template("adsl")`.

# Preparing your Specification Files

<br>


In order to make use of the functions within `xportr` you will need to create an R data frame  that contains your specification file.  You will most likely need to do some pre-processing of your spec sheets after loading in the spec files for them to work appropriately with the `xportr` functions.  Please see our example spec sheets in `system.file(paste0("specs/", "ADaM_admiral_spec.xlsx"), package = "xportr")` to see how `xportr` expects the specification sheets.

<br>

```{r}
var_spec <- readxl::read_xlsx(
  system.file(paste0("specs/", "ADaM_admiral_spec.xlsx"), package = "xportr"), sheet = "Variables") %>%
  dplyr::rename(type = "Data Type") %>%
  rlang::set_names(tolower) 
  
```

<br>

Below is a quick snapshot of the specification file pertaining to the `ADSL` data set, which we will make use of in the 6 `xportr` function calls below.  Take note of the order, label, type, length and format columns. 

<br>

```{r, echo = FALSE, eval = TRUE}
var_spec_view <- var_spec %>% filter(dataset == "ADSL")

DT::datatable(var_spec_view, options = list(
  autoWidth = FALSE, scrollX = TRUE, pageLength = 5,
  lengthMenu = c(5, 10, 15, 20)
))
```

<br>

# xportr_type() 

<br>

In order to be compliant with transport v5 specifications  an `xpt` file can only have two data types: character and numeric/dbl.  Currently the `ADSL` data set has chr, dbl, time, factor and date.

```{r, eval = TRUE}
look_for(adsl, details = TRUE)
```

<br>

Using `xport_type` and the supplied specification file, we can *coerce* the variables in the `ADSL` set to be either numeric or character. 

<br>

```{r, warning=FALSE, message=FALSE, echo = TRUE, results='hide'}
adsl_type <- xportr_type(adsl, var_spec, domain = "ADSL", verbose = "message") 
```

<br>
Now all appropriate types have been applied to the dataset as seen below.
<br>

```{r, eval = TRUE}
look_for(adsl_type, details = TRUE)
```

# xportr_length() 

<br>

Next we can apply the lengths from a variable level specification file to the data frame.  `xportr_length` will identify variables that are missing from your specification file.  The function will also alert you to how many lengths have been applied successfully.  Before we apply the lengths lets verify that no lengths have been applied to the original dataframe.

<br>



```{r, echo = TRUE, eval = FALSE}
str(adsl)
```

```{r, max.height='300px', attr.output='.numberLines', echo = FALSE}
str(adsl)
```

<br>

No lengths have been applied to the variables as seen in the printout - the lengths would be in the `attr` part of each variables.  Let's now use `xportr_length` to apply our lengths from the specification file.  
<br>

```{r}
adsl_length <- adsl %>% xportr_length(var_spec, domain = "ADSL", "message")
```

<br>

```{r, max.height='300px', attr.output='.numberLines', echo = TRUE}
str(adsl_length)
```

Note the additional `attr(*, "width")=` after each variable with the width.  These have been directly applied from the specification file that we loaded above!  

# xportr_order()

Please note that the order of the `ADSL` variables, see above, does not match specification file order column.  We can quickly remedy this with a call to `xportr_order()`.  Note that the variable `SITEID` has been moved as well as many others to match the specification file order column.

```{r, warning=FALSE, message=FALSE, echo = TRUE, results='hide'}
adsl_order <- xportr_order(adsl,var_spec, domain = "ADSL", verbose = "message") 
```

```{r, echo = FALSE}
DT::datatable(adsl_order, options = list(
  autoWidth = FALSE, scrollX = TRUE, pageLength = 5,
  lengthMenu = c(5, 10, 15, 20)
))
```

# xportr_format()

Now we apply formats to the dataset.  These will typically be `DATE9.`, `DATETIME20` or `TIME5`, but many others can be used.  Notice that 8 Date/Time variables are missing a format in our `ADSL` dataset. Here we just take a peak at a few `TRT` variables, which have a `NULL` format.

```{r}
attr(adsl$TRTSDT, "format.sas")
attr(adsl$TRTEDT, "format.sas")
attr(adsl$TRTSDTM, "format.sas")
attr(adsl$TRTEDTM, "format.sas")
```

Using our `xportr_format()` we apply our formats.

```{r}
adsl_fmt <- adsl %>% xportr_format(var_spec, domain = "ADSL", "message")
```

```{r}
attr(adsl_fmt$TRTSDT, "format.sas")
attr(adsl_fmt$TRTEDT, "format.sas")
attr(adsl_fmt$TRTSDTM, "format.sas")
attr(adsl_fmt$TRTEDTM, "format.sas")
```

# xportr_label() 

<br>

Please observe that our `ADSL` dataset is missing many variable labels.  Sometimes these labels can be lost while using R's function.  However,  A CDISC compliant data set needs to have each variable with a variable label. 

```{r, eval = TRUE}
look_for(adsl, details = FALSE)
```

<br>

Using the `xport_label` function we can take the specifications file and label all the variables available. `xportr_label` will produce a warning message if you the variable in the data set is not in the specification file. 

<br>

```{r}
adsl_update <- adsl %>% xportr_label(var_spec, domain = "ADSL", "message")
```

```{r}
look_for(adsl_update, details = FALSE)
```

# xportr_write()

<br>

Finally, we arrive at exporting the R data frame object as a xpt file with the function `xportr_write()`.  The xpt file will be written directly to your current working directory.  To make it more interesting, we have put together all six functions with the magrittr pipe, `%>%`.  A user can now apply types, length, variable labels, formats, data set label and write out their final xpt file in one pipe!  Appropriate warnings and messages will be supplied to a user to the console for any potential issues before sending off to standard clinical data set validator application or data reviewers.  

```{r, eval=FALSE}
adsl %>%
  xportr_type(var_spec, "ADSL", "message") %>%
  xportr_length(var_spec, "ADSL", "message") %>%
  xportr_label(var_spec, "ADSL", "message") %>%
  xportr_order(var_spec, "ADSL", "message") %>% 
  xportr_format(var_spec, "ADSL", "message") %>% 
  xportr_write("adsl.xpt", label = "Subject-Level Analysis Dataset")
```

That's it!  We now have a xpt file created in R with all appropriate types, lengths, labels, ordering and formats from our specification file.

As always, we welcome your feedback. If you spot a bug, would like to
see a new feature, or if any documentation is unclear - submit an issue
on [xportr's Github page](https://github.com/atorus-research/xportr/issues). 

