Learn R Programming

JuliaConnectoR (version 1.1.4)

juliaImport: 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)

Value

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

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.

Examples

Run this code
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$``(1)
   GreekModule$`log`(1)
}

# \dontshow{
JuliaConnectoR:::stopJulia()
# }

Run the code above in your browser using DataLab