secmalasct/utils.R
Jens Sauer 351b26368d uitils: Remove global data object before reinitializing
It seems that just overriding the object may not be enough to pure it.
Before loading the data, the variable "secmal" is removed.
2020-12-21 16:39:24 +01:00

126 lines
3.6 KiB
R

# secMalASCT utilities
#
# License: GPL version 3
# Jens Mathis Sauer (c) 2020
library(survival)
library(survminer)
library(tidyverse)
#
# Write plot to filename
#
# This will plot "fun" with arguments "..." as filetype "ftype" and
# the output to "filename". Title and legends can be passed as list,
# or NULL if not needed.
# Default filesize is 3000x3000 px and 300 dpi resolution.
#
sma_plot_file <- function(fname, ftype, fun, width = 4000, height = 3000) {
# Open file for writing
ftype(filename = fname, width = width, height = height, res = 300)
#
# Write plot into variable, then printing it manually. This is
# needed to save plots from package "survminer" at the moment.
#
# See: https://github.com/kassambara/survminer/issues/152
#
p <- fun()
print(p)
# hide "null device 1" in garbage
garbage <- dev.off()
}
#
# Load secMalASCT data
#
# This loads the secMalASCT data from a CSV file and adjusts some
# columns to the correct type.
#
sma_load_data <- function(file) {
sma <- as_tibble(read.csv(file, header = TRUE))
# Type adjustments
sma$follow_up_multi <- as.logical(sma$follow_up_multi)
sma$nicotin_history <- as.logical(sma$nicotin_history)
sma$relapse_treatment_asct <- as.logical(sma$relapse_treatment_asct)
sma$relapse_treatment_chemotherapy <- as.logical(sma$relapse_treatment_chemotherapy)
sma$relapse_treatment_radiotherapy <- as.logical(sma$relapse_treatment_radiotherapy)
sma$relapse_treatment_surgery <- as.logical(sma$relapse_treatment_surgery)
sma$pre_rt <- as.logical(sma$pre_rt)
sma$pre_rt_type <- as.factor(sma$pre_rt_type)
sma$post_rt <- as.logical(sma$post_rt)
sma$post_rt_type <- as.factor(sma$post_rt_type)
sma$pre_alkylating_agents <- as.logical(sma$pre_alkylating_agents)
sma$pre_topoisomerase <- as.logical(sma$pre_topoisomerase)
sma$pre_anthracyclines <- as.logical(sma$pre_anthracyclines)
sma$peri_alkylating_agents <- as.logical(sma$peri_alkylating_agents)
sma$peri_topoisomerase <- as.logical(sma$peri_topoisomerase)
sma$peri_anthracyclines <- as.logical(sma$peri_anthracyclines)
sma$post_alkylating_agents <- as.logical(sma$post_alkylating_agents)
sma$post_topoisomerase <- as.logical(sma$post_topoisomerase)
sma$post_anthracyclines <- as.logical(sma$post_anthracyclines)
sma$maintenance_treatment <- as.logical(sma$maintenance_treatment)
sma$maintenance_treatment_time <- as.integer(sma$maintenance_treatment_time)
sma$maintenance_treatment_type <- as.factor(sma$maintenance_treatment_type)
sma$thalidomid <- as.logical(sma$thalidomid)
sma$bortezomib <- as.logical(sma$bortezomib)
#
# Limit to patients which are at least 16.0 years old at time
# of transplantation.
sma <- filter(sma, asct_age >= 16.0)
# At this time only solid tumors are important
sma <- filter(sma, diagnosis_type == "solid")
# After filtering unused levels must be cleared from the data
sma <- droplevels(sma)
return(sma)
}
#
# Global break.time.by value
#
sma_break.time.by = 5 * 365.25
#
# Initialize secMalASCT workspace
#
# This function can be called multiple times, the data will only
# be loaded once. To manually reload data call:
# > sma_init_force()
#
sma_init <- function() {
if (exists("sma_initialized") == FALSE) {
print("Initialize secMalASCT workspace")
print("Loading data")
#
# Use superassignment operator '<<-' to make
# secmal and sma_initialized with global scope.
#
secmal <<- sma_load_data("data/data.csv")
sma_initialized <<- TRUE
}
}
#
# Force reinitialization of workspace
#
# Forcefully calls sma_init() again.
#
sma_init_force <- function() {
print("Reloading secMalASCT data")
remove(sma_initialized, pos = ".GlobalEnv")
remove(secmal, pos = ".GlobalEnv")
sma_init()
}