diff --git a/NEWS.md b/NEWS.md index d6b4a45e..d734f891 100644 --- a/NEWS.md +++ b/NEWS.md @@ -24,7 +24,7 @@ BUGFIXES : * `readInputThermal()` return data from file data.txt with `thermalData` parameter * `setSimulationPath()` has also the parameter **areasWithSTClusters** in 'output' mode - +* `readClusterDesc()` / `readClusterResDesc()` / `readClusterSTDesc()` return a data.table in API mode # antaresRead 2.7.0 diff --git a/R/readClusterDesc.R b/R/readClusterDesc.R index f9f75032..23b4fb2d 100644 --- a/R/readClusterDesc.R +++ b/R/readClusterDesc.R @@ -104,18 +104,17 @@ readClusterSTDesc <- function(opts = simOptions()) { "st-storage/clusters" = "st-storages" ) - if(api_study){ - + if (api_study) { # api request with all columns - list_clusters = api_get( + list_clusters <- api_get( opts = opts, endpoint = paste0(opts$study_id, "/table-mode/", table_type), - query = list( - columns = "" - ) + query = list(columns = "") ) - return(list_clusters) + dt_clusters <- .convert_list_clusterDesc_to_datatable(list_clusters) + + return(dt_clusters) } # "text" mode @@ -124,7 +123,7 @@ readClusterSTDesc <- function(opts = simOptions()) { # READ cluster properties properties <- get_input_cluster_properties(table_type = table_type, opts = opts) - + # read properties for each area res <- plyr::llply(areas, function(x, prop_ref=properties) { clusters <- readIniFile(file.path(path, x, "list.ini")) @@ -225,3 +224,29 @@ get_input_cluster_properties <- function(table_type, opts){ return(wide_ref) } + + +.convert_list_clusterDesc_to_datatable <- function(list_clusters) { + + if (length(list_clusters) == 0) { + return(data.table()) + } + + rows_cluster <- lapply(names(list_clusters), FUN = function(cl_name) { + + row_cluster <- as.data.frame(list_clusters[[cl_name]]) + row_cluster[,c("area", "cluster")] <- unlist(strsplit(cl_name, split = " / ")) + + return(row_cluster) + } + ) + + df_clusters <- do.call("rbind", rows_cluster) + id_cols <- intersect(c("area", "cluster", "group"), colnames(df_clusters)) + additional_cols <- setdiff(colnames(df_clusters), id_cols) + df_clusters <- df_clusters[,c(id_cols, additional_cols)] + df_clusters$cluster <- as.factor(tolower(df_clusters$cluster)) + colnames(df_clusters) <- tolower(colnames(df_clusters)) + + return(as.data.table(df_clusters)) +}