Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] colnames and rownames wrappers #273

Open
pachadotdev opened this issue May 26, 2022 · 2 comments
Open

[Feature Request] colnames and rownames wrappers #273

pachadotdev opened this issue May 26, 2022 · 2 comments

Comments

@pachadotdev
Copy link
Contributor

Such as it's the case for names(), it would be extremely convenient to have rownames() and colnames() in cpp codes.

My idea (sorry that I don't enough C++) would be like:

// THIS WORKS
[[cpp11::register]]
doubles cpp_names(writable::doubles X) {
    X.attr("names") = {"a","b"};
    return X;
}

// THIS WON'T
// [[cpp11::register]]
// doubles_matrix<> cpp_colnames(writable::doubles_matrix<> X) {
//     writable::list dimnames;
//     dimnames.push_back(NULL);
//     dimnames.push_back({"a","b"});
//     X.attr("dimnames") = dimnames;
//     return X;
// }
@stephematician
Copy link

stephematician commented Apr 15, 2023

Furthermore .... attributes can't be set on matrices. For example:

// set_and_get.cpp
#include "cpp11.hpp"

using namespace cpp11;

[[cpp11::register]]
sexp set_and_get_attr() {
    writable::doubles_matrix<> Y(0,0);
    Y.attr("my-attr") = writable::strings({"hello"}); 
    return Y.attr("my-attr");
}

Then in R:

cpp11::cpp_source('set_and_get.cpp')
set_and_get_attr()
# NULL

To set attributes, I think something like moving (or maybe copying? didn't try it) to a sexp works e.g.

// hacky_set_attr.cpp
#include "cpp11.hpp"

using namespace cpp11;

[[cpp11::register]]
sexp hacky_set_attr() {
    writable::doubles_matrix<> Y(0,0);
    sexp Y_sexp(std::move(Y.data()));
    Y_sexp.attr("my-attr") = "hello"; 
    return Y_sexp;
}

And in R:

cpp11::cpp_source('hacky_set_attr.cpp')
hacky_set_attr()
# <0 x 0 matrix>
# attr(,"my-attr")
# [1] "hello"

So basically some kind of 'upcasting'.

@pachadotdev
Copy link
Contributor Author

Here's another reply from stackoverflow, @stephematician
https://stackoverflow.com/a/76019987/3720258

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants