Skip to content

Commit

Permalink
Merge pull request #66 from africanmathsinitiative/master
Browse files Browse the repository at this point in the history
Updating master br
  • Loading branch information
N-thony authored Jan 30, 2023
2 parents e3ea3ed + 6e50912 commit b2200f1
Show file tree
Hide file tree
Showing 60 changed files with 3,742 additions and 1,537 deletions.
14 changes: 4 additions & 10 deletions instat/Model/Output/clsOutputLogger.vb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Public Class clsOutputLogger
''' Event to show a new output as been added
''' </summary>
''' <param name="outputElement"></param>
Public Event NewOutputAdded(outputElement As clsOutputElement)
Public Event NewOutputAdded(outputElement As clsOutputElement, bDisplayOutputInExternalViewer As Boolean)

''' <summary>
''' Event to show an output as been added to a new filtered list
Expand All @@ -70,7 +70,7 @@ Public Class clsOutputLogger
End Set
End Property

Public Sub AddOutput(strScript As String, strOutput As String, bAsFile As Boolean, bAddOutputInInternalViewer As Boolean)
Public Sub AddOutput(strScript As String, strOutput As String, bAsFile As Boolean, bDisplayOutputInExternalViewer As Boolean)
'Note this always takes the last script added as corresponding script
If String.IsNullOrWhiteSpace(strScript) Then
Throw New Exception("Cannot find script to attach output to.")
Expand Down Expand Up @@ -105,14 +105,8 @@ Public Class clsOutputLogger

_outputElements.Add(outputElement)

If bAddOutputInInternalViewer Then
'raise event for output pages
RaiseEvent NewOutputAdded(outputElement)
Else
Dim frmMaximiseOutput As New frmMaximiseOutput
frmMaximiseOutput.Show(strFileName:=strOutput)
End If

'raise event for output pages
RaiseEvent NewOutputAdded(outputElement, bDisplayOutputInExternalViewer)
End Sub

''' <summary>
Expand Down
2 changes: 1 addition & 1 deletion instat/Model/RCommand/clsPrepareFunctionsForGrids.vb
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ Public Class clsPrepareFunctionsForGrids
Else
clsReplaceValue.AddParameter("new_value", strNewValue)
End If
_RLink.RunScript(clsReplaceValue.ToScript(), strComment:="Replace Value In Data", bAddOutputInInternalViewer:=bAddOutputInInternalViewer)
_RLink.RunScript(clsReplaceValue.ToScript(), strComment:="Replace Value In Data")
End Sub

''' <summary>
Expand Down
24 changes: 14 additions & 10 deletions instat/UserControl/ucrOutputPage.vb
Original file line number Diff line number Diff line change
Expand Up @@ -128,23 +128,27 @@ Public Class ucrOutputPage
''' Add output to page
''' </summary>
''' <param name="outputElement"></param>
Public Sub AddNewOutput(outputElement As clsOutputElement)
Public Sub AddNewOutput(outputElement As clsOutputElement, Optional bDisplayOutputInExternalViewer As Boolean = False)
'add the script first. This applies to whether the output has an output or not or
'whether it's just a script output
AddNewScript(outputElement)

'then add the output of the script. If the output element is just a script, ignore it since it's already been added above
If Not String.IsNullOrEmpty(outputElement.Output) Then
Select Case outputElement.OutputType
Case OutputType.TextOutput
AddNewTextOutput(outputElement)
Case OutputType.ImageOutput
AddNewImageOutput(outputElement)
Case OutputType.HtmlOutput
AddNewHtmlOutput(outputElement)
End Select
If bDisplayOutputInExternalViewer Then
Dim frmMaximiseOutput As New frmMaximiseOutput
frmMaximiseOutput.Show(strFileName:=outputElement.Output)
Else
Select Case outputElement.OutputType
Case OutputType.TextOutput
AddNewTextOutput(outputElement)
Case OutputType.ImageOutput
AddNewImageOutput(outputElement)
Case OutputType.HtmlOutput
AddNewHtmlOutput(outputElement)
End Select
End If
End If

pnlMain.VerticalScroll.Value = pnlMain.VerticalScroll.Maximum
pnlMain.PerformLayout()
End Sub
Expand Down
4 changes: 2 additions & 2 deletions instat/UserControl/ucrOutputPages.vb
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ Public Class ucrOutputPages
AddHandler ucrMainOutputPage.RefreshContextButtons, AddressOf EnableDisableTopButtons
End Sub

Private Sub AddNewOutput(outputElement As clsOutputElement)
ucrMainOutputPage.AddNewOutput(outputElement)
Private Sub AddNewOutput(outputElement As clsOutputElement, bDisplayOutputInExternalViewer As Boolean)
ucrMainOutputPage.AddNewOutput(outputElement, bDisplayOutputInExternalViewer)
End Sub

Private Sub AddNewOutputToTab(outputElement As clsOutputElement, tabName As String)
Expand Down
5 changes: 4 additions & 1 deletion instat/UserControls/frmMaximiseOutput.vb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ Public Class frmMaximiseOutput
Return
End Select

MyBase.Show()
'todo. how else can we attach the main form to have this form be displayed infront of it (top most)
'this is an issue when this function is called after click ok of a dialog
'MyBase.Show()
MyBase.Show(frmMain)
End Sub

Private Sub mnuSave_Click(sender As Object, e As EventArgs) Handles mnuSave.Click
Expand Down
3 changes: 2 additions & 1 deletion instat/clsRCodeStructure.vb
Original file line number Diff line number Diff line change
Expand Up @@ -529,14 +529,15 @@ Public Class RCodeStructure
'set the R command and parameters for the add object R function. This is used for adding the object in the data book
'set the R command and parameters for the get object R function. This is used for viewing the object.
clsAddRObject.SetRCommand(frmMain.clsRLink.strInstatDataObject & "$add_object")
clsGetRObject.SetRCommand(frmMain.clsRLink.strInstatDataObject & "$get_object")
clsGetRObject.SetRCommand(frmMain.clsRLink.strInstatDataObject & "$get_object_data")

If Not String.IsNullOrEmpty(_strDataFrameNameToAddAssignToObject) Then
clsAddRObject.AddParameter("data_name", Chr(34) & _strDataFrameNameToAddAssignToObject & Chr(34))
clsGetRObject.AddParameter("data_name", Chr(34) & _strDataFrameNameToAddAssignToObject & Chr(34))
End If

clsGetRObject.AddParameter("object_name", Chr(34) & _strAssignToName & Chr(34))
clsGetRObject.AddParameter("as_file", "TRUE")

clsAddRObject.AddParameter("object_name", Chr(34) & _strAssignToName & Chr(34))
clsAddRObject.AddParameter("object_type_label", Chr(34) & _strAssignToObjectTypeLabel & Chr(34))
Expand Down
85 changes: 32 additions & 53 deletions instat/clsRLink.vb
Original file line number Diff line number Diff line change
Expand Up @@ -781,18 +781,14 @@ Public Class RLink
''' <paramref name="strScript"/>.</param>
''' <param name="bSilent"> if false and an exception is raised then open a message box that
''' displays the exception message.</param>
''' <param name="bAddOutputInInternalViewer"> if true and the script produces and output, the output will be added
''' in the output viewer, if false, the output will be displayed in a different viewer.
''' displays the exception message.</param>
'''--------------------------------------------------------------------------------------------
Public Sub RunScript(strScript As String,
Optional iCallType As Integer = 0,
Optional strComment As String = "",
Optional bSeparateThread As Boolean = True,
Optional bShowWaitDialogOverride As Nullable(Of Boolean) = Nothing,
Optional bUpdateGrids As Boolean = True,
Optional bSilent As Boolean = False,
Optional bAddOutputInInternalViewer As Boolean = True)
Optional bSilent As Boolean = False)

'if there is no script to run then just ignore and exit sub
If String.IsNullOrWhiteSpace(strScript) Then
Expand All @@ -804,6 +800,8 @@ Public Class RLink
Dim strScriptWithComment As String = If(String.IsNullOrEmpty(strComment), strScript, GetFormattedComment(strComment) & Environment.NewLine & strScript)

If bLogRScripts Then
'todo. adding a lot of text to the text control can raise an out of memory exception.
'change this to only display the text when the audit log is visible.
txtLog.Text = txtLog.Text & strScriptWithComment & Environment.NewLine
End If

Expand All @@ -812,43 +810,45 @@ Public Class RLink
' MsgBox("The following command cannot be run because it exceeds the character limit of 2000 characters for a command in R-Instat." & Environment.NewLine & strScript & Environment.NewLine & Environment.NewLine & "It may be possible to run the command directly in R.", MsgBoxStyle.Critical, "Cannot run command")

Try
Dim strOutput As String = ""
Dim bAsFile As Boolean = True
Dim bDisplayOutputInExternalViewer As Boolean = False

'get the last R script command. todo, this should eventually use the RScript library functions to identify the last R script command
Dim strLastScript As String = GetRunnableCommandLines(strScript).LastOrDefault
If strLastScript.Contains("get_object") OrElse strLastScript.Contains("get_last_object") OrElse strLastScript.Contains("view_object") Then
If strLastScript.StartsWith(strInstatDataObject & "$get_object_data") OrElse
strLastScript.StartsWith(strInstatDataObject & "$get_last_object_data") OrElse
strLastScript.StartsWith("view_object_data") Then

Dim strFilePathName As String = GetFileOutput(strScript, bSilent, bSeparateThread, bShowWaitDialogOverride)
If Not String.IsNullOrEmpty(strFilePathName) Then
clsOutputLogger.AddOutput(strScriptWithComment, strFilePathName, True, bAddOutputInInternalViewer)
End If
strOutput = GetFileOutput(strScript, bSilent, bSeparateThread, bShowWaitDialogOverride)
'if last function is view_object then display in external viewer (maximised)
bDisplayOutputInExternalViewer = strLastScript.Contains("view_object_data")

ElseIf strLastScript.StartsWith("print") Then
bAsFile = False
Evaluate(strScript, bSilent:=bSilent, bSeparateThread:=bSeparateThread, bShowWaitDialogOverride:=bShowWaitDialogOverride)
ElseIf iCallType = 0 Then
'if script output should be ignored. todo. deprecate this block after implementing correctly
bAsFile = False
Evaluate(strScript, bSilent:=bSilent, bSeparateThread:=bSeparateThread, bShowWaitDialogOverride:=bShowWaitDialogOverride)
clsOutputLogger.AddOutput(strScriptWithComment, "", False, bAddOutputInInternalViewer)
ElseIf iCallType = 1 OrElse iCallType = 4 Then
'todo. this is used by the calculator dialog
'todo. icall types 1 and 4 seem not to be used anywhere? remove this block?
'else if script output should be stored in a temp variable
' TODO SJL In RInstat, iCallType only seems to be 0, 2 or 3. Are icall types 1 and 4 used?

bAsFile = False
Dim strTempAssignTo As String = ".temp_val"
'TODO check this is valid syntax in all cases
' i.e. this is potentially: x <- y <- 1
Evaluate(strTempAssignTo & " <- " & strScript, bSilent:=bSilent, bSeparateThread:=bSeparateThread, bShowWaitDialogOverride:=bShowWaitDialogOverride)
Dim expTemp As RDotNet.SymbolicExpression = GetSymbol(strTempAssignTo)
If expTemp IsNot Nothing Then
Dim strOutput As String = String.Join(Environment.NewLine, expTemp.AsCharacter()) & Environment.NewLine
' if there's something to output
If strOutput IsNot Nothing AndAlso strOutput <> "" Then
clsOutputLogger.AddOutput(strScriptWithComment, strOutput, False, bAddOutputInInternalViewer)
End If
strOutput = String.Join(Environment.NewLine, expTemp.AsCharacter()) & Environment.NewLine
End If

Else
'else if script output should not be ignored or not stored as an object or variable

Dim arrRScriptLines() As String = GetRunnableCommandLines(strScript)

'if output should be stored as a variable just execute the script
If arrRScriptLines.Last().Contains("<-") Then
Evaluate(strScript, bSilent:=bSilent, bSeparateThread:=bSeparateThread, bShowWaitDialogOverride:=bShowWaitDialogOverride)
Expand All @@ -861,15 +861,14 @@ Public Class RLink
End If

If bSuccess Then
Dim strFilePathName As String = GetFileOutput("view_object(object = " & arrRScriptLines.Last() & " , object_format = 'text' )", bSilent, bSeparateThread, bShowWaitDialogOverride)
If Not String.IsNullOrEmpty(strFilePathName) Then
clsOutputLogger.AddOutput(strScriptWithComment, strFilePathName, True, bAddOutputInInternalViewer)
End If
strOutput = GetFileOutput("view_object_data(object = " & arrRScriptLines.Last() & " , object_format = 'text' )", bSilent, bSeparateThread, bShowWaitDialogOverride)
End If
End If

End If

'log script and output
clsOutputLogger.AddOutput(strScriptWithComment, strOutput, bAsFile, bDisplayOutputInExternalViewer)
Catch e As Exception
MsgBox(e.Message & Environment.NewLine & "The error occurred in attempting to run the following R command(s):" & Environment.NewLine & strScript, MsgBoxStyle.Critical, "Error running R command(s)")
End Try
Expand Down Expand Up @@ -982,27 +981,6 @@ Public Class RLink
Return strScriptCmd
End Function


'''--------------------------------------------------------------------------------------------
''' <summary> View last graph. </summary>
'''
''' <param name="bAsPlotly"> (Optional) If true then view last graph as plotly. </param>
'''--------------------------------------------------------------------------------------------
Public Sub ViewLastGraph(bAsPlotly As String)
Dim clsLastGraph As New RFunction
clsLastGraph.SetRCommand(strInstatDataObject & "$get_last_graph")
clsLastGraph.AddParameter("print_graph", "FALSE", iPosition:=0)

Dim strGlobalGraphDisplayOption As String
'store the current set graph display option, to restore after display
strGlobalGraphDisplayOption = Me.strGraphDisplayOption
Me.strGraphDisplayOption = "view_R_viewer"
clsLastGraph.AddParameter("print_graph", "TRUE", iPosition:=0)
RunScript(clsLastGraph.ToScript(), iCallType:=3, bAddOutputInInternalViewer:=False, strComment:="View last graph", bSeparateThread:=False)
'restore the graph display option
Me.strGraphDisplayOption = strGlobalGraphDisplayOption
End Sub

'''--------------------------------------------------------------------------------------------
''' <summary> Executes the the <paramref name="strScript"/> R script and returns the result
''' as a 'SymbolicExpression' object. </summary>
Expand Down Expand Up @@ -1430,14 +1408,17 @@ Public Class RLink
clsGetItems.SetRCommand(strInstatDataObject & "$get_filter_names")
Case "column_selection"
clsGetItems.SetRCommand(strInstatDataObject & "$get_column_selection_names")
Case "object"
Case "object",
RObjectTypeLabel.Graph,
RObjectTypeLabel.Model,
RObjectTypeLabel.Table,
RObjectTypeLabel.Summary,
RObjectTypeLabel.StructureLabel
clsGetItems.SetRCommand(strInstatDataObject & "$get_object_names")
Case "model"
clsGetItems.SetRCommand(strInstatDataObject & "$get_model_names")
Case "graph"
clsGetItems.SetRCommand(strInstatDataObject & "$get_graph_names")
Case "surv"
clsGetItems.SetRCommand(strInstatDataObject & "$get_surv_names")
If strType <> "object" Then
clsGetItems.AddParameter(strParameterName:="object_type_label",
strParameterValue:=Chr(34) & strType & Chr(34))
End If
Case "dataframe"
clsGetItems.SetRCommand(strInstatDataObject & "$get_data_names")
Case "link"
Expand All @@ -1452,8 +1433,6 @@ Public Class RLink
clsGetItems.AddParameter("file", Chr(34) & strNcFilePath & Chr(34))
Case "variable_sets"
clsGetItems.SetRCommand(strInstatDataObject & "$get_variable_sets_names")
Case "table"
clsGetItems.SetRCommand(strInstatDataObject & "$get_table_names")
Case "calculation"
clsGetItems.SetRCommand(strInstatDataObject & "$get_calculation_names")
End Select
Expand Down
20 changes: 12 additions & 8 deletions instat/dlgCombineforGraphics.vb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Public Class dlgCombineforGraphics
Private bFirstLoad As Boolean = True
Private bReset As Boolean = True
Private bResetSubDialog As Boolean = False
Private clsDefaultRFunction As New RFunction
Private clsArrangeRFunction As New RFunction
Private Sub dlgCombineforGraphics_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If bFirstLoad Then
InitialiseDialog()
Expand All @@ -42,28 +42,32 @@ Public Class dlgCombineforGraphics
ucrCombineGraphReceiver.SetParameter(New RParameter("grobs", 0))
ucrCombineGraphReceiver.SetParameterIsRFunction()
ucrCombineGraphReceiver.Selector = ucrCombineGraphSelector
ucrCombineGraphReceiver.SetItemType("graph")
ucrCombineGraphReceiver.SetItemType(RObjectTypeLabel.Graph)
ucrCombineGraphReceiver.strSelectorHeading = "Graphs"

ucrSave.SetPrefix("combined_graph")
ucrSave.SetDataFrameSelector(ucrCombineGraphSelector.ucrAvailableDataFrames)
ucrSave.SetSaveTypeAsGraph()
ucrSave.SetSaveType(strRObjectType:=RObjectTypeLabel.Graph, strRObjectFormat:=RObjectFormat.Image)
ucrSave.SetCheckBoxText("Save Graph")
ucrSave.SetIsComboBox()
ucrSave.SetAssignToIfUncheckedValue("last_graph")
End Sub

Private Sub SetDefaults()
clsDefaultRFunction = New RFunction
clsArrangeRFunction = New RFunction

ucrCombineGraphReceiver.SetMeAsReceiver()
ucrCombineGraphSelector.Reset()
ucrSave.Reset()

clsDefaultRFunction.SetPackageName("gridExtra")
clsDefaultRFunction.SetRCommand("grid.arrange")
clsDefaultRFunction.SetAssignTo("last_graph", strTempDataframe:=ucrCombineGraphSelector.ucrAvailableDataFrames.cboAvailableDataFrames.Text, strTempGraph:="last_graph")
ucrBase.clsRsyntax.SetBaseRFunction(clsDefaultRFunction)
clsArrangeRFunction.SetPackageName("gridExtra")
clsArrangeRFunction.SetRCommand("grid.arrange")
clsArrangeRFunction.SetAssignToOutputObject(strRObjectToAssignTo:="last_graph",
strRObjectTypeLabelToAssignTo:=RObjectTypeLabel.Graph,
strRObjectFormatToAssignTo:=RObjectFormat.Image,
strRDataFrameNameToAddObjectTo:=ucrCombineGraphSelector.strCurrentDataFrame,
strObjectName:="last_graph")
ucrBase.clsRsyntax.SetBaseRFunction(clsArrangeRFunction)
bResetSubDialog = True
End Sub

Expand Down
Loading

0 comments on commit b2200f1

Please sign in to comment.