| name | r-language-api |
| description | R interfaces to other languages. Use for calling Python, Java, JavaScript, and other languages from R. |
R Language Interfaces
Call other programming languages from R.
reticulate (Python)
library(reticulate)
py_run_string("x = 1 + 1")
py$x
np <- import("numpy")
pd <- import("pandas")
np$array(c(1, 2, 3))
pd$DataFrame(list(a = 1:3, b = 4:6))
rJava (Java)
library(rJava)
.jinit()
str <- .jnew("java/lang/String", "Hello")
.jcall(str, "I", "length")
.jcall(str, "S", "toUpperCase")
V8 (JavaScript)
library(V8)
ctx <- v8()
ctx$eval("var x = 1 + 1")
ctx$get("x")
ctx$eval("function add(a, b) { return a + b; }")
ctx$call("add", 1, 2)
Rcpp (C++)
library(Rcpp)
cppFunction('
int add(int x, int y) {
return x + y;
}
')
add(1, 2)
sourceCpp("functions.cpp")
Data Exchange
py$df <- r_to_py(mtcars)
r_df <- py_to_r(py$df)
reticulate::py_config()
Best Practices
reticulate::py_available()
rJava::.jcheck()
tryCatch({
py_run_string("import nonexistent")
}, error = function(e) {
message("Python error: ", e$message)
})