diff --git a/survival.R b/survival.R index f6ef2b2..9b28727 100644 --- a/survival.R +++ b/survival.R @@ -3,11 +3,8 @@ # License: GPL version 3 # Jens Mathis Sauer (c) 2020 -library(survival) -library(survminer) source("utils.R") - -secmal <- sma_load_data("current.csv") +sma_init() # Setup survival object surv_dx <- Surv(time = secmal$event_time_dx, event = secmal$event_status) diff --git a/utils.R b/utils.R index e1cae60..e047f9b 100644 --- a/utils.R +++ b/utils.R @@ -3,8 +3,6 @@ # License: GPL version 3 # Jens Mathis Sauer (c) 2020 -library(tidyverse) - # # Write plot to filename # @@ -86,3 +84,40 @@ sma_load_data <- function(file) { # 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 libraries") + library(survival) + library(survminer) + library(tidyverse) + + print("Loading data") + # + # Use superassignment operator '<<-' to make + # secmal and sma_initialized with global scope. + # + secmal <<- sma_load_data("current.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") + sma_init() +}