The @rlibrary and @rimport Macros

1. @rlibrary

The @rlibrary macro lets the user import the installed R packages into the current session in the Julia prompt. RCall package must be installed and imported to use it in our code.

Julia




julia> @rlibrary datasets
  
julia> R"head(iris)"


In the above code, we are importing the datasets package of R in our current Julia session using the rlibrary macro. Then we are using the iris dataset and the head() function of R to print the first 5 rows of the iris dataset.

Output

RObject{VecSxp}
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

2. @rimport

The work of @rimport is similar to as of @rlibrary, but this allows to import of specific functions rather than the entire package. Also, it allows the users to import using an alias

Example –

Julia




julia> @rimport base as bs
  
julia> bs.sum([10,20,30])


Output

60



Julia – RCall

To bridge the gap between the R language, which has been in the scene for a long time and is renowned for its huge collection of statistical and data analysis packages, and one of the newest members, Julia, which is a high-level programming language, the package RCall has been developed. using the RCall package, the developers can use the power and features of both languages simultaneously. use the libraries of R in Julia or vice versa. Even developers can run R code from the Julia prompt, transfer variables, etc. It allows seamless integration of R’s functionality within a Julia environment.

Similar Reads

Prerequisites

1. The R language and Julia language both must be installed on the user’s device....

Implementation

Now we will convert the Julia prompt into an R prompt. While in the same command line/ terminal press the SHIFT+$ key together, and the prompt will change from Julia to R. Now to go back to Julia we will just press the BACKSPACE once....

RCall API

...

The @rlibrary and @rimport Macros

...

Contact Us