-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdistributionInfo.R
63 lines (58 loc) · 2.07 KB
/
distributionInfo.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
library(yaml)
distributionInfoClass <- function(id, name, inputNames, labels, defaultValues, hasImplementation = TRUE, isWithSD = FALSE, fitFunc = NULL, plotlyFunc = NULL) {
structure(list(
id = as.integer(id),
name = name,
inputNames = inputNames,
labels = labels,
defaultValues = defaultValues,
hasImplementation = hasImplementation,
isWithSD = isWithSD,
fitFunc = fitFunc,
plotlyFunc = plotlyFunc
), class = "distributionInfo")
}
loadDistributionInfo <- function(file_path) {
yaml_data <- read_yaml(file_path)
parsed_list <- lapply(yaml_data, function(x) {
distributionInfoClass(
id = x$id,
name = x$name,
inputNames = x$inputNames,
labels = x$labels,
defaultValues = x$defaultValues,
hasImplementation = x$hasImplementation,
isWithSD = x$isWithSD,
fitFunc = x$fitFunc,
plotlyFunc = x$plotlyFunc
)
})
# Convert the list to a named list for distributionInfoList
distributionInfoList <- setNames(parsed_list, names(parsed_list))
# Load the fit and plotly functions for each distribution
distributionInfoList <- load_functions(distributionInfoList)
return(distributionInfoList)
}
load_functions <- function(distributionInfoList) {
for (name in names(distributionInfoList)) {
fitFuncName <- distributionInfoList[[name]]$fitFunc
if (exists(fitFuncName)) {
distributionInfoList[[name]]$fitFunc <- get(fitFuncName)
} else {
distributionInfoList[[name]]$fitFunc <- NULL
print(paste0("[WARNING] fit function ", fitFuncName, " does not exist"))
}
plotlyFuncName <- distributionInfoList[[name]]$plotlyFunc
if (exists(plotlyFuncName)) {
distributionInfoList[[name]]$plotlyFunc <- get(plotlyFuncName)
} else {
distributionInfoList[[name]]$plotlyFunc <- NULL
print(paste0("[WARNING] plotly function ", plotlyFuncName, " does not exist"))
}
}
return(distributionInfoList)
}
writeDistributionInfo <- function(distributionInfoList, file_path) {
data_to_save <- lapply(distributionInfoList, as.list)
write_yaml(data_to_save, file_path)
}