Skip to content
This repository has been archived by the owner on Jun 30, 2024. It is now read-only.

Commit

Permalink
Add extendr to R type mapping (#14)
Browse files Browse the repository at this point in the history
* add data types doc

* add listsxp nextto pairlist

* fix: add `data-types.qmd` to the mix

* additional types written

---------

Co-authored-by: Mossa <[email protected]>
  • Loading branch information
JosiahParry and CGMossa authored Mar 31, 2024
1 parent 9544462 commit 12f9e29
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions _quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ website:
- hello-world.qmd
- project-structure.qmd
- conversion.qmd
- data-types.qmd
style: "docked"
search: true
navbar:
Expand Down
47 changes: 47 additions & 0 deletions data-types.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
title: R to Rust type mapping
---

extendr is allows us to create a bridge between R and Rust. When writing a function in Rust that is intended to be called from R, it is important that the input types be R types. There are many types of objects in R (almost) all of which are available in extendr via a wrapper struct.

The below lists the extendr structs that wrap R object types. These types can be used as function arguments or return values.

## Scalar types

| R type | extendr type | Rust type |
| ------| -----------| ----------|
| `integer(1)` | `Rint` | `i32` |
| `double(1)` | `Rfloat` | `f64` |
| `logical(1)` | `Rbool` | `bool` |
| `complex(1)` | `Rcplx` | `Complex<f64>` |
| `character(1)` | `Rstr` | `String` |

## Vector types

| R type | extendr type | extendr scalar type | C API Type |
|-------------|------------|-------------------|----------|
| `integer()` | `Integers` | `Rint` | `INTSXP` |
| `double()` | `Doubles` | `Rfloat` | `REALSXP` |
| `logical()` | `Logicals` | `Rbool` | `LGLSXP` |
| `complex()` | `Complexes` | `Rcplx` | `CPLXSXP` |
| `character()` | `Strings` | `Rstr` | `STRSXP` |
| `raw()` | `Raw` | `&[u8]` | `RAWSXP` |
| `list()` | `List` | `Robj` | `VECSXP` |


## Other types


- [`Environment`](https://extendr.github.io/extendr/extendr_api/wrapper/environment/struct.Environment.html) (`ENVSXP`)
- [`Expressions`](https://extendr.github.io/extendr/extendr_api/wrapper/expr/struct.Expressions.html) (`EXPRSXP`)
- [`ExternalPtr`](https://extendr.github.io/extendr/extendr_api/wrapper/externalptr/struct.ExternalPtr.html) (`EXTPTRSXP`)
- [`Function`](https://extendr.github.io/extendr/extendr_api/wrapper/function/struct.Function.html) (`CLOSSXP`)
- [`Language`](https://extendr.github.io/extendr/extendr_api/wrapper/lang/struct.Language.html) (`LANGSXP`)
- [`RArray`](https://extendr.github.io/extendr/extendr_api/wrapper/matrix/struct.RArray.html)
- [`Pairlist`](https://extendr.github.io/extendr/extendr_api/wrapper/pairlist/struct.Pairlist.html) (`LISTSXP`)
- [`Promise`](https://extendr.github.io/extendr/extendr_api/wrapper/promise/struct.Promise.html) (`PROMSXP`)
- [`S4`](https://extendr.github.io/extendr/extendr_api/wrapper/s4/struct.S4.html) (`S4SXP`)

## Using Rust library types vs R-native types

## Returning Rust values to R

0 comments on commit 12f9e29

Please sign in to comment.