Running .jl file from R or Python

Viewed 998

I am new to Julia. I developed a few lines of code to get the results I needed from packages I was not able to find in Python or R. Now, I am trying to get this file to be easily accessible, and wrap the code in Python or R. Has anyone done this before? I have tried a few methods and have not found anything that has helped.

The most simple way to do this would be just a few lines of code that calls the .jl file, runs it (which the code is then added to a .txt file from julia), and then alerts you when the code is done.

Any help would be greatly appreciated. R is the preferable method and at this point Python would be appreciated as well.

2 Answers

Please find below instructions for Python, R and just an external process (which of course is an executable command that can be run from any other process). I recommend putting your code in a package and loading it in one of those languages rather than executing this as an external process.

Python

  1. Use Python Anaconda (not in-built system Python) and install Julia

  2. Run Julia and install PyCall

    using Pkg
    ENV["PYTHON"]="/path/to/your/python/executable"
    pkg"add PyCall"
    pkg"build PyCall"
    
  3. Put your code into a Julia package

    using Pkg
    Pkg.generate("MyPackage")
    

    In the folder src you will find MyPackage.jl, edit it to look like this:

    module MyPackage
    function main(x,y)
         #do very complex staff or place it in your_other_file.jl
         2x.+y
    end
    include("your_other_file.jl")
    export main, and_whatever_other_functio_you_defined
    end
    
  4. Install pyjulia

    python -m pip install julia
    

    (On Linux systems you might want to use python3 instead of python command)

    For this step note that while an external Python can be used with Julia. However, for a convenience it might be worth to consider using a Python that got installed together with Julia as PyCall. In that case for installation use a command such this one:

    %HOMEPATH%\.julia\conda\3\python -m pip install julia
    

    or on Linux

    ~/.julia/conda/3/python -m pip install julia
    

    Note that if you have JULIA_DEPOT_PATH variable defined you can replace %HOMEPATH%\.julia or ~/.julia/ with its value.

  5. Run the appropiate Python and tell it to configure the Python-Julia integration:

    import julia
    julia.install()
    
  6. Now you are ready to call your Julia code:

    >>> from julia import Pkg
    >>> Pkg.activate(".\\MyPackage") #use the correct path
        Activating environment at `MyPackage\Project.toml`
    >>> from julia import MyPackage
    >>> MyPackage.main([1,2],5)
        [7,9]
    

Gnu R

  1. Configure your system PATH variable to point to your Julia location. Hence when you type julia in the console it should start Julia

  2. Run the script below to install R-Julia integration

install.packages("JuliaCall")

library(JuliaCall)
julia <- julia_setup()
  1. Follow the above instructions for Python (step 3 only) and create the package named MyPackage

  2. Run the code

library(JuliaCall)
julia_eval("using Pkg;Pkg.activate(\"C:/temp/rrr/MyPackage\")")
julia_library("MyPackage")

julia_eval("MyPackage.main(3,5)")

Bash (or just any language)

  1. Build the package following instructions for Python (step 3 only)

  2. Configure the system PATH variable

  3. Being in the package directory run the command (note string(:.) is a Julian trick that I use to avoid apostrophe escaping in bash commands):

julia -e "using Pkg;Pkg.activate(string(:.));Pkg.instantiate();using MyPackage;MyPackage.main(3,4)"

This will install all dependencies for your package. In order to skip the installation remove Pkg.instantiate() from the above command.

The answer from @przemyslaw-szufel is correct but maybe a bit overcomplicated. You don't necessarily need to wrap your code in a module or define a custom environment (yes it is good practice, but a step at the time...)

First create a file juliaScripts.jl with content:

function getAnElement(array,n)
    return array[n]
end

To run functions defined in a .jl file in R

Then in R you just do:

> install.packages("JuliaCall")
> library(JuliaCall)
> julia_setup() # on every new R session !
> julia_source("juliaScript.jl")
> out <- julia_call("getAnElement",c(10,20,30),2)
> out
[1] 20

Note that the R vector has been automatically converted to a Julia Array.

To run functions defined in a .jl file in Python

In Python, it is even easier:

$ python3 -m pip install --user julia
>>> import julia
>>> julia.install() # only once, not every session
>>> jl=julia.Julia(compiled_modules=False)
>>> from julia import Main
>>> Main.include("juliaScript.jl")
>>> Main.getAnElement([1,2,3],2)
20

Also in Python arrays (native python lists as well Numpy arrays and other commonly used data structures) are automatically converted between Python and Julia.

Not to make advertising, but more details on interfacing R <-> Julia or Python <-> Julia are on my Apress(2019) book "Julia Quick Syntax reference" in Chapter 7 "Interfacing Julia with other languages" (I shouldn't say it, but you can easily find the pdf online in well-known sites...)

Related