Hi everyone!
I am writing a function with cpp11 that, ideally, uses a user provided R function to act on each column of a user provided matrix through a for loop. However, I was not able to extract the values of an entire column to use it in the user provided function. I also couldn't find how to do this after looking cpp11 documentation up.
Reproducible example:
require(cpp11)
cpp11::cpp_source(code='
#include <cpp11.hpp>
using namespace cpp11;
namespace writable = cpp11::writable;
[[cpp11::register]]
sexp test_f(cpp11::doubles_matrix<> x, cpp11::function f) {
int n = x.ncol();
writable::doubles out(n);
cpp11::unwind_protect([&] {
for (int i = 0; i < n; ++i) {
out[i] = f(x[???][i]); // what should I use instead of ???
}
});
return out;
}')
f <- function(x) sum(x) # toy example
test_f(matrix(1.2:9.2, 3, 3), f)
Thanks in advance!