Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensured that Default Column and Variable Name checkbox function correctly in New DataFrame dialog #8148

169 changes: 90 additions & 79 deletions instat/dlgNewDataFrame.vb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Public Class dlgNewDataFrame
clsRepFunction, clsCorporaFunction, clsListDfFunction As New RFunction
Public bFirstLoad As Boolean = True
Private bReset As Boolean = True

Private bLastRow As Boolean = False
Private arrAvailableCategories() As String
Private arrAvailableLists() As String

Expand Down Expand Up @@ -87,7 +87,8 @@ Public Class dlgNewDataFrame

ucrChkVariable.SetText("Variable Name")
ucrChkVariable.SetParameter(New RParameter("var", 0))
ucrChkVariable.SetValuesCheckedAndUnchecked("TRUE", "FALSE")
ucrChkVariable.AddParameterValuesCondition(True, "variablename", "True")
ucrChkVariable.AddParameterValuesCondition(False, "variablename", "False")

ucrChkIncludeLabel.SetText("Variable Label")
ucrChkIncludeLabel.SetParameter(New RParameter("label", 0))
Expand Down Expand Up @@ -118,6 +119,7 @@ Public Class dlgNewDataFrame
'reset the controls
ucrNewDFName.Reset()
ucrTryNewDataFrame.SetRSyntax(ucrBase.clsRsyntax)
clsDummyVarFunction.AddParameter("variablename", "True", iPosition:=0)

clsGetCategoriesFunction.SetPackageName("rcorpora")
clsGetCategoriesFunction.SetRCommand("categories")
Expand Down Expand Up @@ -368,77 +370,79 @@ Public Class dlgNewDataFrame
Dim lstLabels As New List(Of String)
Dim iColPosition As Integer = 0
clsNewDataFrameFunction.ClearParameters()
For Each row As DataGridViewRow In dataTypeGridView.Rows
'labels column is optional, so check for empty if it exists
Dim clsColExpRFunction As New RFunction
Dim clsEmptyRepFunction As New RFunction
Dim iRows As Integer = ucrNudRows.Value
Dim strType As String = row.Cells("cbType").Value
If strType IsNot Nothing Then
Select Case strType
Case "Character"
clsColExpRFunction.SetRCommand("as.character")
Case "Numeric"
clsColExpRFunction.SetRCommand("as.numeric")
Case "Factor"
clsColExpRFunction.SetRCommand("factor")
Dim strLevels As String = row.Cells("colLevels").Value
If strLevels <> "" Then
If strLevels.Count(Function(x) x = ":") = 1 Then
If IsNumeric(strLevels.Split(":")(0)) _
AndAlso IsNumeric(strLevels.Split(":")(1)) Then
clsColExpRFunction.AddParameter("levels", strLevels, iPosition:=1)
If bLastRow Then
For Each row As DataGridViewRow In dataTypeGridView.Rows
'labels column is optional, so check for empty if it exists
Dim clsColExpRFunction As New RFunction
Dim clsEmptyRepFunction As New RFunction
Dim iRows As Integer = ucrNudRows.Value
Dim strType As String = row.Cells("cbType").Value
If strType IsNot Nothing Then
Select Case strType
Case "Character"
clsColExpRFunction.SetRCommand("as.character")
Case "Numeric"
clsColExpRFunction.SetRCommand("as.numeric")
Case "Factor"
clsColExpRFunction.SetRCommand("factor")
Dim strLevels As String = row.Cells("colLevels").Value
If strLevels <> "" Then
If strLevels.Count(Function(x) x = ":") = 1 Then
If IsNumeric(strLevels.Split(":")(0)) _
AndAlso IsNumeric(strLevels.Split(":")(1)) Then
clsColExpRFunction.AddParameter("levels", strLevels, iPosition:=1)
End If
Else
clsColExpRFunction.AddParameter("levels", GetLevelsAsRString(strLevels), iPosition:=1)
End If
Else
clsColExpRFunction.AddParameter("levels", GetLevelsAsRString(strLevels), iPosition:=1)
End If
End If
Case "Integer"
clsColExpRFunction.SetRCommand("as.integer")
Case Else
MsgBox("Developer error: Only expected one predefined item to set the column type.")
End Select
End If
Case "Integer"
clsColExpRFunction.SetRCommand("as.integer")
Case Else
MsgBox("Developer error: Only expected one predefined item to set the column type.")
End Select
End If

lstLabels.Add(row.Cells("colLabel").Value)
clsSjLabelledFunction.AddParameter("label", GetLabelAsRString(lstLabels), iPosition:=1)
lstLabels.Add(row.Cells("colLabel").Value)
clsSjLabelledFunction.AddParameter("label", GetLabelAsRString(lstLabels), iPosition:=1)

clsEmptyRepFunction.SetRCommand("rep")
clsEmptyRepFunction.AddParameter("times", iRows, bIncludeArgumentName:=False, iPosition:=1)
clsEmptyRepFunction.RemoveParameterByName("each")
clsEmptyRepFunction.RemoveParameterByName("length.out")
clsEmptyRepFunction.SetRCommand("rep")
clsEmptyRepFunction.AddParameter("times", iRows, bIncludeArgumentName:=False, iPosition:=1)
clsEmptyRepFunction.RemoveParameterByName("each")
clsEmptyRepFunction.RemoveParameterByName("length.out")

Dim strDefault As String = row.Cells("colDefault").Value
If strDefault IsNot Nothing Then
If strDefault = "NA" Then
clsEmptyRepFunction.AddParameter("x", "NA", bIncludeArgumentName:=False, iPosition:=0)
ElseIf IsNumeric(strDefault) Then
clsEmptyRepFunction.AddParameter("x", strDefault, bIncludeArgumentName:=False, iPosition:=0)
Else
clsEmptyRepFunction.AddParameter("x", Chr(34) & strDefault & Chr(34), bIncludeArgumentName:=False, iPosition:=0)
End If
Dim strDefault As String = row.Cells("colDefault").Value
If strDefault IsNot Nothing Then
If strDefault = "NA" Then
clsEmptyRepFunction.AddParameter("x", "NA", bIncludeArgumentName:=False, iPosition:=0)
ElseIf IsNumeric(strDefault) Then
clsEmptyRepFunction.AddParameter("x", strDefault, bIncludeArgumentName:=False, iPosition:=0)
Else
clsEmptyRepFunction.AddParameter("x", Chr(34) & strDefault & Chr(34), bIncludeArgumentName:=False, iPosition:=0)
End If

Dim strColumnName As String = frmMain.clsRLink.MakeValidText(row.Cells("colNames").Value)
If (strType = "Integer" OrElse strType = "Numeric") AndAlso Not strDefault = "NA" _
AndAlso IsNumeric(strDefault) AndAlso strDefault.Contains(",") Then

Dim clsSeqFunction As New RFunction
clsSeqFunction.SetRCommand("seq")
clsSeqFunction.AddParameter("from", strDefault, bIncludeArgumentName:=False, iPosition:=0)
clsEmptyRepFunction.RemoveParameterByName("times")
clsEmptyRepFunction.AddParameter("x", clsRFunctionParameter:=clsSeqFunction, bIncludeArgumentName:=False, iPosition:=0)
clsEmptyRepFunction.AddParameter("each", "1", iPosition:=1)
clsEmptyRepFunction.AddParameter("length.out", iRows, iPosition:=2)
clsColExpRFunction.AddParameter("x", clsRFunctionParameter:=clsEmptyRepFunction, bIncludeArgumentName:=False, iPosition:=0)
ElseIf strDefault.Contains("LETTERS") OrElse strDefault.Contains("letters") Then
clsColExpRFunction.AddParameter("x", strDefault, bIncludeArgumentName:=False, iPosition:=0)
Else
clsColExpRFunction.AddParameter("x", clsRFunctionParameter:=clsEmptyRepFunction, bIncludeArgumentName:=False, iPosition:=0)
Dim strColumnName As String = frmMain.clsRLink.MakeValidText(row.Cells("colNames").Value)
If (strType = "Integer" OrElse strType = "Numeric") AndAlso Not strDefault = "NA" _
AndAlso IsNumeric(strDefault) AndAlso strDefault.Contains(",") Then

Dim clsSeqFunction As New RFunction
clsSeqFunction.SetRCommand("seq")
clsSeqFunction.AddParameter("from", strDefault, bIncludeArgumentName:=False, iPosition:=0)
clsEmptyRepFunction.RemoveParameterByName("times")
clsEmptyRepFunction.AddParameter("x", clsRFunctionParameter:=clsSeqFunction, bIncludeArgumentName:=False, iPosition:=0)
clsEmptyRepFunction.AddParameter("each", "1", iPosition:=1)
clsEmptyRepFunction.AddParameter("length.out", iRows, iPosition:=2)
clsColExpRFunction.AddParameter("x", clsRFunctionParameter:=clsEmptyRepFunction, bIncludeArgumentName:=False, iPosition:=0)
ElseIf strDefault.Contains("LETTERS") OrElse strDefault.Contains("letters") Then
clsColExpRFunction.AddParameter("x", strDefault, bIncludeArgumentName:=False, iPosition:=0)
Else
clsColExpRFunction.AddParameter("x", clsRFunctionParameter:=clsEmptyRepFunction, bIncludeArgumentName:=False, iPosition:=0)
End If
clsNewDataFrameFunction.AddParameter(strColumnName, clsRFunctionParameter:=clsColExpRFunction, iPosition:=iColPosition)
iColPosition += 1
End If
clsNewDataFrameFunction.AddParameter(strColumnName, clsRFunctionParameter:=clsColExpRFunction, iPosition:=iColPosition)
iColPosition += 1
End If
Next
Next
End If
End Sub

Private Sub CreateEmptyDataFrame(iCol As Integer)
Expand Down Expand Up @@ -489,7 +493,11 @@ Public Class dlgNewDataFrame
End Sub

Private Sub dataTypeGridView_ValueChanged(sender As Object, e As EventArgs) Handles dataTypeGridView.CellValueChanged
SampleEmpty()
If bLastRow Then
SampleEmpty()
bLastRow = False
End If

End Sub

Private Sub DataGridView_DataError(ByVal sender As Object, ByVal e As DataGridViewDataErrorEventArgs) Handles dataTypeGridView.DataError
Expand All @@ -501,21 +509,21 @@ Public Class dlgNewDataFrame

Private Sub FillGrid(iRow As Integer, dgrView As DataGridView, bInsert As Boolean)
If bInsert Then
With dgrView.Rows
.Item(iRow).Cells(0).Value = iRow + 1
.Item(iRow).Cells(1).Value = "x" & (iRow + 1)
.Item(iRow).Cells(2).Value = "Character"
.Item(iRow).Cells(3).Value = "NA"
.Item(iRow).Cells(4).Value = ""
.Item(iRow).Cells(5).Value = ""
End With
For i As Integer = iRow To dgrView.Rows.Count - 1
Dim iRowHeader = i + 1
With dgrView.Rows
.Item(i).Cells(0).Value = iRowHeader
.Item(i).Cells(1).Value = String.Concat("x", iRowHeader)
.Item(i).Cells(2).Value = "Character"
.Item(i).Cells(3).Value = "NA"
End With
bLastRow = If(i = dgrView.Rows.Count - 1, True, False)
Next
Else
For i As Integer = 0 To dgrView.Rows.Count - 1
With dgrView.Rows
.Item(i).Cells(0).Value = i + 1
.Item(i).Cells(1).Value = "x" & (i + 1)
.Item(i).Cells(4).Value = ""
.Item(i).Cells(5).Value = ""
If i = 0 Then
.Item(i).Cells(2).Value = "Numeric"
.Item(i).Cells(3).Value = "1,1000"
Expand All @@ -535,9 +543,12 @@ Public Class dlgNewDataFrame
If iValue > iRow Then
Dim iNumRowsToInsert As Integer = iValue - iRow
dataTypeGridView.Rows.Insert(iRow, iNumRowsToInsert)
FillGrid(iValue - 1, dataTypeGridView, True)
FillGrid(iRow, dataTypeGridView, True)
ElseIf iValue < iRow Then
dataTypeGridView.Rows.RemoveAt(iRow - 1)
Dim iRange = iRow - iValue
For i As Integer = 1 To iRange
dataTypeGridView.Rows.RemoveAt(iRow - i)
Next
End If
End If
CreateEmptyDataFrame(ucrNudCols.Value)
Expand Down