diff --git a/instat/clsRLink.vb b/instat/clsRLink.vb index 4b8323f856e..c966d9c3108 100644 --- a/instat/clsRLink.vb +++ b/instat/clsRLink.vb @@ -1080,6 +1080,22 @@ Public Class RLink Return strColumn End Function + Public Function GetCorruptionComponentsColumnNames(strDataName As String) As String() + Dim clsGetComponents As New RFunction + Dim strColumn() As String + Dim expColumn As SymbolicExpression + + clsGetComponents.SetRCommand(strInstatDataObject & "$get_CRI_component_column_names") + clsGetComponents.AddParameter("data_name", Chr(34) & strDataName & Chr(34)) + expColumn = RunInternalScriptGetValue(clsGetComponents.ToScript(), bSilent:=True) + If expColumn IsNot Nothing AndAlso Not expColumn.Type = Internals.SymbolicExpressionType.Null Then + strColumn = expColumn.AsCharacter.ToArray + Else + strColumn = Nothing + End If + Return strColumn + End Function + Public Function GetClimaticColumnOfType(strDataName As String, strType As String) As String Dim clsGetColumnName As New RFunction Dim strColumn As String diff --git a/instat/dlgCorrelation.vb b/instat/dlgCorrelation.vb index 4a97da8589d..a4fec9a7bd1 100644 --- a/instat/dlgCorrelation.vb +++ b/instat/dlgCorrelation.vb @@ -21,6 +21,9 @@ Public Class dlgCorrelation Private clsCorrelationTestFunction, clsRGGcorrGraphicsFunction, clsRGraphicsFuction, clsRGGscatMatrixFunction, clsCorrelationFunction, clsRTempFunction, clsTempFunc As New RFunction Private clsColFunction As String Private bResetSubdialog As Boolean = False + Public strDefaultDataFrame As String = "" + Public strDefaultColumns() As String = Nothing + Private Sub dlgCorrelation_Load(sender As Object, e As EventArgs) Handles MyBase.Load If bFirstload Then InitialiseDialog() @@ -33,6 +36,7 @@ Public Class dlgCorrelation bReset = False autoTranslate(Me) ReopenDialog() + SetDefaultColumn() TestOKEnabled() End Sub @@ -176,6 +180,20 @@ Public Class dlgCorrelation ucrSaveModel.SetRCode(clsCorrelationFunction, bReset) End Sub + Private Sub SetDefaultColumn() + If strDefaultDataFrame <> "" Then + ucrSelectorCorrelation.SetDataframe(strDefaultDataFrame) + rdoMultipleColumns.Checked = True + End If + If strDefaultColumns IsNot Nothing AndAlso strDefaultColumns.Count > 0 Then + For Each strVar As String In strDefaultColumns + ucrReceiverMultipleColumns.Add(strVar, strDefaultDataFrame) + Next + End If + strDefaultDataFrame = "" + strDefaultColumns = Nothing + End Sub + Public Sub TestOKEnabled() If rdoTwoColumns.Checked Then ucrBase.clsRsyntax.RemoveAssignTo() diff --git a/instat/frmMain.Designer.vb b/instat/frmMain.Designer.vb index cad5ad94acf..609adc92f8a 100644 --- a/instat/frmMain.Designer.vb +++ b/instat/frmMain.Designer.vb @@ -224,6 +224,8 @@ Partial Class frmMain Me.mnuClimaticPICSARainfall = New System.Windows.Forms.ToolStripMenuItem() Me.mnuClimaticPICSATemperature = New System.Windows.Forms.ToolStripMenuItem() Me.mnuClimaticPICSACrops = New System.Windows.Forms.ToolStripMenuItem() + Me.mnuCMSAF = New System.Windows.Forms.ToolStripMenuItem() + Me.PlToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem() Me.mnuClimaticModels = New System.Windows.Forms.ToolStripMenuItem() Me.mnuClimaticModelsExtremes = New System.Windows.Forms.ToolStripMenuItem() Me.mnuClimaticModelsCircular = New System.Windows.Forms.ToolStripMenuItem() @@ -459,6 +461,7 @@ Partial Class frmMain Me.mnuProcurementModel = New System.Windows.Forms.ToolStripMenuItem() Me.mnuProcurementModelFitModelToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem() Me.DefineRedFlagsToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem() + Me.CorrelationsRedFlagsorOthersToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem() Me.CalculateCRIToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem() Me.ProcurementUseCRI = New System.Windows.Forms.ToolStripMenuItem() Me.mnuProcurementUseCRISummariseCRIbyCountry = New System.Windows.Forms.ToolStripMenuItem() @@ -483,8 +486,6 @@ Partial Class frmMain Me.splDataOutput = New System.Windows.Forms.SplitContainer() Me.ucrDataViewer = New instat.ucrDataView() Me.ucrOutput = New instat.ucrOutputWindow() - Me.mnuCMSAF = New System.Windows.Forms.ToolStripMenuItem() - Me.PlToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem() Me.stsStrip.SuspendLayout() Me.Tool_strip.SuspendLayout() Me.mnuBar.SuspendLayout() @@ -1765,21 +1766,34 @@ Partial Class frmMain 'mnuClimaticPICSARainfall ' Me.mnuClimaticPICSARainfall.Name = "mnuClimaticPICSARainfall" - Me.mnuClimaticPICSARainfall.Size = New System.Drawing.Size(152, 22) + Me.mnuClimaticPICSARainfall.Size = New System.Drawing.Size(150, 22) Me.mnuClimaticPICSARainfall.Text = "Rainfall..." ' 'mnuClimaticPICSATemperature ' Me.mnuClimaticPICSATemperature.Name = "mnuClimaticPICSATemperature" - Me.mnuClimaticPICSATemperature.Size = New System.Drawing.Size(152, 22) + Me.mnuClimaticPICSATemperature.Size = New System.Drawing.Size(150, 22) Me.mnuClimaticPICSATemperature.Text = "Temperature..." ' 'mnuClimaticPICSACrops ' Me.mnuClimaticPICSACrops.Name = "mnuClimaticPICSACrops" - Me.mnuClimaticPICSACrops.Size = New System.Drawing.Size(152, 22) + Me.mnuClimaticPICSACrops.Size = New System.Drawing.Size(150, 22) Me.mnuClimaticPICSACrops.Text = "Crops..." ' + 'mnuCMSAF + ' + Me.mnuCMSAF.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.PlToolStripMenuItem}) + Me.mnuCMSAF.Name = "mnuCMSAF" + Me.mnuCMSAF.Size = New System.Drawing.Size(212, 22) + Me.mnuCMSAF.Text = "CM SAF" + ' + 'PlToolStripMenuItem + ' + Me.PlToolStripMenuItem.Name = "PlToolStripMenuItem" + Me.PlToolStripMenuItem.Size = New System.Drawing.Size(144, 22) + Me.PlToolStripMenuItem.Text = "Plot Region..." + ' 'mnuClimaticModels ' Me.mnuClimaticModels.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.mnuClimaticModelsExtremes, Me.mnuClimaticModelsCircular, Me.mnuClimaticModelsMarkovModelling}) @@ -3339,7 +3353,7 @@ Partial Class frmMain ' 'mnuProcurement ' - Me.mnuProcurement.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.mnuCorruptionDefineCorruptionData, Me.mnuProcurementPrepare, Me.mnuProcurementDescribe, Me.DefineOutputsToolStripMenuItem, Me.mnuProcurementModel, Me.DefineRedFlagsToolStripMenuItem, Me.CalculateCRIToolStripMenuItem, Me.ProcurementUseCRI}) + Me.mnuProcurement.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.mnuCorruptionDefineCorruptionData, Me.mnuProcurementPrepare, Me.mnuProcurementDescribe, Me.DefineOutputsToolStripMenuItem, Me.mnuProcurementModel, Me.DefineRedFlagsToolStripMenuItem, Me.CorrelationsRedFlagsorOthersToolStripMenuItem, Me.CalculateCRIToolStripMenuItem, Me.ProcurementUseCRI}) Me.mnuProcurement.Name = "mnuProcurement" Me.mnuProcurement.Size = New System.Drawing.Size(88, 20) Me.mnuProcurement.Text = "Procurement" @@ -3443,6 +3457,12 @@ Partial Class frmMain Me.DefineRedFlagsToolStripMenuItem.Size = New System.Drawing.Size(292, 22) Me.DefineRedFlagsToolStripMenuItem.Text = "Define Red Flag Variables..." ' + 'CorrelationsRedFlagsorOthersToolStripMenuItem + ' + Me.CorrelationsRedFlagsorOthersToolStripMenuItem.Name = "CorrelationsRedFlagsorOthersToolStripMenuItem" + Me.CorrelationsRedFlagsorOthersToolStripMenuItem.Size = New System.Drawing.Size(292, 22) + Me.CorrelationsRedFlagsorOthersToolStripMenuItem.Text = "Correlations (Red Flags or others)..." + ' 'CalculateCRIToolStripMenuItem ' Me.CalculateCRIToolStripMenuItem.Name = "CalculateCRIToolStripMenuItem" @@ -3696,19 +3716,6 @@ Partial Class frmMain Me.ucrOutput.Size = New System.Drawing.Size(444, 231) Me.ucrOutput.TabIndex = 0 ' - 'mnuCMSAF - ' - Me.mnuCMSAF.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.PlToolStripMenuItem}) - Me.mnuCMSAF.Name = "mnuCMSAF" - Me.mnuCMSAF.Size = New System.Drawing.Size(212, 22) - Me.mnuCMSAF.Text = "CM SAF" - ' - 'PlToolStripMenuItem - ' - Me.PlToolStripMenuItem.Name = "PlToolStripMenuItem" - Me.PlToolStripMenuItem.Size = New System.Drawing.Size(152, 22) - Me.PlToolStripMenuItem.Text = "Plot Region..." - ' 'frmMain ' Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) @@ -4202,4 +4209,5 @@ Partial Class frmMain Friend WithEvents BoxplotsConceptualisedAndConstructedToolStripMenuItem As ToolStripMenuItem Friend WithEvents mnuCMSAF As ToolStripMenuItem Friend WithEvents PlToolStripMenuItem As ToolStripMenuItem + Friend WithEvents CorrelationsRedFlagsorOthersToolStripMenuItem As ToolStripMenuItem End Class diff --git a/instat/frmMain.vb b/instat/frmMain.vb index 2bf2bcf1269..7cddbea9e95 100644 --- a/instat/frmMain.vb +++ b/instat/frmMain.vb @@ -1657,4 +1657,22 @@ Public Class frmMain Private Sub PlToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PlToolStripMenuItem.Click dlgPlotRegion.ShowDialog() End Sub + + Private Sub CorrelationsRedFlagsorOthersToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CorrelationsRedFlagsorOthersToolStripMenuItem.Click + Dim lstDataNames As List(Of String) + Dim strComponentColumnNames() As String + + lstDataNames = clsRLink.GetCorruptionContractDataFrameNames() + If lstDataNames.Count > 0 Then + dlgCorrelation.strDefaultDataFrame = lstDataNames(0) + strComponentColumnNames = clsRLink.GetCorruptionComponentsColumnNames(lstDataNames(0)) + If strComponentColumnNames IsNot Nothing AndAlso strComponentColumnNames.Count > 0 Then + dlgCorrelation.strDefaultColumns = strComponentColumnNames + End If + Else + dlgCorrelation.strDefaultDataFrame = "" + dlgCorrelation.strDefaultColumns = Nothing + End If + dlgCorrelation.ShowDialog() + End Sub End Class \ No newline at end of file diff --git a/instat/static/InstatObject/R/data_object_R6.R b/instat/static/InstatObject/R/data_object_R6.R index 23ffb65e123..4a22fc2e6af 100644 --- a/instat/static/InstatObject/R/data_object_R6.R +++ b/instat/static/InstatObject/R/data_object_R6.R @@ -1078,7 +1078,7 @@ data_object$set("public", "convert_column_to_type", function(col_names = c(), to stop("to_type must be a character of length one") } - if(!(to_type %in% c("integer", "factor", "numeric", "character", "ordered_factor"))) { + if(!(to_type %in% c("integer", "factor", "numeric", "character", "ordered_factor", "logical"))) { stop(to_type, " is not a valid type to convert to") } @@ -1146,6 +1146,11 @@ data_object$set("public", "convert_column_to_type", function(col_names = c(), to else if(to_type == "character") { new_col <- sjmisc::to_character(curr_col) } + else if(to_type == "logical") { + if(is.logical.like(curr_col)) new_col <- as.logical(curr_col) + else stop("Column is not numeric or contains values other than 0 and 1. Converting to logical would result in losing information.") + } + self$add_columns_to_data(col_name = col_name, col_data = new_col) if(keep_attr) { @@ -2557,6 +2562,20 @@ data_object$set("public","is_corruption_type_present", function(type) { } ) +instat_object$set("public","get_CRI_component_column_names", function(data_name) { + self$get_data_objects(data_name)$get_CRI_component_column_names() +} +) + +data_object$set("public","get_CRI_component_column_names", function() { + include <- list(TRUE) + names(include) <- corruption_index_label + return(self$get_column_names(include = include)) +} +) + +corruption_index_label + instat_object$set("public","get_CRI_column_names", function(data_name) { self$get_data_objects(data_name)$get_CRI_column_names() } diff --git a/instat/static/InstatObject/R/stand_alone_functions.R b/instat/static/InstatObject/R/stand_alone_functions.R index b81f84f3513..bee2ee43049 100644 --- a/instat/static/InstatObject/R/stand_alone_functions.R +++ b/instat/static/InstatObject/R/stand_alone_functions.R @@ -650,6 +650,12 @@ import_from_iri <- function(download_from, data_file, path, X1, X2,Y1,Y2, get_ar return(list(dataout,lat_lon_dataframe)) } +is.logical.like <- function(x) { + if(is.logical(x)) return(TRUE) + else if(is.numeric(x)) return(all(na.omit(x) %in% c(1,0))) + else return(FALSE) +} + is.binary <- function(x) { if(is.logical(x)) return(TRUE) else if(is.numeric(x)) return(all(na.omit(x) %in% c(1,0)))