#' Create a dataframe of adverse events from a tumor registry that can be combined into a storyboard
#' @description
#' `adverse_events()`wrangles data from the Adverse Events form of tumor registries to produce a dataframe of details about adverse events of systemic therapy, which can then be incorporated into a Patient Storyboard
#' @param data is a data frame which contains the data for which you want to create a storyboard
#' @return A data frame with five variables ("record_id", "description", "value", "date", and "hover" that can be combined with others dfs with the same five variables to form a storyboard
#' @export
#' @examples
#' # Test with embedded data set "storyboard_dataset"
#' storyboard_dataset %>%
#' adverse_events()
adverse_events <- function(data){
##########################################################################################################################
# Load Data
##########################################################################################################################
dt <- data
##########################################################################################################################
##########################################################################################################################
################################################# New Data Frame ########################################################
##########################################################################################################################
##########################################################################################################################
##########################################################################################################################
# Select relevant variables
##########################################################################################################################
rx <- dt %>%
select(record_id,
redcap_repeat_instrument,
redcap_repeat_instance,
rx_name,
rx_name_other) %>%
drop_na(rx_name)
##########################################################################################################################
# If the rx_name is "Other" replace with the the entered name "rx_name_other", if there isn't one labeled, just use
## "other". Make this another variable called "rx_name.m"
##########################################################################################################################
rx <- rx %>%
mutate(rx_name.m = ifelse(rx_name == "other",
rx_name_other,
rx_name))
##########################################################################################################################
# rename rx_name.m to rx_name and narrow variables
##########################################################################################################################
rx <- rx %>%
select(record_id,
redcap_repeat_instance,
rx_name.m) %>%
rename(rx_name = rx_name.m)
##########################################################################################################################
# Change to title case
##########################################################################################################################
rx$rx_name <- stringr::str_to_title(string = rx$rx_name)
##########################################################################################################################
# Create a new variable that will be used in the hover text
##########################################################################################################################
rx <- rx %>%
mutate(rx_paste = paste("<b>Agent:</b>",
rx_name
))
##########################################################################################################################
##########################################################################################################################
################################################# New Data Frame ########################################################
##########################################################################################################################
##########################################################################################################################
##########################################################################################################################
# Use the ae_codebook function to create a df with the codes transformed
##########################################################################################################################
dt_replaced <- StoryboardR::ae_codebook(dt)
##########################################################################################################################
##########################################################################################################################
################################################# New Data Frame ########################################################
##########################################################################################################################
##########################################################################################################################
##########################################################################################################################
# Make a new df selected for a few variables and filtered for those records with an AE
##########################################################################################################################
adverse_effects <- dt_replaced %>%
select(record_id,
redcap_repeat_instrument,
redcap_repeat_instance,
ae_yn:ae_stop_dtc_yr) %>%
filter(ae_yn == "Yes")
##########################################################################################################################
# Create a new variable for hover text purposes that pastes the instrument and instance so we can use this as a key to
## link it to the df "rx" later
##########################################################################################################################
adverse_effects <- adverse_effects %>%
mutate(instrument_inst = paste(adverse_effects$redcap_repeat_instrument,
adverse_effects$redcap_repeat_instance,
sep = "_")) %>%
relocate(instrument_inst,
.after = redcap_repeat_instance)
##########################################################################################################################
##########################################################################################################################
################################################# New Data Frame ########################################################
##########################################################################################################################
##########################################################################################################################
##########################################################################################################################
# Make a new df that incorporates fields pertaining the relatedness of the drug to the AE
##########################################################################################################################
ae_sat_rel <- adverse_effects %>%
select(record_id,
instrument_inst,
contains("ae_sat_rel_"))
##########################################################################################################################
# Pivot ae_sat_rel longer
##########################################################################################################################
ae_sat_rel.long <- pivot_longer(data = ae_sat_rel,
cols = !c(record_id,
instrument_inst),
names_to = "variable",
values_to = "value") %>%
drop_na(value)
##########################################################################################################################
##########################################################################################################################
################################################# New Data Frame ########################################################
##########################################################################################################################
##########################################################################################################################
##########################################################################################################################
# Make a new df filters for those AEs that are related to a systemic therapy and create of new column for hover text
##########################################################################################################################
ae.sat.drop.unrelated <- ae_sat_rel.long %>%
filter(
str_detect(.$value,
"Probable|Definite|Possible")
) %>%
mutate(attribution_paste = paste("<b>Attribution</b>:",
value))
##########################################################################################################################
##########################################################################################################################
################################################# New Data Frame ########################################################
##########################################################################################################################
##########################################################################################################################
##########################################################################################################################
# make a new column in the rx df that will serve as a key to use to link to the "adverse events" df from earlier and
## select a subset of the columns
##########################################################################################################################
rx <- rx %>%
mutate(variable = paste("ae_sat_rel_",
rx$redcap_repeat_instance,
sep = "")) %>%
select(record_id,
variable,
rx_name,
rx_paste)
##########################################################################################################################
##########################################################################################################################
################################################# New Data Frame ########################################################
##########################################################################################################################
##########################################################################################################################
##########################################################################################################################
# make a new df of the SOC (System Organ Class) variables in CTCAE v5
##########################################################################################################################
ae <- adverse_effects %>%
select(record_id,
instrument_inst,
ae_blood:ae_search)
##########################################################################################################################
##########################################################################################################################
################################################# New Data Frame ########################################################
##########################################################################################################################
##########################################################################################################################
##########################################################################################################################
# Pivot "ae" long and drop missing rows
##########################################################################################################################
ae.long <- pivot_longer(data = ae,
cols = !c(record_id,
instrument_inst),
names_to = "variable",
values_to = "ae") %>%
drop_na(ae) %>%
select(record_id,
instrument_inst,
ae)
##########################################################################################################################
# Create a new column for hover text
##########################################################################################################################
ae.long <- ae.long %>%
mutate(ae_paste = paste("<b>Adverse Event:</b>",
ae))
##########################################################################################################################
##########################################################################################################################
################################################# New Data Frame ########################################################
##########################################################################################################################
##########################################################################################################################
##########################################################################################################################
# Left Join ae.sat.drop.unrelated and rx by record id and variable and create a new column for hover text
##########################################################################################################################
ae_sat_rel.long.rx <- left_join(ae.sat.drop.unrelated,
rx,
by = c("record_id",
"variable")) %>%
mutate(rx_attribution_paste = paste(rx_paste,
attribution_paste,
sep = ", ")) %>%
select(record_id,
instrument_inst,
rx_attribution_paste)
##########################################################################################################################
##########################################################################################################################
################################################# New Data Frame ########################################################
##########################################################################################################################
##########################################################################################################################
##########################################################################################################################
# Left Join ae_sat_rel.long.rx and ae.long
##########################################################################################################################
ae.rx <- left_join(ae_sat_rel.long.rx,
ae.long,
by = c("record_id",
"instrument_inst"))
##########################################################################################################################
##########################################################################################################################
################################################# New Data Frame ########################################################
##########################################################################################################################
##########################################################################################################################
##########################################################################################################################
# Create a new df that highlights the severity of the AE so we can combine it with the CTCAE grade
##########################################################################################################################
ae.grade <- adverse_effects %>%
select(record_id,
instrument_inst,
ae_sev) %>%
drop_na(ae_sev)
##########################################################################################################################
##########################################################################################################################
################################################# New Data Frame ########################################################
##########################################################################################################################
##########################################################################################################################
##########################################################################################################################
# Load the CTCAE v5.0 csv so we can use it as a look up table for AE grade
##########################################################################################################################
ctcae <- StoryboardR::ctcae_v5
##########################################################################################################################
# Select the first 8 columns
##########################################################################################################################
ctcae <- ctcae %>%
select(1:8)
##########################################################################################################################
##########################################################################################################################
################################################# New Data Frame ########################################################
##########################################################################################################################
##########################################################################################################################
##########################################################################################################################
# Pivot "ctcae" long and create a new column that pastes the ctcae AE with the grade and select only the new column and
##"value"
##########################################################################################################################
ctcae.long <- pivot_longer(data = ctcae,
cols = !c(`MedDRA Code`,
`MedDRA SOC`,
`CTCAE Term`),
names_to = "Grade",
values_to = "value")
ctcae.long <- ctcae.long %>%
mutate(ctcae_grade = paste(ctcae.long$`CTCAE Term`,
ctcae.long$Grade,
sep = "_")) %>%
select(ctcae_grade,
value)
##########################################################################################################################
# Trim ctcae_grade
##########################################################################################################################
ctcae.long$ctcae_grade <- stringr::str_trim(ctcae.long$ctcae_grade,
side = "right")
##########################################################################################################################
##########################################################################################################################
################################################# New Data Frame ########################################################
##########################################################################################################################
##########################################################################################################################
##########################################################################################################################
# Join ae.long and ae.grade and create a new column that pastes the AE and the grade together so we can link this with
## the severity df from above
##########################################################################################################################
ae.long.grade <- left_join(ae.long,
ae.grade,
by = c("record_id",
"instrument_inst"))
ae.long.grade <- ae.long.grade %>%
mutate(ctcae_grade = paste(ae.long.grade$ae,
ae.long.grade$ae_sev,
sep = "_")) %>%
drop_na(ae_sev)
##########################################################################################################################
# Trim the white space so the strings will match
##########################################################################################################################
ae.long.grade$ctcae_grade <- trimws(ae.long.grade$ctcae_grade)
ctcae.long$ctcae_grade <- stringr::str_trim(ctcae.long$ctcae_grade,
side = "right")
##########################################################################################################################
##########################################################################################################################
################################################# New Data Frame ########################################################
##########################################################################################################################
##########################################################################################################################
##########################################################################################################################
# Create a new df by combining ae.long.grade adn ctcae.long to form a look up table to match the AE with the grade
##########################################################################################################################
ae.ctcae.lookup <- left_join(ae.long.grade,
ctcae.long)
##########################################################################################################################
# Create a new column ae_grade_paste that will be used a hover text
##########################################################################################################################
ae.ctcae.lookup <- ae.ctcae.lookup %>%
mutate(ae_grade_paste = paste("<b>CTCAE: </b>",
ae_sev,
" (",
value,
")",
sep = ""))
##########################################################################################################################
##########################################################################################################################
################################################# New Data Frame ########################################################
##########################################################################################################################
##########################################################################################################################
##########################################################################################################################
# Create a new df that uses the df adverse_effects as a base and creates a synthetic start date as the first of the month
## if only the month and year were known in the data base
##########################################################################################################################
ae.start.synthetic <- adverse_effects %>%
select(record_id,
instrument_inst,
ae_st_dtc_yr,
ae_st_dtc_mo) %>%
drop_na(ae_st_dtc_mo) %>%
mutate(day = "01") %>%
mutate(ae_st_dtc = paste(ae_st_dtc_yr,
ae_st_dtc_mo,
day,
sep = "-")) %>%
select(record_id,
instrument_inst,
ae_st_dtc)
##########################################################################################################################
##########################################################################################################################
################################################# New Data Frame ########################################################
##########################################################################################################################
##########################################################################################################################
##########################################################################################################################
# Create a new df for those records that hava a known start date for the AE
##########################################################################################################################
ae.start <- adverse_effects %>%
select(record_id,
instrument_inst,
ae_st_dtc) %>%
drop_na(ae_st_dtc)
##########################################################################################################################
# Now join those records that have a known start date with those that may have only had a synthetic start date
##########################################################################################################################
ae.start <- rbind(ae.start,
ae.start.synthetic)
##########################################################################################################################
# make ae_st_dtc a date
##########################################################################################################################
ae.start$ae_st_dtc <- as.Date(ae.start$ae_st_dtc)
##########################################################################################################################
# make a new column for hover text
##########################################################################################################################
ae.start <- ae.start %>%
mutate(st_dtc_paste = paste("<b>Date of Onset:</b>",
ae_st_dtc))
##########################################################################################################################
##########################################################################################################################
################################################# New Data Frame ########################################################
##########################################################################################################################
##########################################################################################################################
##########################################################################################################################
# join ae.ctcae.lookup to ae.rx
##########################################################################################################################
ae.rx.lookup <- left_join(ae.rx,
ae.ctcae.lookup,
by = c("record_id",
"instrument_inst",
"ae",
"ae_paste")
)
##########################################################################################################################
##########################################################################################################################
################################################# New Data Frame ########################################################
##########################################################################################################################
##########################################################################################################################
##########################################################################################################################
# join ae.rx.lookup to ae.start
##########################################################################################################################
ae.rx.dtc <- left_join(ae.rx.lookup,
ae.start,
by = c("record_id",
"instrument_inst"))
##########################################################################################################################
##########################################################################################################################
################################################# New Data Frame ########################################################
##########################################################################################################################
##########################################################################################################################
##########################################################################################################################
# Start to Prepare the final ae df
##########################################################################################################################
ae.final.pre.1 <- ae.rx.dtc %>%
select(record_id,
ae_st_dtc,
contains("_paste"))
##########################################################################################################################
# create a new column and new df for hover
##########################################################################################################################
ae.final.pre.2 <- ae.final.pre.1 %>%
group_by(record_id,
ae_paste,
ae_grade_paste,
st_dtc_paste) %>%
mutate(drugs_grouped_paste = paste0(rx_attribution_paste,
collapse = "<br>")) %>%
ungroup()
##########################################################################################################################
# Slice to remove redundant rows (if any)
##########################################################################################################################
ae.final.pre.2.slice <- ae.final.pre.2 %>%
group_by(record_id,
ae_paste,
st_dtc_paste) %>%
slice_head() %>%
ungroup()
##########################################################################################################################
# create date column
##########################################################################################################################
ae.final.pre.3 <- ae.final.pre.2.slice %>%
select(-rx_attribution_paste) %>%
mutate(date = ae_st_dtc)
##########################################################################################################################
# Create hover text variable
##########################################################################################################################
ae.final.pre.3 <- ae.final.pre.3 %>%
mutate(hover = paste(.$ae_paste,
.$ae_grade_paste,
.$drugs_grouped_paste,
.$st_dtc_paste,
sep = "<br>"
))
##########################################################################################################################
# Create the relevant variables that will allow us to combine with other Storyboard Data Frames
## description", and "hover"
##########################################################################################################################
ae.final.pre.4 <- ae.final.pre.3 %>%
mutate(description = "Adverse Event",
value = "Adverse Event")
ae.final.pre.4$date <- as.Date(ae.final.pre.4$date)
##########################################################################################################################
# Select the relevant variables for the final dataframe
##########################################################################################################################
ae.final <- ae.final.pre.4 %>%
select(record_id,
description,
value,
date,
hover)
##########################################################################################################################
# Return the final df which is intended to be combined with other Storyboard DFs
##########################################################################################################################
return(ae.final)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.