Skip to content

Commit 6b61592

Browse files
authored
Merge pull request #9318 from N-thony/improve_additional_libPath
Improving the additional libPaths
2 parents de7f1d5 + ed3d4c2 commit 6b61592

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

instat/frmMain.vb

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,15 @@ Public Class frmMain
223223
'---------------------------------------
224224

225225
'--------------------------------------
226-
CreateAdditionalLibraryDirectory()
226+
Dim strVersion As String = My.Application.Info.Version.Major.ToString() & "." &
227+
My.Application.Info.Version.Minor.ToString() & "." &
228+
My.Application.Info.Version.Build.ToString()
229+
230+
Me.Text = "R-Instat " & strVersion
231+
232+
CreateAdditionalLibraryDirectory(strVersion)
227233
'-------------------------------------
228-
SetAppVersionNumber()
234+
229235
isMaximised = True 'Need to get the windowstate when the application is loaded
230236
SetHideMenus()
231237
End Sub
@@ -411,28 +417,25 @@ Public Class frmMain
411417
End If
412418
End Function
413419

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

418424
Try
419425
' Check if the directory exists, if not, create it
420426
If Not Directory.Exists(strLibraryPath) Then
421427
Directory.CreateDirectory(strLibraryPath)
422428
End If
423429

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

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

606-
Public Sub SetAppVersionNumber()
607-
Me.Text = "R-Instat " & My.Application.Info.Version.Major.ToString() & "." &
608-
My.Application.Info.Version.Minor.ToString() & "." &
609-
My.Application.Info.Version.Build.ToString()
609+
Public Sub SetAppVersionNumber(strVersionNumber As String)
610+
610611
End Sub
611612

612613
Private Sub SetHideMenus()

instat/static/InstatObject/R/stand_alone_functions.R

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3429,4 +3429,21 @@ monitor_memory <- function() {
34293429
time_operation <- function(expr) {
34303430
timing <- system.time(expr)
34313431
print(timing)
3432-
}
3432+
}
3433+
3434+
set_library_paths <- function(library_path) {
3435+
# Update the library paths
3436+
.libPaths(c(library_path, .libPaths()))
3437+
3438+
# Check if there are more than 2 library paths
3439+
if (length(.libPaths()) > 2) {
3440+
# Get the current library paths
3441+
current_paths <- .libPaths()
3442+
3443+
# Select valid indices (1 and 3) only if they exist
3444+
valid_indices <- c(1, 3)[c(1, 3) <= length(current_paths)]
3445+
3446+
# Set the library paths to the valid ones
3447+
.libPaths(current_paths[valid_indices])
3448+
}
3449+
}

0 commit comments

Comments
 (0)