pola-rs / r-polars

Bring polars to R
https://pola-rs.github.io/r-polars/
Other
405 stars 35 forks source link

Building a rust binding R package depends on this r-polars #808

Open jshinonome opened 3 months ago

jshinonome commented 3 months ago

First of all, thank you very much for this great R Polars project.

I would like to implement a db connection rust binding R package and convert data to polars directly and hence r-polars, but I don't know how to access r-polars robj. e.g. py-polars has https://github.com/pola-rs/pyo3-polars, which allows me to access python polars object. I was struggling to access r-polars series and dataframe from rust code, keep getting "invalid permission" or "memory not mapped" error.

Any suggestion is appreciated.

I defined a same RPolarsDataFrame struct, but apparently, this doesn't work.

if any.inherits("RPolarsDataFrame") {
        let df: ExternalPtr<RPolarsDataFrame> = any.try_into().unwrap();
        Ok(K::DataFrame(df.0.clone()))
    }
eitsupi commented 3 months ago

Here is https://github.com/rpolars/extendr_polars written by @sorhawell, but I don't know if it currently works as it is not tested and unmaintained.

See also #732 and https://github.com/pola-rs/r-polars/pull/776#discussion_r1479727362

Ideally, I think polars::DataFrame (Rust) should implement the Arrow C Stream interface and use it for each language's binding. (pola-rs/polars#14208)

jshinonome commented 3 months ago

Thanks for the answers @eitsupi . I will try the repo above

eitsupi commented 3 months ago

Contributions to https://github.com/rpolars/extendr_polars or this repository are welcome!

eitsupi commented 3 weeks ago

When #1078 is merged, input/output through the Arrow C stream interface of Series and DataFrame will be supported, so it should be possible to read Series and DataFrame with nanoarrow_array_stream as an intermediate format. See also https://github.com/JosiahParry/arrow-extendr.

ju6ge commented 1 day ago

In the near term there is also a very easy way to make this library publishable on crates.io. All you would really needed to do to make this possible is to stop using git imports in your Cargo.toml. That way you could just publish the rust part of the code as a crate and therefor eliminate the need for https://github.com/rpolars/extendr_polars. Which is just a very hacky solution to a problem that should just not exist. The main blocker here is not polars but the used the extendr crate. There have been quite a few features added to main but not published as a version.

ju6ge commented 1 day ago

Oh and allowing the library to also still be built as rust library instead of staticlib only should also help.