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

Improving the additional libPaths #9318

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -398,28 +404,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 @@ -590,10 +593,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])
}
}
Loading