Skip to main content

r-functions

r-functions is a small library which allows users to call R functions from within Python. Each function runs in a temporary R subprocess which exits upon completion. An async version is provided for concurrent execution.

Consider using Apache Arrow to transfer large datasets between Python and R, as parameters and return values are serialized in memory as JSON.

Prerequisites

  • Python 3.6+
  • R 3/4.0+
    • jsonlite

Installation

Ensure that RScript is in your PATH and jsonlite is installed.

# install jsonlite
Rscript -e "install.packages('jsonlite', repos='https://cloud.r-project.org/', lib = .Library)"

# or pip3, depending on your platform
pip install r-functions 

Example file: test.R

add <- function(a, b) {
    a + b
}

greet <- function(name, adjective) {
    paste("Hello", name, "the", adjective)
}

Example: run functions from test.R

from r_functions import create, run

# create Python functions bound to R functions
add = create("test.R", "add")
greet = create("test.R", "greet")

sum = add(2, 3)
print(sum) # 5

# we can use named parameters or positional parameters, but not both
greeting = greet(name="John", adjective="Wise")
print(greeting) # "Hello John the Wise"

# alternatively, use r_functions.run

# lists provide positional parameters
sum = run("test.R", "add", [2, 3])

# dicts provide named parameters
greeting = run("test.R", "greet", {
    "name": "John",
    "adjective": "Wise",
})

# optionally, provide subprocess options
# https://docs.python.org/3/library/subprocess.html#frequently-used-arguments
run("test.R", "add", [1, 2], {
    "cwd": None, 
    "env": None, 
    "input": None, 
    "stdout": None, 
    "stderr": None, 
    # ...
})

Example: run functions from test.R asynchronously

import asyncio
import sys
from r_functions import create_async, run_async

# on Windows, we must use the ProactorEventLoop to support subprocesses
# https://docs.python.org/3.6/library/asyncio-subprocess.html#windows-event-loop
if sys.platform == "win32":
    asyncio.set_event_loop(asyncio.ProactorEventLoop())

async def main():

    # create async Python functions bound to R functions
    add = create_async("test.R", "add")
    greet = create_async("test.R", "greet")

    sum = await add(2, 3)
    print(sum) # 5

    # we can use named parameters or positional parameters, but not both
    greeting = await greet(name="John", adjective="Wise")
    print(greeting) # "Hello John the Wise"

    # alternatively, use r_functions.run

    # lists provide positional parameters
    sum = await run_async("test.R", "add", [2, 3])

    # dicts provide named parameters
    greeting = await run_async("test.R", "greet", {
        "name": "John",
        "adjective": "Wise",
    })

    # optionally, provide subprocess options - by default, stdout/stderr use subprocess.PIPE
    # https://docs.python.org/3.6/library/asyncio-subprocess.html#asyncio.AbstractEventLoop.subprocess_exec
    await run_async("test.R", "add", [1, 2], {
        "cwd": None, 
        "env": None, 
        "stdout": None, 
        "stderr": None, 
        "limit": None, 
        # ...
    })

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Notes

When running async R functions on Windows, you must use the ProactorEventLoop to support subprocesses. For example:

import asyncio
import sys

if sys.platform == "win32":
    asyncio.set_event_loop(asyncio.ProactorEventLoop())

When supplying custom environmental variables to an R subprocess, you must include a valid PATH which contains the R executable. On Windows, a SYSTEMROOT (usually "C:\Windows" or simply %SystemRoot%) must also be provided.

Specifying custom environmental variables for R can be useful if global changes are not desired, or if the user wishes to use a different version of R without reconfiguring their PATH.

For example:

get_env <- function(name) {
    Sys.getenv(name)
}
import os
from r_functions import run

# on Posix-like systems
test_value = run("test.R", "get_env", {"name": "test"}, {
    "shell": True,
    "env": {
        "test": "test_value", 
        "PATH": "/opt/R-$VERSION/bin",
        "R_PROFILE": "/opt/R-$VERSION/etc/Rprofile.site",
        "R_LIBS": "/opt/R-$VERSION/library",
    }
})

# on Windows
test_value = run("test.R", "get_env", {"name": "test"}, {
    "shell": True,
    "env": {
        "test": "test_value", 
        "PATH": "X:\\R-$VERSION\\bin"
        "R_PROFILE": "X:\\R-$VERSION\\etc\\Rprofile.site",
        "R_LIBS": "X:\\R-$VERSION\\library"
        "SYSTEMROOT": os.path.expandvars("%SystemRoot%"), 
    }
})

Metadata

Release files for r-functions 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for r-functions 1.0.3
File Size Uploaded
r-functions-1.0.3.tar.gz 5.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for r-functions 1.0.3
File Interpreter ABI Platform
r_functions-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 10.4 kB

Release files / r-functions-1.0.3.tar.gz

Download URL r-functions-1.0.3.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
22504777b9e4cdf86a64d641e9cd0983b6fd6ca619dee5b55caeba5018469d50
BLAKE2b-256 checksum
How to use checksums
83e01dea557610389961ff538cc9744716e524bf0cb6c14f178f4b8a13f8e379
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.6.8

Release files / r_functions-1.0.3-py3-none-any.whl

Download URL r_functions-1.0.3-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
29f919a47addf85e4cef6154f825caf7566456a73cb4bbb21d72a3cbf1013245
BLAKE2b-256 checksum
How to use checksums
25ac0568511ad7b7dbaf73b18ef6a4cc88fe644d7ec35647991a4acea14f1f04
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.6.8

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page