juliaImport: Load and import a Julia package via 'import' statement

View source: R/importing.R

juliaImportR Documentation

Load and import a Julia package via import statement

Description

The specified package/module is loaded via import in Julia. Its functions and type constructors are wrapped into R functions. The return value is an environment containing all these R functions.

Usage

juliaImport(modulePath, all = TRUE)

Arguments

modulePath

a module path or a module object. A module path may simply be the name of a package but it may also be a relative module path. Specifying a relative Julia module path like .MyModule allows importing a module that does not correspond to a package, but has been loaded in the Main module, e. g. by juliaCall("include", "path/to/MyModule.jl"). Additionally, via a path such as SomePkg.SubModule, a submodule of a package can be imported.

all

logical value, default TRUE. Specifies whether all functions and types shall be imported or only those exported explicitly.

Value

an environment containing all functions and type constructors from the specified module as R functions

Note

If a package or module contains functions or types with names that contain non-ASCII characters, (additional) alternatives names are provided if there are LaTeX-like names for the characters available in Julia. In the alternative names of the variables, the LaTeX-like names of the characters surrounded by <...> replace the original characters. (See example below.) For writing platform independent code, it is recommended to use those alternative names. (See also JuliaConnectoR-package under "Limitations".)

Examples

if (juliaSetupOk()) {

   # Importing a package and using one of its exported functions
   UUIDs <- juliaImport("UUIDs")
   juliaCall("string", UUIDs$uuid4())


   # Importing a module without a package
   testModule <- system.file("examples", "TestModule1.jl",
                             package = "JuliaConnectoR")
   # take a look at the file
   writeLines(readLines(testModule))
   # load in Julia
   juliaCall("include", testModule)
   # import in R via relative module path
   TestModule1 <- juliaImport(".TestModule1")
   TestModule1$test1()

   # Importing a local module is also possible in one line,
   # by directly using the module object returned by "include".
   TestModule1 <- juliaImport(juliaCall("include", testModule))
   TestModule1$test1()


   # Importing a submodule
   testModule <- system.file("examples", "TestModule1.jl",
                             package = "JuliaConnectoR")
   juliaCall("include", testModule)
   # load sub-module via module path
   SubModule1 <- juliaImport(".TestModule1.SubModule1")
   # call function of submodule
   SubModule1$test2()


   # Functions using non-ASCII characters
   greekModule <- system.file("examples", "GreekModule.jl",
                             package = "JuliaConnectoR")
   suppressWarnings({ # importing gives a warning on non-UTF-8 locales
      GreekModule <- juliaImport(juliaCall("include", greekModule))
   })
   # take a look at the file
   cat(readLines(greekModule, encoding = "UTF-8"), sep = "\n")
   # use alternative names
   GreekModule$`<sigma>`(1)
   GreekModule$`log<sigma>`(1)
}



JuliaConnectoR documentation built on Oct. 9, 2024, 1:08 a.m.