#Packages

{
library(DiagrammeR)
library(tidyverse)
library(readxl)
library(writexl)
library(glue)
library(readxl)
library(tableone)
library(ggplot2)
library(ggalluvial)
library(forcats)
library(cowplot)
library(scales)
library(magick)
}

#Rename dataset

{
Total_included <- XXXXXX_Total_included
Total_included$Age <- as.numeric(Total_included$Age)
}  

#Group Division

{
Tot1 <- filter(Total_included, !grepl("Liquor", Total_included$Indication_Group))
NoCSF <- filter(Tot1, Tot1$Secondary_CSFOD_binary == 0)
Tot_Tumor <- filter(Total_included, grepl("Tumor", Total_included$Indication_Group))
Tot_VH <- filter(Total_included, grepl("Yes", Total_included$Hemorrhagic))
Tot_VNH <- filter(Total_included, grepl("Yes", Total_included$`Not Hemorrhagic`))
Tot_Trauma <- filter(Total_included, grepl("Trauma", Total_included$Indication_Group))
CSF <- filter(Total_included, grepl("Yes", Total_included$Secondary_CSFOD))
CSF_Tumor <- filter(CSF, grepl("Tumor", CSF$Indication_Group))
CSF_VH <- filter(CSF, grepl("Yes", CSF$Hemorrhagic))
CSF_VNH <- filter(CSF, grepl("Yes", CSF$`Not Hemorrhagic`))
CSF_Trauma <- filter(CSF, grepl("Trauma", CSF$Indication_Group))
noCSF <- filter(Tot1, Tot1$Secondary_CSFOD_binary == 0) %>% nrow()
noCSFTumor <- filter(Tot_Tumor, Tot_Tumor$Secondary_CSFOD_binary == 0) %>% nrow()
noCSFVH <- filter(Tot_VH, Tot_VH$Secondary_CSFOD_binary == 0) %>% nrow()
noCSFVNH <- filter(Tot_VNH, Tot_VNH$Secondary_CSFOD_binary == 0) %>% nrow()
noCSFTrauma <- filter(Tot_Trauma, Tot_Trauma$Secondary_CSFOD_binary == 0) %>% nrow()
}

#Number of patients per indication group

{
nTot <- nrow(Total_included)
nTot1 <- filter(Total_included, !grepl("Liquor", Total_included$Indication_Group)) %>% nrow()
nNoCSF <- nrow(NoCSF)
nTotTumor <- nrow(Tot_Tumor)
nTotVH <- nrow(Tot_VH)
nTotVNH <- nrow(Tot_VNH)
nTotTrauma <- nrow(Tot_Trauma)
nCSF <- nrow(CSF)
nCSFTumor <- nrow(CSF_Tumor)
nCSFVH <- nrow(CSF_VH)
nCSFVNH <- nrow(CSF_VNH)
nCSFTrauma <- nrow(CSF_Trauma)
}

#Base Analysis

Tot_Data <- {
  #Age
  mean_age_Tot <- mean(Tot1$Age)
  mean_age_CSF <- mean(CSF$Age)
  mean_age_noCSF <- mean(NoCSF$Age)
  SD_age_Tot <- sd(Tot1$Age)
  SD_age_CSF <- sd(CSF$Age)
  SD_age_noCSF <- sd(NoCSF$Age)
  age_Tot <- sprintf("%f (%.2f)", mean_age_Tot, SD_age_Tot)
  age_CSF <- sprintf("%f (%.2f)", mean_age_CSF, SD_age_CSF)
  age_noCSF <- sprintf("%f (%.2f)", mean_age_noCSF, SD_age_noCSF)
  #Sex Tot
  n_sex_m_Tot <- filter(Tot1, grepl("m", Tot1$Sex)) %>% nrow()
  perc_sex_m_Tot <- n_sex_m_Tot/nTot1 *100
  n_sex_f_Tot <- filter(Tot1, grepl("w", Tot1$Sex)) %>% nrow()
  perc_sex_f_Tot <- n_sex_f_Tot/nTot1 *100
  sex_m_Tot <- sprintf("%d (%.2f)", n_sex_m_Tot, perc_sex_m_Tot)
  sex_f_Tot <- sprintf("%d (%.2f)", n_sex_f_Tot, perc_sex_f_Tot)
  #Sex CSF
  n_sex_m_CSF <- filter(CSF, grepl("m", CSF$Sex)) %>% nrow()
  perc_sex_m_CSF <- n_sex_m_CSF/n_sex_m_Tot *100 
  n_sex_f_CSF <- filter(CSF, grepl("w", CSF$Sex)) %>% nrow()
  perc_sex_f_CSF <- n_sex_f_CSF/n_sex_f_Tot *100 
  sexmCSF <- sprintf("%d (%.2f)", n_sex_m_CSF, perc_sex_m_CSF)
  sexfCSF <- sprintf("%d (%.2f)", n_sex_f_CSF, perc_sex_f_CSF)
  #Sex NoCSF
  nsex_mNoCSF <- filter(NoCSF, grepl("m", NoCSF$Sex)) %>% nrow()
  percsex_mNoCSF <- nsex_mNoCSF/n_sex_m_Tot *100 
  nsex_fNoCSF <- filter(NoCSF, grepl("w", NoCSF$Sex)) %>% nrow()
  percsex_fNoCSF <- nsex_fNoCSF/n_sex_f_Tot *100
  sexmNoCSF <- sprintf("%d (%.2f)", nsex_mNoCSF, percsex_mNoCSF)
  sexfNoCSF <- sprintf("%d (%.2f)", nsex_fNoCSF, percsex_fNoCSF)
  #Tumor Tot
  percTotTumor <- nTotTumor/nTot1 *100
  resTotTumor <- prop.test(nTotTumor, nTot1)
  lowTotTumor <- resTotTumor$conf.int[1] *100
  highTotTumor <- resTotTumor$conf.int[2] *100
  resultTotTumor <- sprintf("%d (%.2f, %.2f-%.2f)", nTotTumor, percTotTumor, lowTotTumor, highTotTumor)
  #VH Tot
  percTotVH <- nTotVH/nTot1 *100
  resTotVH <- prop.test(nTotVH, nTot1)
  lowTotVH <- resTotVH$conf.int[1] *100
  highTotVH <- resTotVH$conf.int[2] *100
  resultTotVH <- sprintf("%d (%.2f, %.2f-%.2f)", nTotVH, percTotVH, lowTotVH, highTotVH)
  #VNH Tot
  percTotVNH <- nTotVNH/nTot1 *100
  resTotVNH <- prop.test(nTotVNH, nTot1)
  lowTotVNH <- resTotVNH$conf.int[1] *100
  highTotVNH <- resTotVNH$conf.int[2] *100
  resultTotVNH <- sprintf("%d (%.2f, %.2f-%.2f)", nTotVNH, percTotVNH, lowTotVNH, highTotVNH)
  #Trauma Tot
  percTotTrauma <- nTotTrauma/nTot1 *100
  resTotTrauma <- prop.test(nTotTrauma, nTot1)
  lowTotTrauma <- resTotTrauma$conf.int[1] *100
  highTotTrauma <- resTotTrauma$conf.int[2] *100
  resultTotTrauma <- sprintf("%d (%.2f, %.2f-%.2f)", nTotTrauma, percTotTrauma, lowTotTrauma, highTotTrauma)
  #CSF Tot over tot Patients
  resCSF <- prop.test(nCSF, nTot1)
  percCSF <- nCSF/nTot1 * 100
  lowCSF <- resCSF$conf.int[1] * 100
  highCSF <- resCSF$conf.int[2] * 100
  resultCSF <- sprintf("%d (%.2f, %.2f-%.2f)", nCSF, percCSF, lowCSF, highCSF)
  #CSF Tumor over tot Tumor
  resCSFTumor <- prop.test(nCSFTumor, nTotTumor)
  percCSFTumor <- nCSFTumor/nTotTumor * 100
  lowCSFTumor <- resCSFTumor$conf.int[1] * 100
  highCSFTumor <- resCSFTumor$conf.int[2] * 100
  resultCSFTumor <- sprintf("%d (%.2f, %.2f-%.2f)", nCSFTumor, percCSFTumor, lowCSFTumor, highCSFTumor)
  #CSF VH over tot VH
  resCSFVH <- prop.test(nCSFVH, nTotVH)
  percCSFVH <- nCSFVH/nTotVH * 100
  lowCSFVH <- resCSFVH$conf.int[1] * 100
  highCSFVH <- resCSFVH$conf.int[2] * 100
  resultCSFVH <- sprintf("%d (%.2f, %.2f-%.2f)", nCSFVH, percCSFVH, lowCSFVH, highCSFVH)
  #CSF VNH over tot VNH
  resCSFVNH <- prop.test(nCSFVNH, nTotVNH)
  percCSFVNH <- nCSFVNH/nTotVNH * 100
  lowCSFVNH <- resCSFVNH$conf.int[1] * 100
  highCSFVNH <- resCSFVNH$conf.int[2] * 100
  resultCSFVNH <- sprintf("%d (%.2f, %.2f-%.2f)", nCSFVNH, percCSFVNH, lowCSFVNH, highCSFVNH)
  #CSF Trauma over tot trauma
  resCSFTrauma <- prop.test(nCSFTrauma, nTotTrauma)
  percCSFTrauma <- nCSFTrauma/nTotTrauma * 100
  lowCSFTrauma <- resCSFTrauma$conf.int[1] * 100
  highCSFTrauma <- resCSFTrauma$conf.int[2] * 100
  resultCSFTrauma <- sprintf("%d (%.2f, %.2f-%.2f)", nCSFTrauma, percCSFTrauma, lowCSFTrauma, highCSFTrauma)
  #No CSF
  resnoCSF <- prop.test(noCSF, nTot1)
  percnoCSF <- noCSF/nTot1 *100
  lownoCSF <- resnoCSF$conf.int[1] *100
  highnoCSF <- resnoCSF$conf.int[2] *100
  resultnoCSF <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percnoCSF, lownoCSF, highnoCSF)
  resnoCSFTumor <- prop.test(noCSFTumor, nTotTumor)
  percnoCSFTumor <- noCSFTumor/nTotTumor *100
  lownoCSFTumor <- resnoCSFTumor$conf.int[1] *100
  highnoCSFTumor <- resnoCSFTumor$conf.int[2] *100
  resultnoCSFTumor <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percnoCSFTumor, lownoCSFTumor, highnoCSFTumor)
  resnoCSFVH <- prop.test(noCSFVH, nTotVH)
  percnoCSFVH <- noCSFVH/nTotVH *100
  lownoCSFVH <- resnoCSFVH$conf.int[1] *100
  highnoCSFVH <- resnoCSFVH$conf.int[2] *100
  resultnoCSFVH <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percnoCSFVH, lownoCSFVH, highnoCSFVH)
  resnoCSFVNH <- prop.test(noCSFVNH, nTotVNH)
  percnoCSFVNH <- noCSFVNH/nTotVNH *100
  lownoCSFVNH <- resnoCSFVNH$conf.int[1] *100
  highnoCSFVNH <- resnoCSFVNH$conf.int[2] *100
  resultnoCSFVNH <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percnoCSFVNH, lownoCSFVNH, highnoCSFVNH)
  resnoCSFTrauma <- prop.test(noCSFTrauma, nTotTrauma)
  percnoCSFTrauma <- noCSFTrauma/nTotTrauma *100
  lownoCSFTrauma <- resnoCSFTrauma$conf.int[1] *100
  highnoCSFTrauma <- resnoCSFTrauma$conf.int[2] *100
  resultnoCSFTrauma <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percnoCSFTrauma, lownoCSFTrauma, highnoCSFTrauma)
  resultnoCSFTumor <- sprintf("%d (%.2f, %.2f-%.2f)", noCSFTumor, percnoCSFTumor, lownoCSFTumor, highnoCSFTumor)
  resultnoCSFVH <- sprintf("%d (%.2f, %.2f-%.2f)", noCSFVH, percnoCSFVH, lownoCSFVH, highnoCSFVH)
  resultnoCSFVNH <- sprintf("%d (%.2f, %.2f-%.2f)", noCSFVNH, percnoCSFVNH, lownoCSFVNH, highnoCSFVNH)
  resultnoCSFTrauma <- sprintf("%d (%.2f, %.2f-%.2f)", noCSFTrauma, percnoCSFTrauma, lownoCSFTrauma, highnoCSFTrauma)
  resultnoCSFTot <- sprintf("%d (%.2f, %.2f-%.2f)", nNoCSF, percnoCSF, lownoCSF, highnoCSF)
  list(
    age_Tot = age_Tot,
    age_CSF = age_CSF,
    age_noCSF = age_noCSF,
    sex_m_Tot = sex_m_Tot, 
    sex_f_Tot = sex_f_Tot, 
    sexmCSF = sexmCSF, 
    sexfCSF = sexfCSF, 
    sexmNoCSF = sexmNoCSF, 
    sexfNoCSF = sexfNoCSF, 
    resultTotTumor = resultTotTumor, 
    resultTotVH = resultTotVH, 
    resultTotVNH = resultTotVNH, 
    resultTotTrauma = resultTotTrauma, 
    percCSF = percCSF,
    resultCSF = resultCSF,
    percCSFTumor = percCSFTumor,
    resultCSFTumor = resultCSFTumor, 
    perCSFVH = percCSFVH, 
    resultCSFVH = resultCSFVH,  
    percCSFVNH = percCSFVNH, 
    resultCSFVNH = resultCSFVNH,
    percCSFTrauma = percCSFTrauma, 
    resultCSFTrauma = resultCSFTrauma,
    resultnoCSFTumor = resultnoCSFTumor, 
    resultnoCSFVH = resultnoCSFVH, 
    resultnoCSFVNH = resultnoCSFVNH, 
    resultnoCSFTrauma = resultnoCSFTrauma,
    resultnoCSFTot = resultnoCSFTot
  )
}

CSF_Procedure <- {
  #Preparation dataset
  CSFEVDOther <- filter(CSF, CSF$Temporal_sequence == 1) %>% nrow()
  CSFShunt <- filter(CSF, CSF$Temporal_sequence == 2) %>% nrow()
  CSFEVDOtherShunt <- filter(CSF, CSF$Temporal_sequence == 3) %>% nrow()
  CSFEVDOtherTumor <- filter(CSF_Tumor, CSF_Tumor$Temporal_sequence == 1) %>% nrow()
  CSFEVDOtherVH <- filter(CSF_VH, CSF_VH$Temporal_sequence == 1) %>% nrow()
  CSFEVDOtherVNH <- filter(CSF_VNH, CSF_VNH$Temporal_sequence == 1) %>% nrow()
  CSFEVDOtherTrauma <- filter(CSF_Trauma, CSF_Trauma$Temporal_sequence == 1) %>% nrow()
  CSFShuntTumor <- filter(CSF_Tumor, CSF_Tumor$Temporal_sequence == 2) %>% nrow()
  CSFShuntVH <- filter(CSF_VH, CSF_VH$Temporal_sequence == 2) %>% nrow()
  CSFShuntVNH <- filter(CSF_VNH, CSF_VNH$Temporal_sequence == 2) %>% nrow()
  CSFShuntTrauma <- filter(CSF_Trauma, CSF_Trauma$Temporal_sequence == 2) %>% nrow()
  CSFEOSTumor <- filter(CSF_Tumor, CSF_Tumor$Temporal_sequence == 3) %>% nrow()
  CSFEOSVH <- filter(CSF_VH, CSF_VH$Temporal_sequence == 3) %>% nrow()
  CSFEOSVNH <- filter(CSF_VNH, CSF_VNH$Temporal_sequence == 3) %>% nrow()
  CSFEOSTrauma <- filter(CSF_Trauma, CSF_Trauma$Temporal_sequence == 3) %>% nrow()
  CSFEVDOtherTotVH <-  filter(CSF_VH, CSF_VH$Temporal_sequence == 1 | CSF_VH$Temporal_sequence ==3) %>% nrow()
  #CSF tot
  resCSFEVDOther <- prop.test(CSFEVDOther, nTot1)
  percCSFEVDOther <- CSFEVDOther/nTot1 * 100
  lowCSFEVDOther <- resCSFEVDOther$conf.int[1] *100
  highCSFEVDOther <- resCSFEVDOther$conf.int[2] *100
  resultCSFEVDOther <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percCSFEVDOther, lowCSFEVDOther, highCSFEVDOther)
  resCSFShunt <- prop.test(CSFShunt, nTot1)
  percCSFShunt <- CSFShunt/nTot1 *100
  lowCSFShunt <- resCSFShunt$conf.int[1] *100
  highCSFShunt <- resCSFShunt$conf.int[2] *100
  resultCSFShunt <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percCSFShunt, lowCSFShunt, highCSFShunt)
  resCSFEVDOtherShunt <- prop.test(CSFEVDOtherShunt, nTot1)
  percCSFEVDOtherShunt <- CSFEVDOtherShunt/nTot1 *100
  lowCSFEVDOtherShunt <- resCSFEVDOtherShunt$conf.int[1] *100
  highCSFEVDOtherShunt <- resCSFEVDOtherShunt$conf.int[2] *100
  resultCSFEVDOtherShunt <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percCSFEVDOtherShunt, lowCSFEVDOtherShunt, highCSFEVDOtherShunt)
  #CSF Tumor
  resCSFTumorEVDOther <- prop.test(CSFEVDOtherTumor, nTotTumor)
  percCSFTumorEVDOther <- CSFEVDOtherTumor/nTotTumor *100
  lowCSFTumorEVDOther <- resCSFTumorEVDOther$conf.int[1] *100
  highCSFTumorEVDOther <- resCSFTumorEVDOther$conf.int[2] *100
  resultCSFTumorEVDOther <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percCSFTumorEVDOther, lowCSFTumorEVDOther, highCSFTumorEVDOther)
  resCSFTumorShunt <- prop.test(CSFShuntTumor, nTotTumor)
  percCSFTumorShunt <- CSFShuntTumor/nTotTumor *100
  lowCSFTumorShunt <- resCSFTumorShunt$conf.int[1] *100
  highCSFTumorShunt <- resCSFTumorShunt$conf.int[2] *100
  resultCSFTumorShunt <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percCSFTumorShunt, lowCSFTumorShunt, highCSFTumorShunt)
  resCSFTumorEOS <- prop.test(CSFEOSTumor, nTotTumor)
  percCSFTumorEOS <- CSFEOSTumor/nTotTumor *100
  lowCSFTumorEOS <- resCSFTumorEOS$conf.int[1] *100
  highCSFTumorEOS <- resCSFTumorEOS$conf.int[2] *100
  resultCSFTumorEOS <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percCSFTumorEOS, lowCSFTumorEOS, highCSFTumorEOS)
  #CSF VH
  resCSFVHEVDOther <- prop.test(CSFEVDOtherVH, nTotVH)
  percCSFVHEVDOther <- CSFEVDOtherVH/nTotVH *100
  lowCSFVHEVDOther <- resCSFVHEVDOther$conf.int[1] *100
  highCSFVHEVDOther <- resCSFVHEVDOther$conf.int[2] *100
  resultCSFVHEVDOther <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percCSFVHEVDOther, lowCSFVHEVDOther, highCSFVHEVDOther)
  resCSFVHShunt <- prop.test(CSFShuntVH, nTotVH)
  percCSFVHShunt <- CSFShuntVH/nTotVH *100
  lowCSFVHShunt <- resCSFVHShunt$conf.int[1] *100
  highCSFVHShunt <- resCSFVHShunt$conf.int[2] *100
  resultCSFVHShunt <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percCSFVHShunt, lowCSFVHShunt, highCSFVHShunt)
  resCSFVHEOS <- prop.test(CSFEOSVH, nTotVH)
  percCSFVHEOS <- CSFEOSVH/nTotVH *100
  lowCSFVHEOS <- resCSFVHEOS$conf.int[1] *100
  highCSFVHEOS <- resCSFVHEOS$conf.int[2] *100
  resultCSFVHEOS <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percCSFVHEOS, lowCSFVHEOS, highCSFVHEOS)
  resCSFVHEVDOtherTot <- prop.test(CSFEVDOtherTotVH, nTotVH)
  percCSFVHEVDOtherTot <- CSFEVDOtherTotVH/nTotVH *100
  lowCSFVHEVDOtherTot <- resCSFVHEVDOtherTot$conf.int[1] *100
  highCSFVHEVDOtherTot <- resCSFVHEVDOtherTot$conf.int[2] *100
  resultCSFVHEVDOtherTot <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percCSFVHEVDOtherTot, lowCSFVHEVDOtherTot, highCSFVHEVDOtherTot)
  #CSF VNH
  resCSFVNHEVDOther <- prop.test(CSFEVDOtherVNH, nTotVNH)
  percCSFVNHEVDOther <- CSFEVDOtherVNH/nTotVNH *100
  lowCSFVNHEVDOther <- resCSFVNHEVDOther$conf.int[1] *100
  highCSFVNHEVDOther <- resCSFVNHEVDOther$conf.int[2] *100
  resultCSFVNHEVDOther <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percCSFVNHEVDOther, lowCSFVNHEVDOther, highCSFVNHEVDOther)
  resCSFVNHShunt <- prop.test(CSFShuntVNH, nTotVNH)
  percCSFVNHShunt <- CSFShuntVNH/nTotVNH *100
  lowCSFVNHShunt <- resCSFVNHShunt$conf.int[1] *100
  highCSFVNHShunt <- resCSFVNHShunt$conf.int[2] *100
  resultCSFVNHShunt <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percCSFVNHShunt, lowCSFVNHShunt, highCSFVNHShunt)
  resCSFVNHEOS <- prop.test(CSFEOSVNH, nTotVNH)
  percCSFVNHEOS <- CSFEOSVNH/nTotVNH *100
  lowCSFVNHEOS <- resCSFVNHEOS$conf.int[1] *100
  highCSFVNHEOS <- resCSFVNHEOS$conf.int[2] *100
  resultCSFVNHEOS <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percCSFVNHEOS, lowCSFVNHEOS, highCSFVNHEOS)
  #CSF Trauma
  resCSFTraumaEVDOther <- prop.test(CSFEVDOtherTrauma, nTotTrauma)
  percCSFTraumaEVDOther <- CSFEVDOtherTrauma/nTotTrauma *100
  lowCSFTraumaEVDOther <- resCSFTraumaEVDOther$conf.int[1] *100
  highCSFTraumaEVDOther <- resCSFTraumaEVDOther$conf.int[2] *100
  resultCSFTraumaEVDOther <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percCSFTraumaEVDOther, lowCSFTraumaEVDOther, highCSFTraumaEVDOther)
  resCSFTraumaShunt <- prop.test(CSFShuntTrauma, nTotTrauma)
  percCSFTraumaShunt <- CSFShuntTrauma/nTotTrauma *100
  lowCSFTraumaShunt <- resCSFTraumaShunt$conf.int[1] *100
  highCSFTraumaShunt <- resCSFTraumaShunt$conf.int[2] *100
  resultCSFTraumaShunt <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percCSFTraumaShunt, lowCSFTraumaShunt, highCSFTraumaShunt)
  resCSFTraumaEOS <- prop.test(CSFEOSTrauma, nTotTrauma)
  percCSFTraumaEOS <- CSFEOSTrauma/nTotTrauma *100
  lowCSFTraumaEOS <- resCSFTraumaEOS$conf.int[1] *100
  highCSFTraumaEOS <- resCSFTraumaEOS$conf.int[2] *100
  resultCSFTraumaEOS <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percCSFTraumaEOS, lowCSFTraumaEOS, highCSFTraumaEOS)
  #No CSF
  resnoCSF <- prop.test(noCSF, nTot1)
  percnoCSF <- noCSF/nTot1 *100
  lownoCSF <- resnoCSF$conf.int[1] *100
  highnoCSF <- resnoCSF$conf.int[2] *100
  resultnoCSF <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percnoCSF, lownoCSF, highnoCSF)
  resnoCSFTumor <- prop.test(noCSFTumor, nTotTumor)
  percnoCSFTumor <- noCSFTumor/nTotTumor *100
  lownoCSFTumor <- resnoCSFTumor$conf.int[1] *100
  highnoCSFTumor <- resnoCSFTumor$conf.int[2] *100
  resultnoCSFTumor <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percnoCSFTumor, lownoCSFTumor, highnoCSFTumor)
  resnoCSFVH <- prop.test(noCSFVH, nTotVH)
  percnoCSFVH <- noCSFVH/nTotVH *100
  lownoCSFVH <- resnoCSFVH$conf.int[1] *100
  highnoCSFVH <- resnoCSFVH$conf.int[2] *100
  resultnoCSFVH <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percnoCSFVH, lownoCSFVH, highnoCSFVH)
  resnoCSFVNH <- prop.test(noCSFVNH, nTotVNH)
  percnoCSFVNH <- noCSFVNH/nTotVNH *100
  lownoCSFVNH <- resnoCSFVNH$conf.int[1] *100
  highnoCSFVNH <- resnoCSFVNH$conf.int[2] *100
  resultnoCSFVNH <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percnoCSFVNH, lownoCSFVNH, highnoCSFVNH)
  resnoCSFTrauma <- prop.test(noCSFTrauma, nTotTrauma)
  percnoCSFTrauma <- noCSFTrauma/nTotTrauma *100
  lownoCSFTrauma <- resnoCSFTrauma$conf.int[1] *100
  highnoCSFTrauma <- resnoCSFTrauma$conf.int[2] *100
  resultnoCSFTrauma <- sprintf("%.3f%% (%.3f%%, %.3f%%)", percnoCSFTrauma, lownoCSFTrauma, highnoCSFTrauma)
  #Lista
  list(
    resultCSFEVDOther = resultCSFEVDOther,
    resultCSFShunt = resultCSFShunt,
    resultCSFEVDOtherShunt = resultCSFEVDOtherShunt,
    resultnoCSF = resultnoCSF, 
    resultCSFTumorEVDOther = resultCSFTumorEVDOther,
    resultCSFTumorShunt = resultCSFTumorShunt,
    resultCSFTumorEOS = resultCSFTumorEOS,
    resultnoCSFTumor = resultnoCSFTumor, 
    resultCSFVHEVDOther = resultCSFVHEVDOther,
    resultCSFVHShunt = resultCSFVHShunt,
    resultCSFVHEOS = resultCSFVHEOS,
    resultnoCSFVH = resultnoCSFVH, 
    resultCSFVHEVDOtherTot = resultCSFVHEVDOtherTot,
    resultCSFVNHEVDOther = resultCSFVNHEVDOther,
    resultCSFVNHShunt = resultCSFVNHShunt,
    resultCSFVNHEOS = resultCSFVNHEOS,
    resultnoCSFVNH = resultnoCSFVNH,
    resultCSFTraumaEVDOther = resultCSFTraumaEVDOther,
    resultCSFTraumaShunt = resultCSFTraumaShunt,
    resultCSFTraumaEOS = resultCSFTraumaEOS, 
    resultnoCSFTrauma = resultnoCSFTrauma
  )
}

#Table 1: Basic characteristics of the study population

{
Table_1_1 <- data.frame(
  'Variables' = c("Patients, n (%)", "Age, years (SD)", "Sex, n (%, 95%CI)", "Male", "Female", "Indication group, n (%, 95%CI)", "Tumor", "Vascular hemorrhagic", "Vascular not hemorrhagic", "Trauma"),
  'Overall' = c(nTot1, Tot_Data$age_Tot, "", Tot_Data$sex_m_Tot, Tot_Data$sex_f_Tot, "", Tot_Data$resultTotTumor, Tot_Data$resultTotVH, Tot_Data$resultTotVNH, Tot_Data$resultTotTrauma), 
  'No secondary CSFOD' = c(Tot_Data$resultnoCSFTot, Tot_Data$age_noCSF, "", Tot_Data$sexmNoCSF, Tot_Data$sexfNoCSF, "", Tot_Data$resultnoCSFTumor, Tot_Data$resultnoCSFVH, Tot_Data$resultnoCSFVNH, Tot_Data$resultnoCSFTrauma),
  'Secondary CSFOD'= c(Tot_Data$resultCSF, Tot_Data$age_CSF, "", Tot_Data$sexmCSF, Tot_Data$sexfCSF, "", Tot_Data$resultCSFTumor, Tot_Data$resultCSFVH, Tot_Data$resultCSFVNH, Tot_Data$resultCSFTrauma) 
)
}

##Table 1: Specific indication

{Specificindicationtumor <- as.data.frame(table(Tot_Tumor$Indication_1, Tot_Tumor$Secondary_CSFOD))
  SpecificindicationVH <- as.data.frame(table(Tot_VH$Indication_1, Tot_VH$Secondary_CSFOD))
  SpecificindicationVNH <- as.data.frame(table(Tot_VNH$Indication_1, Tot_VNH$Secondary_CSFOD))
  Specificindicationtrauma <- as.data.frame(table(Tot_Trauma$Indication_1, Tot_Trauma$Secondary_CSFOD))
  Specificindicationtotal <- as.data.frame(table(Tot1$Indication_1))
  Specificindicationstratified <- c("Indication_1")
  Specindstrat1 <- CreateTableOne(vars = Specificindicationstratified, data = Tot1)
  Specindstrat2 <- CreateTableOne(vars = Specificindicationstratified, strata = "Secondary_CSFOD", data = Tot1)
  Specindstrat1xl <- print(Specindstrat1, printToggle = FALSE, catDigits = 2) %>% as.data.frame()
  Specindstrat2xl <- print(Specindstrat2, printToggle = FALSE, catDigits = 2) %>% as.data.frame()
}

##Figure 2A: Barchart Sex: new version

{
#Dataset with necessary data: column with indication group, column sex, column individual per sex, column width (correspond to percent, weight of the group over the total)

wTottumor <- filter(Tot_Tumor, Tot_Tumor$Sex == "w") %>% nrow()
mTottumor <- filter(Tot_Tumor, Tot_Tumor$Sex == "m") %>% nrow()
wTottrauma <- filter(Tot_Trauma, Tot_Trauma$Sex == "w") %>% nrow()
mTottrauma <- filter(Tot_Trauma, Tot_Trauma$Sex == "m") %>% nrow()
wTotVH <- filter(Tot_VH, Tot_VH$Sex == "w") %>% nrow()
mTotVH <- filter(Tot_VH, Tot_VH$Sex == "m") %>% nrow()
wTotVNH <- filter(Tot_VNH, Tot_VNH$Sex == "w") %>% nrow()
mTotVNH <- filter(Tot_VNH, Tot_VNH$Sex == "m") %>% nrow()

Dataset_BarchartSex <- data_frame(
  Indicationgroup = c("Tumor", "Tumor", "Trauma", "Trauma", "VH", "VH", "VNH", "VNH"),
  Sex = c("m", "w", "m", "w", "m", "w", "m", "w"),
  Percsex = c(mTottumor/nTotTumor, wTottumor/nTotTumor, mTottrauma/nTotTrauma, wTottrauma/nTotTrauma, mTotVH/nTotVH, wTotVH/nTotVH, mTotVNH/nTotVNH, wTotVNH/nTotVNH),
  Width = c(percTotTumor/100, percTotTumor/100, percTotTrauma/100, percTotTrauma/100, percTotVH/100, percTotVH/100, percTotVNH/100, percTotVNH/100)
)

#Calculate position on x-axis: The x-axis is not numerical; rather, it contains the four diagnostic group categories. To create a custom width, you need to take the entire x-axis—
#which is calculated as 100%—and determine where a specific group should end. Therefore, we are not actually calculating percentages, but rather modifying the
#width of the bars. However, we choose this width based on the percentage that the group represents of the total. In this way, even though the program does not actually interpret the x-axis as 
#a numerical axis or a percentage axis, the graphical result is the same)

Dataset_BarchartSex_position <- Dataset_BarchartSex %>%
  distinct(Indicationgroup, Width) %>%
  arrange(desc(Width)) %>%
  mutate(
    xmin = lag(cumsum(Width), default = 0), 
    xmax = cumsum(Width),
    xcenter = (xmax+xmin)/2
  )

#Combine the position dataset with the dataset containing the necessary data

Dataset_BarchartSex_Plot <- Dataset_BarchartSex %>%
  left_join(Dataset_BarchartSex_position, by = c("Indicationgroup", "Width")) %>%
  group_by(Indicationgroup) %>%
  mutate(
    ymin = lag(cumsum(Percsex), default = 0),
    ymax = cumsum(Percsex)
  )

#Create the graph

Figure_2A <- ggplot(Dataset_BarchartSex_Plot) +
  geom_rect(aes(xmin = xmin, xmax = xmax, 
            ymin = ymin, ymax = ymax, 
            fill = Sex), colour = "white", 
            size = 0.2) +
  scale_x_continuous(
    breaks = seq(0, 1, 0.25),
    labels = percent_format()) +
  scale_y_continuous(
    breaks = seq(0, 1, 0.25),
    labels = percent_format()) +
  scale_fill_brewer(palette = "Reds") +
  labs(
    x = "",
    y = "Sex (%)",
    fill = "Sex") +
  geom_text(aes(x = xcenter, y = 1.05, label = Indicationgroup), size = 2.81, inherit.aes = FALSE) +
  theme_minimal() +
  theme(
    text = element_text(family = "Helvetica", size = 8),
    labs = element_text(family = "Helvetica", size = 8),
    panel.grid = element_line(size = 0.2),
    legend.position = "right",
    legend.margin = margin(t = 0, r = 0, b = 0, l = 0),
    legend.box.margin = margin(0, 0, 0, 0) 
  )

Figure_2A
}  

##Figure 2B: Box plot indication group, age

{Figure_2B <- ggplot(data = Tot1, aes(x = Indication_Group_Analysis, y = Age)) +
  geom_boxplot(fill = "gray80", color = "gray40", size = 0.2) +
  labs(x = "", y = "Age (years)") +
  theme_minimal() +
  theme(text = element_text(size = 8), 
        axis.text.x = element_text(angle = 45, hjust = 1, family = "Helvetica", size = 8),
        panel.grid = element_line(size = 0.2))  

Figure_2B
}

#Figure 2: Barchart + Boxplot together: 

{Figure_2 <- plot_grid(
  Figure_2A, Figure_2B,
  labels = c("A", "B"),
  label_size = 12,
  label_fontfamily = "Helvetica",
  ncol = 2,
  align = "hv", 
  axis = "tblr"
)

Figure_2

ggsave("Figure_2A.tif",
       plot = Figure_2A,
       device = "tiff",
       width = 8, height = 6, units = "cm",
       dpi = 600,
       compression = "lzw") 
ggsave("Figure_2B.tif",
       plot = Figure_2B,
       device = "tiff",
       width = 8, height = 6, units = "cm",
       dpi = 600,
       compression = "lzw") 
}

#Figure 3: Alluvial plot: new version

{
Dataset_Alluvialplot <- data.frame(
 "Diagnosis group" = c("Tumor", "Trauma", "VH", "VNH", "Tumor", "Trauma", "VH", "VNH"),
  "Surgery for secondary CSFOD" = c("No", "No", "No", "No", "Yes", "Yes", "Yes", "Yes"),
  "Number of patients" = c(noCSFTumor, noCSFTrauma, noCSFVH, noCSFVNH, nCSFTumor, nCSFTrauma, nCSFVH, nCSFVNH))
Dataset_Alluvialplot$Diagnosis.group <- fct_rev(fct_infreq(Dataset_Alluvialplot$Diagnosis.group))
Dataset_Alluvialplot$Diagnosis.group <- fct_reorder(Dataset_Alluvialplot$Diagnosis.group, Dataset_Alluvialplot$Number.of.patients, sum)
  
Figure_3 <- ggplot(Dataset_Alluvialplot, aes(axis1 = Diagnosis.group, axis2 = Surgery.for.secondary.CSFOD, y = Number.of.patients)) +
                     geom_alluvium(aes(fill = Diagnosis.group), width = 1/3) + 
                     geom_stratum(fill = "gray80", color = "gray40", width = 1/3, size = 0.2) + 
                     geom_text(stat = "stratum", 
                               aes(label = after_stat(stratum)),
                               size = 2.81) +
                     scale_x_discrete(
                       limits = c("Diagnosis group", "Surgery for secondary CSFOD"), 
                     labels = c("Diagnosis group", "Surgery for\n secondary\n CSFOD"),
                     expand = c(.05, .05)) + 
                     labs(y = "Number of patients") +
                     theme_minimal() +
                     theme(
                       family = "Helvetica",
                       text = element_text(size = 8),
                       plot.title = element_text(size = 8), 
                       axis.title.y = element_text(size = 8), 
                       axis.text.x = element_text(margin = margin(t = -1), size = 8),
                       axis.text.y = element_text(size = 8),
                       legend.position = "none", 
                       panel.grid = element_line(size = 0.2)
                     )
Figure_3
ggsave("Figure_3.tif",
       plot = Figure_3,
       device = "tiff",
       width = 8, height = 8, units = "cm",
       dpi = 600,
       compression = "lzw") 
}

##Table 2: Temporal sequence procedures

{Table_Patients_secondary_CSFOD_Operations <- data.frame(
  `Indication Group` = c("Tumor", "", "", "", "Vascular hemorrhagic", "", "", "", "Vascular not hemorrhagic", "", "", "", "Trauma", "", "", "", "Total", "", "", ""),
  `Procedure` = c("First EVD/Other", "First Shunt", "EVD/Other, then shunt", "No CSFOD operations", "First EVD/Other", "First Shunt", "EVD/Other, then shunt", "No CSFOD operations",
                  "First EVD/Other", "First Shunt", "EVD/Other, then shunt", "No CSFOD operations", "First EVD/Other", "First Shunt", "EVD/Other, then shunt", "No CSFOD operations",
                  "First EVD/Other", "First Shunt", "EVD/Other, then shunt", "No CSFOD operations"), 
  `Number of patients` = c(CSFEVDOtherTumor, CSFShuntTumor, CSFEOSTumor, noCSFTumor, CSFEVDOtherVH, CSFShuntVH, CSFEOSVH, noCSFVH,  
                           CSFEVDOtherVNH, CSFShuntVNH, CSFEOSVNH, noCSFVNH, CSFEVDOtherTrauma, CSFShuntTrauma, CSFEOSTrauma, noCSFTrauma,
                           CSFEVDOther, CSFShunt, CSFEVDOtherShunt, noCSF), 
  `% (± IC95%)` = c(CSF_Procedure$resultCSFTumorEVDOther, CSF_Procedure$resultCSFTumorShunt, CSF_Procedure$resultCSFTumorEOS, CSF_Procedure$resultnoCSFTumor, 
                    CSF_Procedure$resultCSFVHEVDOther, CSF_Procedure$resultCSFVHShunt, CSF_Procedure$resultCSFVHEOS, CSF_Procedure$resultnoCSFVH,
                    CSF_Procedure$resultCSFVNHEVDOther, CSF_Procedure$resultCSFVNHShunt, CSF_Procedure$resultCSFVNHEOS, CSF_Procedure$resultnoCSFVNH,
                    CSF_Procedure$resultCSFTraumaEVDOther, CSF_Procedure$resultCSFTraumaShunt, CSF_Procedure$resultCSFTraumaEOS, CSF_Procedure$resultnoCSFTrauma,
                    CSF_Procedure$resultCSFEVDOther, CSF_Procedure$resultCSFShunt, CSF_Procedure$resultCSFEVDOtherShunt, CSF_Procedure$resultnoCSF))
print(Table_Patients_secondary_CSFOD_Operations)}

#Table 3: Logistic regression

{##Predictors: factor, levels, pattern
  
  Tot1$Indication_Group_Analysis <- factor(Tot1$Indication_Group_Analysis) %>% relevel(Tot1$Indication_Group_Analysis, ref = "Tumor")
  Tot1$Sex <- factor(Tot1$Sex) %>% relevel(Tot1$Sex, ref = "m")
  Tot1$decimalage <- Tot1$Age / 10
  
  ##Logistic regression: calculations
  
  LReg <- glm(Secondary_CSFOD_binary ~ decimalage+Sex+Indication_Group_Analysis, family=binomial, data=Tot1)
  summary(LReg)
  OR_CI <- exp(cbind(OR=coef(LReg), confint(LReg)))
  OR <- exp(coef(LReg))
  CI <- exp(confint(LReg))
  Table_LogReg <- as.data.frame(OR_CI)
  pvalue <- coef(summary(LReg))[, "Pr(>|z|)"]
  Tablepvalue <- as.data.frame(pvalue)
}

##Other analysis

{
CSFShunttotVH <- CSFEOSVH + CSFShuntVH 
CSFShunttotVH_res <- prop.test(CSFShunttotVH, nTotVH)
CSFShunttotVH_res$conf.int[1]*100
CSFShunttotVH_res$conf.int[2]*100
CSFShunttotVH_res$estimate*100
}

##Figure 4: Flowchart_temporal_total_New

#Figure 4A: Flow chart temporal included New

{#Data
  CSFEVDOtherall <- CSFEVDOther + CSFEVDOtherShunt
  CSFEVDOtherallVH <- CSFEVDOtherVH + CSFEOSVH
  
  #Label
  labelA1 <- glue("Patients included\n(n={nTot1})")
  labelB1 <- glue("EVD/Other")
  labelC1 <- glue("Shunt")
  labelD1 <- glue("No CSF operations")
  labelE1 <- glue("{CSFEVDOtherall}")
  labelF1 <- glue("{CSFShunt}")
  labelG1 <- glue("{nNoCSF}")
  labelH1 <- glue("{CSFEVDOtherShunt}")
  
  #Node
  
  Flowchart_temporal_total <- glue('
digraph flowchart {{
graph [fontname = Helvetica]
node [fontname = Helvetica, fontsize = 32, fixedsize = true]
edge [fontname = Helvetica]
layout=neato
rankdir = TB
splines = FALSE
nodesep = 0.5
ranksep = 0.5
A1 [shape = rectangle, label = "{labelA1}", pos=\"0,4!\", width = 10, height = 2]
B1 [shape = rectangle, label = "{labelB1}", pos=\"-3,0!\", width = 4, height = 2]
C1 [shape = rectangle, label = "{labelC1}", pos=\"-2.25,-4!\", width = 5.5, height = 2]
D1 [shape = rectangle, label = "{labelD1}", pos=\"3,-4!\", width = 4, height = 2]
E1 [shape = rectangle, label = "{labelE1}", pos=\"-4,2!\", style=filled, color=transparent]
F1 [shape = rectangle, label = "{labelF1}", pos=\"-1,2!\", style=filled, color=transparent]
G1 [shape = rectangle, label = "{labelG1}", pos=\"2,2!\", style=filled, color=transparent]
H1 [shape = rectangle, label = "{labelH1}", pos=\"-4,-2!\", style=filled, color=transparent]
a [pos=\"-3,3!\", label=\"\", shape=point, width=0.01, style=invis]
b [pos=\"-3,1!\", label=\"\", shape=point, width=0.01, style=invis]
c [pos=\"-3,-1!\", label=\"\", shape=point, width=0.01, style=invis]
d [pos=\"-3,-3!\", label=\"\", shape=point, width=0.01, style=invis]
e [pos=\"0,3!\", label=\"\", shape=point, width=0.01, style=invis]
f [pos=\"0,-3!\", label=\"\", shape=point, width=0.01, style=invis]
g [pos=\"3,3!\", label=\"\", shape=point, width=0.01, style=invis]
h [pos=\"3,-3!\", label=\"\", shape=point, width=0.01, style=invis]

#Edges
a -> b
c -> d
e -> f
g -> h
}}
')
  grViz(Flowchart_temporal_total)}

##Figure 4B: Flowchart temporal VH New

{#Data
  CSFEVDOtherall <- CSFEVDOther + CSFEVDOtherShunt
  CSFEVDOtherallVH <- CSFEVDOtherVH + CSFEOSVH
  
  #Label
  labelA2 <- glue("Vascular hemorrhagic\n(n={nTotVH})")
  labelB2 <- glue("EVD/Other")
  labelC2 <- glue("Shunt")
  labelD2 <- glue("No CSF operations")
  labelE2 <- glue("{CSFEVDOtherallVH}")
  labelF2 <- glue("{CSFShuntVH}")
  labelG2 <- glue("{noCSFVH}")
  labelH2 <- glue("{CSFEOSVH}")
  
  #Node
  
  Flowchart_temporal_VH <- glue('
digraph flowchart {{
graph [fontname = Helvetica]
node [fontname = Helvetica, fontsize = 32, fixedsize = true]
edge [fontname = Helvetica]
layout=neato
rankdir = TB
splines = FALSE
nodesep = 0.5
ranksep = 0.5
A2 [shape = rectangle, label = "{labelA2}", pos=\"0,4!\", width = 10, height = 2]
B2 [shape = rectangle, label = "{labelB2}", pos=\"-3,0!\", width = 4, height = 2]
C2 [shape = rectangle, label = "{labelC2}", pos=\"-2.25,-4!\", width = 5.5, height = 2]
D2 [shape = rectangle, label = "{labelD2}", pos=\"3,-4!\", width = 4, height = 2]
E2 [shape = rectangle, label = "{labelE2}", pos=\"-4,2!\", style=filled, color=transparent]
F2 [shape = rectangle, label = "{labelF2}", pos=\"-1,2!\", style=filled, color=transparent]
G2 [shape = rectangle, label = "{labelG2}", pos=\"2,2!\", style=filled, color=transparent]
H2 [shape = rectangle, label = "{labelH2}", pos=\"-4,-2!\", style=filled, color=transparent]
a [pos=\"-3,3!\", label=\"\", shape=point, width=0.01, style=invis]
b [pos=\"-3,1!\", label=\"\", shape=point, width=0.01, style=invis]
c [pos=\"-3,-1!\", label=\"\", shape=point, width=0.01, style=invis]
d [pos=\"-3,-3!\", label=\"\", shape=point, width=0.01, style=invis]
e [pos=\"0,3!\", label=\"\", shape=point, width=0.01, style=invis]
f [pos=\"0,-3!\", label=\"\", shape=point, width=0.01, style=invis]
g [pos=\"3,3!\", label=\"\", shape=point, width=0.01, style=invis]
h [pos=\"3,-3!\", label=\"\", shape=point, width=0.01, style=invis]

#Edges
a -> b
c -> d
e -> f
g -> h
}}
')
  grViz(Flowchart_temporal_VH)}
