Get startedGet started for free

Importing Functions from Python and R

1. Importing Functions from Python and R

External packages such as DataFrames.jl allow us to extend Julia further.

2. Package overview

While Julia has impressive external packages, we might want to use packages from Python or R. Perhaps we're working in a niche area, and an excellent package is only available in Python or R, or we want to keep code consistent across a company. Fortunately, it is possible to import these packages into Julia. To do this, we need to install two packages - PythonCall and RCall, which allow us to call Python and R functions respectively. Note that the reverse is possible. We can call Julia code from Python and R, but we will not cover this here.

3. Importing python packages

To import Python functions in Julia, we use the PythonCall package. You can use any package with PythonCall, as long as it is installed. We import PythonCall, and then import a package from Python using the pyimport function. In this example we have imported the time package, giving it an alias of pytime. The pyimport function is just one feature of PythonCall. Other, more complex functions exist that we will not cover here.

4. Calling python functions

Let's call functions from Python's time package. We have imported PythonCall, the time package with alias pytime, and called the ctime function, which returns the local time and date. Looking at the output, we get a date and time for when this function was called. Let's try another example. We will now call strftime, which returns a formatted string. As an input it takes a datetime. Using Python formatting, we use %m and %Y to indicate we want the month and year as output, and we pass in another function, localtime, which returns the local time. Chaining these expressions inside a print statement prints the formatted local time - the month and year.

5. Importing R libraries

To use R functions in Julia, we use the RCall package. Note we need to have R installed on our computer. Again, we can use any installed R package in our Julia program. Now, we use the using syntax to import RCall into Julia. Then, to load an R package, we use the @rimport macro, followed by the name of the package we want to import. Here we have imported two packages, base, the base R library, and ggplot, one of the most popular plotting packages. Note there are a lot of advanced functions in the RCall package, but we will not cover them in this course.

6. Calling R functions

Now that we have imported RCall into Julia, let's call an R function. We will use the base R environment. When importing the base package, we have given it an alias, r_base. To call a function from R, we prefix that function using our alias. Let's call R's sum function. We prefix our function call with the alias of the base package, r_base, and then pass a vector of integers to the sum function. As expected, we get the sum of the integers, 15.

7. R sum output

We can compare this to the output we get when calling the function in R. This image shows the output in R - and it is consistent with what we see in Julia.

8. Calling further R functions

Let's call another R function. We call abbreviate, a function that will abbreviate given strings to a set number of characters, in this case three, by stripping vowels, ensuring that all strings passed into the function have unique abbreviations. We can see the output here in Julia, with each string converted into a three-letter abbreviation, taking the first three letters of each string, with vowels stripped.

9. Let's practice!

Now that you've seen how to use Python and R in Julia, let's practice!