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

Allow passing number of rows from outside #272

Merged
merged 5 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# cpp11 (development version)

* New `writable::data_frame` constructor that also takes the number of rows as
input. This accounts for the edge case where the input list has 0 columns but
you'd still like to specify a known number of rows (#272).

* `cpp11::writable::r_vector<T>::iterator` no longer implicitly deletes its
copy assignment operator (#360).

Expand Down
22 changes: 22 additions & 0 deletions cpp11test/src/test-data_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@ context("data_frame-C++") {
expect_true(df.nrow() == 10);
}

test_that("writable::data_frame::nrow works with 0x0 dfs") {
SEXP x = PROTECT(Rf_allocVector(VECSXP, 0));

cpp11::writable::data_frame df(x);
expect_true(df.nrow() == 0);

UNPROTECT(1);
}

test_that("writable::data_frame::nrow works with 10x0 dfs (#272)") {
SEXP x = PROTECT(Rf_allocVector(VECSXP, 0));

bool is_altrep = false;
int nrow = 10;

// Manually specify `nrow` using special constructor
cpp11::writable::data_frame df(x, is_altrep, nrow);
expect_true(df.nrow() == 10);

UNPROTECT(1);
}

test_that("writable::data_frame works") {
using namespace cpp11::literals;
cpp11::writable::data_frame df({"x"_nm = {1, 2, 3}, "y"_nm = {"a", "b", "c"}});
Expand Down
8 changes: 7 additions & 1 deletion inst/include/cpp11/data_frame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ namespace writable {
class data_frame : public cpp11::data_frame {
private:
writable::list set_data_frame_attributes(writable::list&& x) {
x.attr(R_RowNamesSymbol) = {NA_INTEGER, -static_cast<int>(calc_nrow(x))};
return set_data_frame_attributes(std::move(x), calc_nrow(x));
}

writable::list set_data_frame_attributes(writable::list&& x, int nrow) {
x.attr(R_RowNamesSymbol) = {NA_INTEGER, -nrow};
x.attr(R_ClassSymbol) = "data.frame";
return std::move(x);
}
Expand All @@ -76,6 +80,8 @@ class data_frame : public cpp11::data_frame {
data_frame(const SEXP data) : cpp11::data_frame(set_data_frame_attributes(data)) {}
data_frame(const SEXP data, bool is_altrep)
: cpp11::data_frame(set_data_frame_attributes(data), is_altrep) {}
data_frame(const SEXP data, bool is_altrep, int nrow)
: cpp11::data_frame(set_data_frame_attributes(data, nrow), is_altrep) {}
data_frame(std::initializer_list<list> il)
: cpp11::data_frame(set_data_frame_attributes(writable::list(il))) {}
data_frame(std::initializer_list<named_arg> il)
Expand Down
Loading