Skip to content

Commit

Permalink
Merge pull request #6067 from dannyparsons/save-position
Browse files Browse the repository at this point in the history
Saving and position columns bug fixes and changes
  • Loading branch information
dannyparsons authored Nov 11, 2020
2 parents 119862b + 827e646 commit a97a824
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 35 deletions.
6 changes: 3 additions & 3 deletions instat/clsRCodeStructure.vb
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ Public Class RCodeStructure
''' <param name="strDataFrameNames"> (Optional) Optional R character vector to give
''' names of new data frames if data frame list is
''' not named. </param>
''' <param name="strAdjacentColumn"> (Optional) The new value for strAdjacentColumn. </param>
'''--------------------------------------------------------------------------------------------
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
Expand All @@ -277,10 +277,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

'''--------------------------------------------------------------------------------------------
Expand Down
11 changes: 5 additions & 6 deletions instat/clsRSyntax.vb
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,13 @@ Public Class RSyntax
''' <param name="bInsertColumnBefore"> (Optional) The new value for bInsertColumnBefore. </param>
''' <param name="bRequireCorrectLength"> (Optional) The new value for bRequireCorrectLength. </param>
'''--------------------------------------------------------------------------------------------
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)
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)
clsBaseOperator.SetAssignTo(strTemp:=strAssignToName, strTempDataframe:=strTempDataframe, strTempColumn:=strTempColumn, strTempModel:=strTempModel, strTempGraph:=strTempGraph, bAssignToIsPrefix:=bAssignToIsPrefix, bAssignToColumnWithoutNames:=bAssignToColumnWithoutNames, bInsertColumnBefore:=bInsertColumnBefore, bRequireCorrectLength:=bRequireCorrectLength, strAdjacentColumn:=strAdjacentColumn)
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)
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

Expand Down
2 changes: 1 addition & 1 deletion instat/dlgCalculator.vb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 21 additions & 20 deletions instat/static/InstatObject/R/data_object_R6.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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")

Expand All @@ -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)
}
)

Expand Down
5 changes: 2 additions & 3 deletions instat/static/InstatObject/R/instat_object_R6.R
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,8 @@ 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) {
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)
}
)

Expand Down

0 comments on commit a97a824

Please sign in to comment.