Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Julia from R Issue

Tags:

r

julia

I am using Julia 1.0.0 and R 3.5.1 and I am having an issue calling Julia from R/RStudio using both JuliaCall and XRJulia.

When I try:

library(JuliaCall)
julia <- julia_setup(JULIA_HOME = "C:\\Program Files\\Julia-1.0.0\\bin")

The Julia version is located but an error message pops up saying "libgcc_s_seh-1.dll is missing"

and when I try :

library(XRJulia)
JULIA_BIN <- "C:\\Program Files\\Julia-1.0.0\\bin"
findJulia(test=TRUE)

The test returns false.

However, when I call R from Julia using RCall, Julia can locate and use R (using Atom/Juno IDE). Both Julia and R are located in C:\Program Files path.

What is the correct way to call Julia from R/RStuio?

I looked here for a solution but could not get it to work.

Update- I tried this solution so that R can recognize where the Julia files are located. When I restart my session and set the new env and then try to run the code below my R session just aborts

Sys.setenv(PATH = paste(Sys.getenv("PATH"),"C:\\Program Files\\Julia-1.0.0\\bin",sep=";"))
library(JuliaCall)
julia <- julia_setup(JULIA_HOME = "C:\\Program Files\\Julia-1.0.0\\bin")
like image 929
Mike Avatar asked Nov 07 '22 01:11

Mike


1 Answers

(This is more generic advice with dll issues with Julia. If I get R installed, I'll try the exact situation.)

Windows SetDllDirectory issues

I was getting a similar issue with PyInstaller calling another language. It ended up being related to a call to SetDllDirectory in PyInstaller that modified the search path in an odd way.

https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setdlldirectorya

https://github.com/pyinstaller/pyinstaller/wiki/Recipe-subprocess#windows-dll-loading-order

To fix it, I just had to tell python to reset SetDllDirectory back to NULL. I'm not entirely sure of the equivalent in R.

SysInternals ListDlls

When debugging what dlls are getting loaded, I am a huge fan of this tool offered by SysInternals. Download it, and run it with ListDlls.exe julia or ListDlls.exe R while julia or R is running.

https://learn.microsoft.com/en-us/sysinternals/downloads/listdlls

R dyn.load documentation

It also sounds like R tries to do a lot of things to find dlls if you load in a 3rd party dll, especially on Windows.

https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/dyn.load

The ‘standard mechanisms for loading DLLs’ include a search order for where a DLL is found (if not given as an absolute path, which is preferred), and of where its dependent DLLs will be found. This search path depends on the version of Windows and its security settings, but for versions since Windows XP SP1 it is

  • The directory from which the application was launched.

  • The various system directories, e.g.c:/Windows/system32, c:/Windows/system and c:/Windows.

  • The current directory.

  • Along the search path for executables given by the environment variable PATH.

Packages often want to supply dependent DLLs in their libs directory, and do this by setting the PATH variable (library.dynam does that automatically in recent versions of R), but the DLL search order means that DLLs in the launch directory and in system directories will be preferred. On Windows XP SP1 and later there is a way to modify the search order. If argument DLLpath is supplied to dyn.load, the latter makes use of the Windows system call SetDllDirectory to insert the value of DLLpath in second place, and removes the current directory, for the duration of that dyn.load call. (Note that only one directory can be inserted in this way.)

Users have been confused by messages like

error: unable to load shared object 
'.../library/rJava/libs/x64/rJava.dll':  
LoadLibrary failure:  The specified module could not be found. 

The final line is a Windows (not R) diagnostic: the ‘module’ that could not be found is not rJava.dll but something else Windows is looking for (here most likely Java DLLs): if you are lucky there will be a dialog box with more details.

How Julia Loads Sys.dll

Julia 1.x current loads some dlls from the folder where the exe is in bin/ and it also loads sys.dll from a nearby folder: ../lib/julia/sys.dll . You can override this by specifying where sys.dll is with the -J argument when calling julia.

Hope that helps.

like image 115
phyatt Avatar answered Nov 12 '22 21:11

phyatt