Skip to content

Commit

Permalink
Merge pull request #9318 from N-thony/improve_additional_libPath
Browse files Browse the repository at this point in the history
Improving the additional libPaths
  • Loading branch information
ChrisMarsh82 authored Jan 13, 2025
2 parents de7f1d5 + ed3d4c2 commit 6b61592
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
33 changes: 17 additions & 16 deletions instat/frmMain.vb
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,15 @@ Public Class frmMain
'---------------------------------------

'--------------------------------------
CreateAdditionalLibraryDirectory()
Dim strVersion As String = My.Application.Info.Version.Major.ToString() & "." &
My.Application.Info.Version.Minor.ToString() & "." &
My.Application.Info.Version.Build.ToString()

Me.Text = "R-Instat " & strVersion

CreateAdditionalLibraryDirectory(strVersion)
'-------------------------------------
SetAppVersionNumber()

isMaximised = True 'Need to get the windowstate when the application is loaded
SetHideMenus()
End Sub
Expand Down Expand Up @@ -411,28 +417,25 @@ Public Class frmMain
End If
End Function

Private Sub CreateAdditionalLibraryDirectory()
Private Sub CreateAdditionalLibraryDirectory(strVersion As String)
' Define the custom library path in the ApplicationData folder
Dim strLibraryPath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "R-Instat", "library")
Dim strLibraryPath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "R-Instat", strVersion, "library")

Try
' Check if the directory exists, if not, create it
If Not Directory.Exists(strLibraryPath) Then
Directory.CreateDirectory(strLibraryPath)
End If


'To ensure this part of the code only runs when the application Is Not in the Debug mode (i.e., in Release mode)
#If Not DEBUG Then
' Add the custom library path to R's .libPaths for user-level package installation
Dim strScript As String = $".libPaths(c('{strLibraryPath.Replace("\", "/")}', .libPaths()))" & Environment.NewLine &
"if (length(.libPaths()) > 2) {
current_paths <- .libPaths()
valid_indices <- c(1, 3)[c(1, 3) <= length(current_paths)]
.libPaths(current_paths[valid_indices])
}"
Dim clsSetLibPathsFunction As New RFunction
clsSetLibPathsFunction.SetRCommand("set_library_paths")
clsSetLibPathsFunction.AddParameter("library_path", Chr(34) & strLibraryPath.Replace("\", "/") & Chr(34))

' Execute the R script to update the library paths
clsRLink.RunScript(strScript:=strScript, bSeparateThread:=False, bSilent:=False)
clsRLink.RunScript(strScript:=clsSetLibPathsFunction.ToScript, bSeparateThread:=False, bSilent:=False)
#End If
Catch ex As Exception
' Handle potential errors (e.g., directory creation failure)
Expand Down Expand Up @@ -603,10 +606,8 @@ Public Class frmMain
mnuTbLan.Visible = bVisible
End Sub

Public Sub SetAppVersionNumber()
Me.Text = "R-Instat " & My.Application.Info.Version.Major.ToString() & "." &
My.Application.Info.Version.Minor.ToString() & "." &
My.Application.Info.Version.Build.ToString()
Public Sub SetAppVersionNumber(strVersionNumber As String)

End Sub

Private Sub SetHideMenus()
Expand Down
19 changes: 18 additions & 1 deletion instat/static/InstatObject/R/stand_alone_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -3429,4 +3429,21 @@ monitor_memory <- function() {
time_operation <- function(expr) {
timing <- system.time(expr)
print(timing)
}
}

set_library_paths <- function(library_path) {
# Update the library paths
.libPaths(c(library_path, .libPaths()))

# Check if there are more than 2 library paths
if (length(.libPaths()) > 2) {
# Get the current library paths
current_paths <- .libPaths()

# Select valid indices (1 and 3) only if they exist
valid_indices <- c(1, 3)[c(1, 3) <= length(current_paths)]

# Set the library paths to the valid ones
.libPaths(current_paths[valid_indices])
}
}

0 comments on commit 6b61592

Please sign in to comment.