Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate study timing given sdtm data. #14

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.Rproj.user
.Rhistory
.Rdata
17 changes: 17 additions & 0 deletions R/getDefaultData.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
getDefaultData <- function(standard) {
data <- list()

if (standard == 'sdtm') {
data$aes <- safetyData::sdtm_ae
data$dm <- safetyData::sdtm_dm
data$labs <- safetyData::sdtm_lb
}

if (standard == 'adam') {
data$aes <- safetyData::adam_adae
data$dm <- safetyData::adam_adsl
data$labs <- safetyData::adam_adlbc
}

data
}
63 changes: 63 additions & 0 deletions R/getDefaultSettings.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
getDefaultSettings <- function(standard) {
settings <- list()

if (standard == 'sdtm') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should be able to use safetyGraphics::makeMapping to do this. Let me see if i can get it working.

settings$aes <- list(
id_col = "USUBJID",
stdt_col = "AESTDTC",
endt_col = "AEENDTC",
stdy_col = NULL,
endy_col = NULL,
aeterm_col = "AETERM",
decod_col = 'AEDECOD',
bodsys_col = "AEBODSYS",
severity_col = "AESEV"
)

settings$dm <- list(
id_col = "USUBJID",
reference_date_col = 'RFSTDTC',
treatment_col = "ARM"
)

settings$labs <- list(
id_col = "USUBJID",
visit_col = 'VISIT',
visit_order_col = 'VISITNUM',
dt_col = 'LBDTC',
dy_col = NULL,
result_col = 'LBSTRESN'
)
}

if (standard == 'adam') {
settings$aes <- list(
id_col = "USUBJID",
stdt_col = 'ASTDT',
endt_col = 'AENDT',
stdy_col = "ASTDY",
endy_col = "AENDY",
aeterm_col = "AETERM",
decod_col = 'AEDECOD',
bodsys_col = "AEBODSYS",
severity_col = "AESEV"
)

settings$dm <- list(
id_col = "USUBJID",
reference_date_col = 'TRTSDT',
treatment_col = "TRT01P"
)

settings$labs <- list(
id_col = "USUBJID",
visit_col = 'AVISIT',
visit_order_col = 'AVISITN',
dt_col = 'ADT',
dy_col = 'ADY',
result_col = 'AVAL'
)
}

settings
}
59 changes: 59 additions & 0 deletions R/getTiming.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#' Calculate reference timepoint.
#'
#' @param params `list` Reactive list of input data and mappings
#' @param domain `character` Name of data domain with which to calculate reference timepoint
#' @param domainDate `character` Name of date column with which to calculate reference timepoint
#' @param reference `character` Name of data reference with which to calculate reference timepoint
#' @param referenceDate `character` Name of date column with which to calculate reference timepoint

getTiming <- function(
params,
domain,
domainDate = NULL,
refDomain = 'dm',
refDate = 'RFSTDTC',
domainStartDate = NULL,
domainEndDate = NULL
) {
# TODO: add stopifnot() logic

id_col <- params$settings[[ refDomain ]]$id_col

ref_data <- params$data[[ refDomain ]] %>%
select(
id_col, refDate
) %>%
mutate(
# TODO: add logic around date format... lubridate::ymd?
refDate = as.Date(.data[[ refDate ]])
)

domain_data <- params$data[[ domain ]] %>%
left_join(
ref_data,
id_col
)

getStudyDay <- function(data, date_col) {
data[[ date_col ]] <- as.Date(data[[ date_col ]])

data[[ paste0(date_col, '_dy') ]] <- as.numeric(
data[[ date_col ]] - data$refDate
) + (
data[[ date_col ]] >= data$refDate
)

data
}

if (!is.null(domainDate))
domain_data <- getStudyDay(domain_data, domainDate)

if (!is.null(domainStartDate))
domain_data <- getStudyDay(domain_data, domainStartDate)

if (!is.null(domainEndDate))
domain_data <- getStudyDay(domain_data, domainEndDate)

domain_data
}
92 changes: 62 additions & 30 deletions R/profileApp.R
Original file line number Diff line number Diff line change
@@ -1,53 +1,85 @@
#' Safety Profile App
#'
#' @param dfAE AE Data
#' @param dfDemog demog data
#' @param settings safetyGraphics settings
#' @param data `list` Named list of data domains
#' @param settings `list` Named list of data mappings
#' @param standard `character` Name of data standard
#' @param runNow `logical` Run app?
#'
#' @import shiny
#' @return `shiny.appobj` Shiny app
#'
#' @importFrom shiny shinyApp callModule
#'
#' @export

profileApp <- function(
data = list(
aes=safetyData::adam_adae,
dm = safetyData::adam_adsl,
labs=safetyData::adam_adlbc
),
settings=NULL,
runNow=TRUE
){

## create default settings when settings is not defined by default
if(is.null(settings)){
settings<-list(
labs=list(id_col="USUBJID"),
aes=list(id_col="USUBJID", siteid_col="SITEID", trarm_col="TRTA",
bodsys_col="AEBODSYS", term_col = 'AEDECOD',
aeterm_col="AETERM", severity_col="AESEV",
stdy_col="ASTDY", endy_col="AENDY"),
dm=list(id_col="USUBJID", treatment_col="ARM")
data = NULL,
settings = NULL,
standard = 'adam',
runNow = TRUE
) {
stopifnot(
'[ standard ] must be a character-classed variable.' =
class(standard) == 'character',
'[ standard ] must be one of "sdtm", "adam", "custom".' =
tolower(standard) %in% c('sdtm', 'adam', 'custom')
)

standard <- tolower(standard)

## Create list of default data when undefined.
if (is.null(data))
data <- getDefaultData(standard)

## Create list of default settings when undefined.
if (is.null(settings))
settings <- getDefaultSettings(standard)

## Calculate study timing.
if (standard == 'sdtm') {
data$aes <- getTiming(
params = list(
data = data,
settings = settings,
standard = standard
),
'aes',
domainStartDate = settings$aes$stdt_col,
domainEndDate = settings$aes$endt_col
)
settings$aes$stdy_col <- paste0(settings$aes$stdt_col, '_dy')
settings$aes$endy_col <- paste0(settings$aes$endt_col, '_dy')

data$labs <- getTiming(
params = list(
data = data,
settings = settings,
standard = standard
),
'labs',
domainDate = settings$labs$dt_col
)
settings$labs$dy_col <- paste0(settings$labs$dt_col, '_dy')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should treat this more like a utility function. Let's move this to a separate function (getSDTMTiming) and have the user call it before starting the app. That way we can use the day variable in other safetyGraphics modules as well if needed. Something like:

sdtm <- getDefaultData(standard="sdtm") %>% getSDTMTiming
safetyGraphicsApp(data=sdtm)
# or profileApp(data=sdtm)

}

## create object containing data and setting to pass to server
## Create reactive list of data and settings to pass to server
params <- reactive({
list(
data=data,
settings=settings
data = data,
settings = settings,
standard = standard
)
})

## Create app with ui and server
## Create app with UI and server
app <- shinyApp(
ui = profile_ui("profile"),
server = function(input,output,session){
server = function(input,output,session) {
callModule(profile_server, "profile", params)
}
)

#if(runNow)
if (runNow)
runApp(app)
#else
#app
else
app
}
5 changes: 1 addition & 4 deletions R/profile_server.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ profile_server <- function(input, output, session, params) {

## set up some basic reactives for convenience
id_col<-reactive({

params()$settings$dm$id_col
})

Expand Down Expand Up @@ -78,9 +77,7 @@ profile_server <- function(input, output, session, params) {
print(params()$data$aes %>% filter(!!sym(id_col()) == input$idSelect))# %in% input$idSelect])
})



# TODO Make this dynamic for any domain provided (use a sub-module?)
# TODO: Make this dynamic for any domain provided (use a sub-module?)
output$overview <- renderDT({domain_choice() %>% filter(!!sym(id_col()) == input$idSelect)})

output$AEplot <- renderPlot({
Expand Down