Skip to content

Commit 7a33de7

Browse files
committed
Document get_r_func()
1 parent 58a7ee1 commit 7a33de7

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

doc/api/r.rst

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
Integrate R code (:mod:`.util.r`)
2+
*********************************
3+
4+
Developers working with :mod:`message_ix_models` can integrate existing R codes or use R to perform data processing related to building, running, and reporting models.
5+
:mod:`message_ix_models.util.r` provides utility code to help with using :mod:`rpy2` to achieve this integration.
6+
7+
Two patterns are supported.
8+
To use either, place a :file:`r_code.R` *in-line* in the source tree, either in :mod:`message_ix_models`, :mod:`message_data`, or another package::
9+
10+
message_ix_models/
11+
project/
12+
example/
13+
__init__.py
14+
module_a.py
15+
module_b.py
16+
r_code.R
17+
18+
The patterns are:
19+
20+
.. contents::
21+
:local:
22+
:backlinks: none
23+
24+
25+
R ‘Modules’
26+
===========
27+
28+
In this pattern, :func:`get_r_func` is used to source (i.e. import or run) R code from a file that defines one or more functions or other objects.
29+
:func:`get_r_func` returns this function, which can then be called from Python code.
30+
In this way, the R source file functions somewhat like a Python **‘module’**, while still being lighter weight than a full, installable R package.
31+
32+
.. code-block:: R
33+
:caption: :file:`r_code.R`
34+
35+
# R file that defines functions and other entry points
36+
37+
mul <- function(x, y) {
38+
# Multiply operands
39+
return(x, y)
40+
}
41+
42+
.. code-block:: python
43+
44+
from message_ix_models.util import get_r_func
45+
46+
# Source r_code.R, retrieve the function named "mul"
47+
mul = get_r_func("message_ix_models.project.example.r_code:mul")
48+
49+
# Call the function
50+
result = mul(1.2, 3.4)
51+
52+
# …use `result` in further Python code
53+
54+
.. currentmodule:: message_ix_models.util.r
55+
56+
.. autofunction:: get_r_func
57+
58+
59+
Stand-alone scripts
60+
===================
61+
62+
In this method, the R script is placed inline.

doc/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Commonly used classes may be imported directly from :mod:`message_ix_models`.
7474
api/tools-messagev
7575
api/data-sources
7676
api/util
77+
api/r
7778
api/testing
7879
api/workflow
7980

message_ix_models/util/r.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Utilities for compatibility with R code."""
22
import re
3+
from typing import Any
34

45
from .common import MESSAGE_DATA_PATH, MESSAGE_MODELS_PATH
56

@@ -30,11 +31,21 @@ def source_module(path):
3031
r.source(str(path))
3132

3233

33-
def get_r_func(path):
34+
def get_r_func(path: str) -> Any:
35+
"""Source R code and return an R function or other object.
36+
37+
Parameters
38+
----------
39+
path : str
40+
Identifies the path to the R ‘module’ and the name of the object to be loaded.
41+
"""
3442
from rpy2.robjects import r
3543

44+
# Separate R code path and object name
3645
path, name = path.rsplit(":", maxsplit=1)
3746

47+
# Source the R code
3848
source_module(path)
3949

50+
# Retrieve and return the object
4051
return r[name]

0 commit comments

Comments
 (0)