As I said, i have checked the structure multiple times, but cant find an error.
I have tried rebuilding it and testing each time i add a part. Each part
functions isolated, but not together in the same function. I can't find any
infinite loops either.
I suspect the function may be too large, and i have to define functions for
each part separately. That isn't an issue necessarily, but i would still like
to know why my code won't run. And whether there are any downsides or
considerations for using many small functions.
Below is my code. I have left part of it out. There are six more parts like the
diabetes part that are similar.
I also use a lot of data/variabeles not included here, to try and keep things
compact. But I can provide additional information if helpful.
Thanks it advance for thinking along!!
Kind regards,
Emily
The code:
decision_algorithm <- function(AB_list, dataset_ab = data.frame(), diagnose = 'cystitis',
diabetes_status = "nee", katheter_status = "nee",
lang_QT_status = "nee", obesitas_status = "nee",
zwangerschap_status = "nee",
medicatie_actief = data.frame(dict[["med_AB"]]), geslacht
= "man", gfr=90){
# vars
list_AB_status <- setNames(as.list(rep("green", length(AB_list))),
names(AB_list)) #make a dict of all AB's and assign status green as deafault for status
list_AB_remarks <- setNames(as.list(rep("Geen opmerkingen", length(AB_list))),
names(AB_list)) #make a dict of all AB's and assign "Geen" as default for remarks #Try empty
list
list_AB_dosering <- setNames(as.list(rep("Geen informatie", length(AB_list))),
names(AB_list)) # make named list of all AB's and assign "Geen informatie", will be replaced
with actual information in algorithm
list_AB_duur <- setNames(as.list(rep("Geen informatie", length(AB_list))),
names(AB_list)) # make named list of all AB's and assign "Geen informatie", will be replaced
with actual information in algorithm
##### CULTURES #####
for (i in names(AB_list)) {
ab_data <- dataset_ab[dataset_ab$middel == i,] #get info for this AB from dataset_ab
# Extract and split the diagnoses, dosering, and duur info for the current antibiotic
ab_diagnoses <- str_split(ab_data$diagnoses, pattern = " \\| ")[[1]]
ab_diagnose_dosering <- str_split(ab_data$`diagnose dosering`, pattern = " \\|
")[[1]]
ab_diagnose_duur <- str_split(ab_data$`diagnose duur`, pattern = " \\|
")[[1]]
# Find the index of the current diagnose in the ab_diagnoses list
diagnose_index <- match(diagnose, ab_diagnoses)
# Determine dosering and duur based on the diagnose_index
if (!is.na(diagnose_index)) {
dosering <- ifelse(ab_diagnose_dosering[diagnose_index] == "standaard",
ab_data$dosering, ab_diagnose_dosering[diagnose_index])
duur <- ifelse(ab_diagnose_duur[diagnose_index] == "standaard",
ab_data$duur, ab_diagnose_duur[diagnose_index])
} else {
# Use general dosering and duur as fallback if diagnose is not found
dosering <- ab_data$dosering
duur <- ab_data$duur
}
list_AB_dosering[[i]] <- dosering
list_AB_duur[[i]] <- duur
if ((!is.null(AB_list[[i]]) && AB_list[[i]] == "I")) {
list_AB_status[[i]] <- "yellow"
list_AB_remarks[[i]] <- "Kweek verminderd gevoelig"
} else if ((!is.null(AB_list[[i]]) && AB_list[[i]] == "R")) {
list_AB_status[[i]] <- "red"
list_AB_remarks[[i]] <- "Kweek resistent"
}else if ((!is.null(AB_list[[i]]) && AB_list[[i]] == "S")) {
next
} else {
list_AB_status[[i]] <- "yellow"
list_AB_remarks[[i]] <- "Geen kweekgegevens"
}
# counters, for check if dosering / duur are updated more than once
dosering_update_count <- 0
duur_update_count <- 0
##### DIABETES #####
if (diabetes_status == "ja") {
if (ab_data$'diabetes veiligheid' == "ja") {
list_AB_status[[i]] <- "red"
list_AB_remarks[[i]] <- paste(list_AB_remarks[[i]], "Niet veilig met
diabetes")
}
if (ab_data$'diabetes effectiviteit' == "aanpassing") {
dosering <- ifelse(ab_data$'diabetes dosering' != "standaard",
ab_data$'diabetes dosering', dosering) # if dosering does not equal standaard, apply
dosering in column, otherwise keep initial dosering
duur <- ifelse(ab_data$'diabetes duur' != "standaard",
ab_data$'diabetes duur', duur) # if dosering does not equal standaard, apply dosering in
column, otherwise keep initial dosering
dosering_update_count <- dosering_update_count + 1
duur_update_count <- duur_update_count + 1
list_AB_remarks[[i]] <- paste(list_AB_remarks[[i]], ab_data$'diabetes
opmerkingen')
}
} else if (diabetes_status == "?") {
if (ab_data$'diabetes veiligheid' == "ja") {
list_AB_remarks[[i]] <- paste(list_AB_remarks[[i]], "Waarschuwing: Dit
middel kan veiligheidsimplicaties hebben bij diabetes.")
}
if (ab_data$'diabetes effectiviteit' == "aanpassing") {
list_AB_remarks[[i]] <- paste(list_AB_remarks[[i]], "Waarschuwing: Dit
middel kan dosisaanpassingen vereisen bij diabetes.")
}
}
list_AB_dosering[[i]] <- dosering
list_AB_duur[[i]] <- duur
# within for loop
}
# within function
return(list(status = list_AB_status, remarks = list_AB_remarks, duur =
list_AB_duur, dosering = list_AB_dosering))
}
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.