Skip to content

Commit

Permalink
improved the addtional path setting code
Browse files Browse the repository at this point in the history
  • Loading branch information
N-thony committed Dec 18, 2024
1 parent cf9c758 commit ed3d4c2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
13 changes: 5 additions & 8 deletions instat/frmMain.vb
Original file line number Diff line number Diff line change
Expand Up @@ -414,18 +414,15 @@ Public Class frmMain
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
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 ed3d4c2

Please sign in to comment.