File tree Expand file tree Collapse file tree 2 files changed +26
-0
lines changed
Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change 55#include " cpp11/integers.hpp"
66#include " cpp11/sexp.hpp"
77#include " cpp11/strings.hpp"
8+ #include " cpp11/logicals.hpp"
89
910#include < testthat.h>
1011
@@ -454,6 +455,22 @@ context("doubles-C++") {
454455 expect_true (!cpp11::is_na (na2[1 ]));
455456 }
456457
458+ test_that (" as_doubles(logicals)" ) {
459+ cpp11::writable::logicals y;
460+
461+ for (int i = 0 ; i < 4 ; i++) {
462+ y.push_back (i % 2 == 0 );
463+ }
464+
465+ cpp11::doubles i (cpp11::as_doubles (y));
466+
467+ expect_true (i[0 ] == 1.0 );
468+ expect_true (i[1 ] == 0.0 );
469+ expect_true (i[2 ] == 1.0 );
470+ expect_true (i[3 ] == 0.0 );
471+ expect_true (cpp11::detail::r_typeof (i) == REALSXP);
472+ }
473+
457474 test_that (" doubles operator[] and at" ) {
458475 cpp11::doubles x (Rf_allocVector (REALSXP, 2 ));
459476 REAL (x)[0 ] = 1 ;
Original file line number Diff line number Diff line change @@ -87,6 +87,15 @@ inline doubles as_doubles(SEXP x) {
8787 return ret;
8888 }
8989
90+ else if (detail::r_typeof (x) == LGLSXP) {
91+ size_t len = Rf_xlength (x);
92+ writable::doubles ret (len);
93+ for (size_t i = 0 ; i < len; ++i) {
94+ ret[i] = LOGICAL (x)[i] == NA_LOGICAL ? NA_REAL : static_cast <double >(LOGICAL (x)[i]);
95+ }
96+ return ret;
97+ }
98+
9099 throw type_error (REALSXP, detail::r_typeof (x));
91100}
92101
You can’t perform that action at this time.
0 commit comments