From 8d73c7746b7393b11d5277dbafafcd932b523a24 Mon Sep 17 00:00:00 2001 From: Danny Parsons Date: Thu, 5 Nov 2020 16:54:43 +0000 Subject: [PATCH 1/2] replacing columns keeps existing columns in same position by default, added parameter to R code to allow existing columns to be moved when replaced, fixed bug in calculator where before parameter was reset on reopening, added strAdjacentColumn as parameter to assign to methods --- instat/clsRCodeStructure.vb | 5 +-- instat/clsRSyntax.vb | 8 ++-- instat/dlgCalculator.vb | 2 +- .../R/Backend_Components/calculations.R | 4 +- instat/static/InstatObject/R/data_object_R6.R | 41 ++++++++++--------- .../static/InstatObject/R/instat_object_R6.R | 6 +-- 6 files changed, 33 insertions(+), 33 deletions(-) diff --git a/instat/clsRCodeStructure.vb b/instat/clsRCodeStructure.vb index b868bbae978..032655868aa 100644 --- a/instat/clsRCodeStructure.vb +++ b/instat/clsRCodeStructure.vb @@ -250,8 +250,7 @@ Public Class RCodeStructure ''' names of new data frames if data frame list is ''' not named. '''-------------------------------------------------------------------------------------------- - Public Sub SetAssignTo(strTemp As String, Optional strTempDataframe As String = "", Optional strTempColumn As String = "", Optional strTempModel As String = "", Optional strTempGraph As String = "", Optional strTempSurv As String = "", Optional strTempTable As String = "", Optional bAssignToIsPrefix As Boolean = False, Optional bAssignToColumnWithoutNames As Boolean = False, Optional bInsertColumnBefore As Boolean = False, Optional bRequireCorrectLength As Boolean = True, Optional bDataFrameList As Boolean = False, Optional strDataFrameNames As String = "") - + Public Sub SetAssignTo(strTemp As String, Optional strTempDataframe As String = "", Optional strTempColumn As String = "", Optional strTempModel As String = "", Optional strTempGraph As String = "", Optional strTempSurv As String = "", Optional strTempTable As String = "", Optional bAssignToIsPrefix As Boolean = False, Optional bAssignToColumnWithoutNames As Boolean = False, Optional bInsertColumnBefore As Boolean = False, Optional bRequireCorrectLength As Boolean = True, Optional bDataFrameList As Boolean = False, Optional strDataFrameNames As String = "", Optional strAdjacentColumn As String = "") strAssignTo = strTemp If Not strTempDataframe = "" Then strAssignToDataFrame = strTempDataframe @@ -277,10 +276,10 @@ Public Class RCodeStructure Me.bAssignToIsPrefix = bAssignToIsPrefix Me.bAssignToColumnWithoutNames = bAssignToColumnWithoutNames Me.bInsertColumnBefore = bInsertColumnBefore + Me.strAdjacentColumn = strAdjacentColumn Me.bRequireCorrectLength = bRequireCorrectLength Me.bDataFrameList = bDataFrameList Me.strDataFrameNames = strDataFrameNames - End Sub '''-------------------------------------------------------------------------------------------- diff --git a/instat/clsRSyntax.vb b/instat/clsRSyntax.vb index 78635453467..2f590788fa3 100644 --- a/instat/clsRSyntax.vb +++ b/instat/clsRSyntax.vb @@ -211,14 +211,14 @@ Public Class RSyntax ''' (Optional) The new value for bInsertColumnBefore. ''' (Optional) The new value for bRequireCorrectLength. '''-------------------------------------------------------------------------------------------- - Public Sub SetAssignTo(strAssignToName As String, Optional strTempDataframe As String = "", Optional strTempColumn As String = "", Optional strTempModel As String = "", Optional strTempGraph As String = "", Optional bAssignToIsPrefix As Boolean = False, Optional bAssignToColumnWithoutNames As Boolean = False, Optional bInsertColumnBefore As Boolean = False, Optional bRequireCorrectLength As Boolean = True) + Public Sub SetAssignTo(strAssignToName As String, Optional strTempDataframe As String = "", Optional strTempColumn As String = "", Optional strTempModel As String = "", Optional strTempGraph As String = "", Optional bAssignToIsPrefix As Boolean = False, Optional bAssignToColumnWithoutNames As Boolean = False, Optional bInsertColumnBefore As Boolean = False, Optional bRequireCorrectLength As Boolean = True, Optional strAdjacentColumn As String = "") If bUseBaseOperator Then - clsBaseOperator.SetAssignTo(strTemp:=strAssignToName, strTempDataframe:=strTempDataframe, strTempColumn:=strTempColumn, strTempModel:=strTempModel, strTempGraph:=strTempGraph, bAssignToIsPrefix:=bAssignToIsPrefix, bAssignToColumnWithoutNames:=bAssignToColumnWithoutNames, bInsertColumnBefore:=bInsertColumnBefore, bRequireCorrectLength:=bRequireCorrectLength) + clsBaseOperator.SetAssignTo(strTemp:=strAssignToName, strTempDataframe:=strTempDataframe, strTempColumn:=strTempColumn, strTempModel:=strTempModel, strTempGraph:=strTempGraph, bAssignToIsPrefix:=bAssignToIsPrefix, bAssignToColumnWithoutNames:=bAssignToColumnWithoutNames, bInsertColumnBefore:=bInsertColumnBefore, bRequireCorrectLength:=bRequireCorrectLength, strAdjacentColumn:=strAdjacentColumn) End If If bUseBaseFunction Then 'TODO SJL 07/04/20 Use ElseIf? - clsBaseFunction.SetAssignTo(strAssignToName, strTempDataframe:=strTempDataframe, strTempColumn:=strTempColumn, strTempModel:=strTempModel, strTempGraph:=strTempGraph, bAssignToIsPrefix:=bAssignToIsPrefix, bAssignToColumnWithoutNames:=bAssignToColumnWithoutNames, bInsertColumnBefore:=bInsertColumnBefore, bRequireCorrectLength:=bRequireCorrectLength) + clsBaseFunction.SetAssignTo(strAssignToName, strTempDataframe:=strTempDataframe, strTempColumn:=strTempColumn, strTempModel:=strTempModel, strTempGraph:=strTempGraph, bAssignToIsPrefix:=bAssignToIsPrefix, bAssignToColumnWithoutNames:=bAssignToColumnWithoutNames, bInsertColumnBefore:=bInsertColumnBefore, bRequireCorrectLength:=bRequireCorrectLength, strAdjacentColumn:=strAdjacentColumn) ElseIf bUseCommandString Then - clsBaseCommandString.SetAssignTo(strAssignToName, strTempDataframe:=strTempDataframe, strTempColumn:=strTempColumn, strTempModel:=strTempModel, strTempGraph:=strTempGraph, bAssignToIsPrefix:=bAssignToIsPrefix, bAssignToColumnWithoutNames:=bAssignToColumnWithoutNames, bInsertColumnBefore:=bInsertColumnBefore, bRequireCorrectLength:=bRequireCorrectLength) + clsBaseCommandString.SetAssignTo(strAssignToName, strTempDataframe:=strTempDataframe, strTempColumn:=strTempColumn, strTempModel:=strTempModel, strTempGraph:=strTempGraph, bAssignToIsPrefix:=bAssignToIsPrefix, bAssignToColumnWithoutNames:=bAssignToColumnWithoutNames, bInsertColumnBefore:=bInsertColumnBefore, bRequireCorrectLength:=bRequireCorrectLength, strAdjacentColumn:=strAdjacentColumn) End If End Sub diff --git a/instat/dlgCalculator.vb b/instat/dlgCalculator.vb index f8f545aa919..b5ba0bb412b 100644 --- a/instat/dlgCalculator.vb +++ b/instat/dlgCalculator.vb @@ -102,7 +102,7 @@ Public Class dlgCalculator Private Sub SaveResults() If ucrCalc.ucrSaveResultInto.IsComplete Then - ucrBase.clsRsyntax.SetAssignTo(ucrCalc.ucrSaveResultInto.GetText(), strTempColumn:=ucrCalc.ucrSaveResultInto.GetText(), strTempDataframe:=ucrCalc.ucrSelectorForCalculations.ucrAvailableDataFrames.cboAvailableDataFrames.Text) + ucrBase.clsRsyntax.SetAssignTo(ucrCalc.ucrSaveResultInto.GetText(), strTempColumn:=ucrCalc.ucrSaveResultInto.GetText(), strTempDataframe:=ucrCalc.ucrSelectorForCalculations.ucrAvailableDataFrames.cboAvailableDataFrames.Text, bAssignToIsPrefix:=ucrBase.clsRsyntax.clsBaseCommandString.bAssignToIsPrefix, bAssignToColumnWithoutNames:=ucrBase.clsRsyntax.clsBaseCommandString.bAssignToColumnWithoutNames, bInsertColumnBefore:=ucrBase.clsRsyntax.clsBaseCommandString.bInsertColumnBefore, bRequireCorrectLength:=ucrBase.clsRsyntax.clsBaseCommandString.bRequireCorrectLength) ucrBase.clsRsyntax.bExcludeAssignedFunctionOutput = True ucrBase.clsRsyntax.iCallType = 0 Else diff --git a/instat/static/InstatObject/R/Backend_Components/calculations.R b/instat/static/InstatObject/R/Backend_Components/calculations.R index d2cef35718a..a7b92b34295 100644 --- a/instat/static/InstatObject/R/Backend_Components/calculations.R +++ b/instat/static/InstatObject/R/Backend_Components/calculations.R @@ -699,8 +699,8 @@ DataBook$set("public", "save_calc_output", function(calc, curr_data_list, previo stop("Cannot save output from this calculation because the data frame does not have any defined keys.") } } - else { - # If no summary or join, then simply add result as new column + else { + # If no summary or join, then simply add result as new column # Because no join was required, the rows should match 1-1 in both data frames self$add_columns_to_data(data_name = calc_from_data_name, col_name = calc$result_name, col_data = curr_data_list[[c_data_label]][[calc$result_name]]) to_data_name <- calc_from_data_name diff --git a/instat/static/InstatObject/R/data_object_R6.R b/instat/static/InstatObject/R/data_object_R6.R index ed7a9caa802..fe2646e32fd 100644 --- a/instat/static/InstatObject/R/data_object_R6.R +++ b/instat/static/InstatObject/R/data_object_R6.R @@ -493,7 +493,7 @@ DataSheet$set("public", "get_calculation_names", function(as_list = FALSE, exclu } ) -DataSheet$set("public", "add_columns_to_data", function(col_name = "", col_data, use_col_name_as_prefix = FALSE, hidden = FALSE, before, adjacent_column, num_cols, require_correct_length = TRUE) { +DataSheet$set("public", "add_columns_to_data", function(col_name = "", col_data, use_col_name_as_prefix = FALSE, hidden = FALSE, before, adjacent_column, num_cols, require_correct_length = TRUE, keep_existing_position = TRUE) { # Column name must be character if(!is.character(col_name)) stop("Column name must be of type: character") if(missing(num_cols)) { @@ -529,7 +529,7 @@ DataSheet$set("public", "add_columns_to_data", function(col_name = "", col_data, use_col_name_as_prefix = TRUE } - replaced = FALSE + replaced <- FALSE previous_length = self$get_column_count() if(!missing(adjacent_column) && !adjacent_column %in% self$get_column_names()) stop(adjacent_column, "not found in the data") @@ -551,34 +551,35 @@ DataSheet$set("public", "add_columns_to_data", function(col_name = "", col_data, if(curr_col_name %in% self$get_column_names()) { message(paste("A column named", curr_col_name, "already exists. The column will be replaced in the data")) self$append_to_changes(list(Replaced_col, curr_col_name)) - replaced = TRUE + replaced <- TRUE } else self$append_to_changes(list(Added_col, curr_col_name)) private$data[[curr_col_name]] <- curr_col self$data_changed <- TRUE } self$add_defaults_variables_metadata(new_col_names) - - #no need to reorder columns if before and adjacent_column column positioning paramaters are missing - if(missing(before) && missing(adjacent_column)) return() - - #get the adjacent position to be used in appending the new column names - if(before){ - if(missing(adjacent_column)) adjacent_position = 0 - else adjacent_position = which(self$get_column_names() == adjacent_column) - 1 - }else{ - if(missing(adjacent_column)) adjacent_position = self$get_column_count() - else adjacent_position = which(self$get_column_names() == adjacent_column) + + # If replacing existing columns and not repositioning them, or before and adjacent_column column positioning parameters are missing + # then do not reposition. + if((replaced && keep_existing_position) || (missing(before) && missing(adjacent_column))) return() + + # Get the adjacent position to be used in appending the new column names + if(before) { + if(missing(adjacent_column)) adjacent_position <- 0 + else adjacent_position <- which(self$get_column_names() == adjacent_column) - 1 + } else { + if(missing(adjacent_column)) adjacent_position <- self$get_column_count() + else adjacent_position <- which(self$get_column_names() == adjacent_column) } - #replace existing names with empty placeholders. Maintains the indices + # Replace existing names with empty placeholders. Maintains the indices temp_all_col_names <- replace(self$get_column_names(), self$get_column_names() %in% new_col_names, "") - #append the newly added column names after the set position + # Append the newly added column names after the set position new_col_names_order <- append(temp_all_col_names, new_col_names, adjacent_position) - #remove all empty characters placeholders to get final reordered column names - new_col_names_order <- new_col_names_order[! new_col_names_order %in% c("")] - #only do reordering if the column names order differ - if( !all( self$get_column_names() == new_col_names_order) ) self$reorder_columns_in_data(col_order=new_col_names_order) + # Remove all empty characters placeholders to get final reordered column names + new_col_names_order <- new_col_names_order[! new_col_names_order == ""] + # Only do reordering if the column names order differ + if(!all(self$get_column_names() == new_col_names_order)) self$reorder_columns_in_data(col_order=new_col_names_order) } ) diff --git a/instat/static/InstatObject/R/instat_object_R6.R b/instat/static/InstatObject/R/instat_object_R6.R index 2aa59349ddf..4d2ce61965a 100644 --- a/instat/static/InstatObject/R/instat_object_R6.R +++ b/instat/static/InstatObject/R/instat_object_R6.R @@ -478,9 +478,9 @@ DataBook$set("public", "set_metadata_changed", function(data_name = "", new_val) } ) -DataBook$set("public", "add_columns_to_data", function(data_name, col_name = "", col_data, use_col_name_as_prefix = FALSE, hidden = FALSE, before = FALSE, adjacent_column, num_cols, require_correct_length = TRUE) { - if(missing(use_col_name_as_prefix)) self$get_data_objects(data_name)$add_columns_to_data(col_name, col_data, hidden = hidden, before = before, adjacent_column = adjacent_column, num_cols = num_cols, require_correct_length = require_correct_length) - else self$get_data_objects(data_name)$add_columns_to_data(col_name, col_data, use_col_name_as_prefix = use_col_name_as_prefix, hidden = hidden, before = before, adjacent_column = adjacent_column, num_cols = num_cols, require_correct_length = require_correct_length) +DataBook$set("public", "add_columns_to_data", function(data_name, col_name = "", col_data, use_col_name_as_prefix = FALSE, hidden = FALSE, before, adjacent_column, num_cols, require_correct_length = TRUE, keep_existing_position = TRUE) { + if(missing(use_col_name_as_prefix)) self$get_data_objects(data_name)$add_columns_to_data(col_name, col_data, hidden = hidden, before = before, adjacent_column = adjacent_column, num_cols = num_cols, require_correct_length = require_correct_length, keep_existing_position = keep_existing_position) + else self$get_data_objects(data_name)$add_columns_to_data(col_name, col_data, use_col_name_as_prefix = use_col_name_as_prefix, hidden = hidden, before = before, adjacent_column = adjacent_column, num_cols = num_cols, require_correct_length = require_correct_length, keep_existing_position = keep_existing_position) } ) From 827e646e3ec1f007d82857b90e9495cb78bafc7d Mon Sep 17 00:00:00 2001 From: Danny Parsons Date: Wed, 11 Nov 2020 09:15:05 +0000 Subject: [PATCH 2/2] corrections from review --- instat/clsRCodeStructure.vb | 1 + instat/clsRSyntax.vb | 3 +-- instat/static/InstatObject/R/instat_object_R6.R | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/instat/clsRCodeStructure.vb b/instat/clsRCodeStructure.vb index 032655868aa..29303529dd4 100644 --- a/instat/clsRCodeStructure.vb +++ b/instat/clsRCodeStructure.vb @@ -249,6 +249,7 @@ Public Class RCodeStructure ''' (Optional) Optional R character vector to give ''' names of new data frames if data frame list is ''' not named. + ''' (Optional) The new value for strAdjacentColumn. '''-------------------------------------------------------------------------------------------- Public Sub SetAssignTo(strTemp As String, Optional strTempDataframe As String = "", Optional strTempColumn As String = "", Optional strTempModel As String = "", Optional strTempGraph As String = "", Optional strTempSurv As String = "", Optional strTempTable As String = "", Optional bAssignToIsPrefix As Boolean = False, Optional bAssignToColumnWithoutNames As Boolean = False, Optional bInsertColumnBefore As Boolean = False, Optional bRequireCorrectLength As Boolean = True, Optional bDataFrameList As Boolean = False, Optional strDataFrameNames As String = "", Optional strAdjacentColumn As String = "") strAssignTo = strTemp diff --git a/instat/clsRSyntax.vb b/instat/clsRSyntax.vb index 2f590788fa3..b3492d3c162 100644 --- a/instat/clsRSyntax.vb +++ b/instat/clsRSyntax.vb @@ -214,8 +214,7 @@ Public Class RSyntax Public Sub SetAssignTo(strAssignToName As String, Optional strTempDataframe As String = "", Optional strTempColumn As String = "", Optional strTempModel As String = "", Optional strTempGraph As String = "", Optional bAssignToIsPrefix As Boolean = False, Optional bAssignToColumnWithoutNames As Boolean = False, Optional bInsertColumnBefore As Boolean = False, Optional bRequireCorrectLength As Boolean = True, Optional strAdjacentColumn As String = "") If bUseBaseOperator Then clsBaseOperator.SetAssignTo(strTemp:=strAssignToName, strTempDataframe:=strTempDataframe, strTempColumn:=strTempColumn, strTempModel:=strTempModel, strTempGraph:=strTempGraph, bAssignToIsPrefix:=bAssignToIsPrefix, bAssignToColumnWithoutNames:=bAssignToColumnWithoutNames, bInsertColumnBefore:=bInsertColumnBefore, bRequireCorrectLength:=bRequireCorrectLength, strAdjacentColumn:=strAdjacentColumn) - End If - If bUseBaseFunction Then 'TODO SJL 07/04/20 Use ElseIf? + ElseIf bUseBaseFunction Then clsBaseFunction.SetAssignTo(strAssignToName, strTempDataframe:=strTempDataframe, strTempColumn:=strTempColumn, strTempModel:=strTempModel, strTempGraph:=strTempGraph, bAssignToIsPrefix:=bAssignToIsPrefix, bAssignToColumnWithoutNames:=bAssignToColumnWithoutNames, bInsertColumnBefore:=bInsertColumnBefore, bRequireCorrectLength:=bRequireCorrectLength, strAdjacentColumn:=strAdjacentColumn) ElseIf bUseCommandString Then clsBaseCommandString.SetAssignTo(strAssignToName, strTempDataframe:=strTempDataframe, strTempColumn:=strTempColumn, strTempModel:=strTempModel, strTempGraph:=strTempGraph, bAssignToIsPrefix:=bAssignToIsPrefix, bAssignToColumnWithoutNames:=bAssignToColumnWithoutNames, bInsertColumnBefore:=bInsertColumnBefore, bRequireCorrectLength:=bRequireCorrectLength, strAdjacentColumn:=strAdjacentColumn) diff --git a/instat/static/InstatObject/R/instat_object_R6.R b/instat/static/InstatObject/R/instat_object_R6.R index 4d2ce61965a..1d1b4ddd5d7 100644 --- a/instat/static/InstatObject/R/instat_object_R6.R +++ b/instat/static/InstatObject/R/instat_object_R6.R @@ -479,8 +479,7 @@ DataBook$set("public", "set_metadata_changed", function(data_name = "", new_val) ) DataBook$set("public", "add_columns_to_data", function(data_name, col_name = "", col_data, use_col_name_as_prefix = FALSE, hidden = FALSE, before, adjacent_column, num_cols, require_correct_length = TRUE, keep_existing_position = TRUE) { - if(missing(use_col_name_as_prefix)) self$get_data_objects(data_name)$add_columns_to_data(col_name, col_data, hidden = hidden, before = before, adjacent_column = adjacent_column, num_cols = num_cols, require_correct_length = require_correct_length, keep_existing_position = keep_existing_position) - else self$get_data_objects(data_name)$add_columns_to_data(col_name, col_data, use_col_name_as_prefix = use_col_name_as_prefix, hidden = hidden, before = before, adjacent_column = adjacent_column, num_cols = num_cols, require_correct_length = require_correct_length, keep_existing_position = keep_existing_position) + self$get_data_objects(data_name)$add_columns_to_data(col_name, col_data, use_col_name_as_prefix = use_col_name_as_prefix, hidden = hidden, before = before, adjacent_column = adjacent_column, num_cols = num_cols, require_correct_length = require_correct_length, keep_existing_position = keep_existing_position) } )