https://github.com/zitsen/libr.rs
A library for types and bingings to native C functions in libR in Rust
https://github.com/zitsen/libr.rs
Last synced: 5 months ago
JSON representation
A library for types and bingings to native C functions in libR in Rust
- Host: GitHub
- URL: https://github.com/zitsen/libr.rs
- Owner: zitsen
- License: mit
- Created: 2015-11-16T02:09:12.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2018-03-20T06:53:57.000Z (over 8 years ago)
- Last Synced: 2026-01-14T01:33:34.740Z (6 months ago)
- Language: Rust
- Size: 1.24 MB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
libr
====
**NOTE: Now I cannot publish libr to its crate name because crates.io says I'm not the owner. The crate name will be changed when I will.**
Native C types and bindings of libR.
[](https://travis-ci.org/zisten/libr.rs)
[Documentation](http://zisten.github.io/libr.rs/libr/index.html)
## Usage
First, add the following to your Cargo.toml:
```toml
[dependencies]
libr = 0.1
```
Next, add this to your crate root:
```rust
extern crate libr;
```
## What is libr?
The primary purpose of this crate is to provide an easily
interface for embedding R functions. This include type
definitions (e.g. `SEXP`), constants(e.g. `PI`) as well as
function headers(e.g. `Rf_initEmbeddedR`)
The library now support 64bit Linux only, for the case
I have no idea to how to test it in other system. I'm
a newer in [Rust](http://rust-lang.org), so it's so welcome
for help and suggestions.
## Examples
### For a similar native R-like app, use this:
```rust
extern crate libr;
use std::env;
use std::ffi::CString;
use libr::embedded::Rf_initialize_R;
use libr::interface::*;
fn main() {
if let Err(_) = env::var("R_HOME") {
panic!("Rembedded test need R_HOME be setted");
}
let args = vec!["R", "--no-save"];
let mut args = args.into_iter()
.map(|arg|
CString::new(arg.as_bytes()).unwrap().into_raw())
.collect::>();
unsafe {
R_running_as_main_program = 1;
Rf_initialize_R(args.len() as i32, args.as_mut_ptr());
Rf_mainloop();
return;
}
}
```
### R math functions
See details in [math](math/index.html).
```rust
extern crate libr;
use libr::math::R_pow;
fn main() {
assert_eq!(unsafe { R_pow(2., 3.) }, 8.);
}
```
### Embedded R
See details documents in [embedded module](embedded/index.html).
```rust
extern crate libr;
use std::env;
use std::ffi::CString;
use libr::internals::*;
use libr::embedded as Rembedded;
use libr::ext::parse::{ParseStatus, R_ParseVector};
fn main() {
if let Err(_) = env::var("R_HOME") {
panic!("Rembedded test need R_HOME be setted");
}
let mut s = Box::new(vec![CString::new("R").unwrap().into_raw(),
CString::new("--quiet").unwrap().into_raw(),
CString::new("--no-save").unwrap().into_raw()]);
unsafe {
Rembedded::Rf_initEmbeddedR(s.len() as i32, s.as_mut_ptr());
// Plot
let mut status = ParseStatus::PARSE_OK;
let mut had_error = 0;
let tmp = Rf_protect(Rf_mkString(CString::new("{pdf(\"03-plot.pdf\"); plot(1:10, \
pch=\"+\"); print(1:10)}")
.unwrap()
.into_raw()));
let e = Rf_protect(R_ParseVector(tmp, 1, &mut status, R_NilValue));
Rf_PrintValue(e);
R_tryEval(VECTOR_ELT(e, 0), R_GlobalEnv, &mut had_error);
Rf_unprotect(2);
Rembedded::Rf_endEmbeddedR(0);
}
}
```