FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Allow passing number of rows from outside by krlmlr · Pull Request #272 · r-lib/cpp11 · GitHub

/ cpp11 Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cpp  (1) .hpp  (1) .md  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
4 changes: 4 additions & 0 deletions NEWS.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL