From be6a136b8f0a532d8b63b5bf9251379f1838e131 Mon Sep 17 00:00:00 2001 From: dannyparsons Date: Thu, 18 Aug 2016 17:04:14 +0100 Subject: [PATCH 1/2] initial work on new calculation object --- .../R/Backend_Components/calculations.R | 72 ++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/instat/static/InstatObject/R/Backend_Components/calculations.R b/instat/static/InstatObject/R/Backend_Components/calculations.R index 11a7d4bd32f..7eb1f0f87da 100644 --- a/instat/static/InstatObject/R/Backend_Components/calculations.R +++ b/instat/static/InstatObject/R/Backend_Components/calculations.R @@ -70,4 +70,74 @@ data_object$set("public", "save_calculation", function(calc) { private$calculations[[calc$name]] <- calc return(calc$name) } -) \ No newline at end of file +) + +instat_calculation <- R6Class("instat_calculation", + public = list( + initialize = function(function_name = "", type = "", sub_type = "", name = "", manipulations = list(), + sub_calculations = list(), calculated_from = list()) { + self$function_name <- function_name + self$type <- type + self$sub_type <- sub_type + self$name <- name + self$manipulations <- manipulations + self$sub_calculations <- sub_calculations + self$calculated_from <- calculated_from + }, + name = "", + type = "", + sub_type = "", + manipulations = list(), + sub_calculations = list(), + function_name = "", + calculated_from = list() + ) +) + +instat_object$set("public", "apply_instat_calculation", function(calc, current_manipulations = list()) { + if(calc$type == "simple") { + + } + else if(calc$type == "summary") { + for(sub_calc in calc$subcalculations){ + self$apply_instat_calculation(sub_calc, current_manipulations) + } + curr_data <- self$get_data_for_calculation(calc, current_manipulations) + + } + else if(calc$type == "manipulation") { + + } +} +) + +instat_object$set("public", "get_data_for_calculation", function(calc, current_manipulations = list()) { + all_manipulations <- c(current_manipulations, calc$manipulations) + data_names <- unique(as.vector(sapply(all_manipulations, function(x) names(x$calculated_from)))) + if(length(data_names == 1)) { + curr_data <- self$get_data_frame(data_names) + for(manipulation in all_manipulations) { + if(manipulation$sub_type == "by") { + col_names_exp = c() + i = 1 + for(col_name in manipulation$calculated_from) { + if(!(col_name %in% names(curr_data))) { + stop(col_name, " not found in data.") + } + col_names_exp[[i]] <- interp(~ var, var = as.name(col_name)) + i = i + 1 + } + curr_data <- curr_data %>% group_by_(.dots = col_names_exp, add = TRUE) + } + else if(manipulation$sub_type == "filter") { + } + } + } + return(curr_data) +} +) + +manip_vil <- instat_calculation$new(type = "manipulation", sub_type = "by", calculated_from = list(survey = "Village.")) +manip_var <- instat_calculation$new(type = "manipulation", sub_type = "by", calculated_from = list(survey = "Variety.")) +calc <- instat_calculation$new(manipulations = list(manip_vil, manip_var)) +InstatDataObject$get_data_for_calculation(calc) \ No newline at end of file From d1f076e69f67dc7493808a32629f1629240fd321 Mon Sep 17 00:00:00 2001 From: dannyparsons Date: Fri, 19 Aug 2016 11:58:17 +0100 Subject: [PATCH 2/2] further work on new calculations --- .../R/Backend_Components/calculations.R | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/instat/static/InstatObject/R/Backend_Components/calculations.R b/instat/static/InstatObject/R/Backend_Components/calculations.R index 7eb1f0f87da..eb46ef6c830 100644 --- a/instat/static/InstatObject/R/Backend_Components/calculations.R +++ b/instat/static/InstatObject/R/Backend_Components/calculations.R @@ -95,18 +95,20 @@ instat_calculation <- R6Class("instat_calculation", ) instat_object$set("public", "apply_instat_calculation", function(calc, current_manipulations = list()) { - if(calc$type == "simple") { - - } - else if(calc$type == "summary") { - for(sub_calc in calc$subcalculations){ - self$apply_instat_calculation(sub_calc, current_manipulations) + if(calc$type == "calculation") { + for(manipulation in calc$manipulations){ + current_manipulations <- self$apply_instat_calculation(manipulation, current_manipulations) + } + sub_calc_results <- list() + for(sub_calc in calc$subcalculations) { + sub_calc_results[[i]] <- self$apply_instat_calculation(sub_calc, current_manipulations) } curr_data <- self$get_data_for_calculation(calc, current_manipulations) - + calc$apply_calc() + self$save_caculation_results() } else if(calc$type == "manipulation") { - + return(c(current_manipulations, calc)) } } )