Skip to content

Commit aad23da

Browse files
authored
Merge pull request #783 from RcppCore/bugfix/issue781
newDate(time)Vector gets is_na() (closes #781)
2 parents 41ee833 + f2fdc81 commit aad23da

File tree

5 files changed

+54
-9
lines changed

5 files changed

+54
-9
lines changed

ChangeLog

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
2017-12-03 Dirk Eddelbuettel <[email protected]>
2+
3+
* inst/include/Rcpp/sugar/functions/is_na.h: Correct test for NA to work
4+
with new as well as old Date(time)Vectors
5+
6+
* inst/unitTests/cpp/dates.cpp (has_na): Added tests
7+
* inst/unitTests/runit.Date.R (test.NA): Idem
8+
19
2017-11-30 James J Balamuta <[email protected]>
210

311
* src/attributes.cpp: Fixed missing Rcpp namespace in export interface

inst/include/Rcpp/date_datetime/date_datetime.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// date_datetime.h: Rcpp R/C++ interface class library -- Date and Datetime support
44
//
5-
// Copyright (C) 2016 Dirk Eddelbuettel
5+
// Copyright (C) 2016 - 2017 Dirk Eddelbuettel
66
//
77
// This file is part of Rcpp.
88
//
@@ -32,7 +32,7 @@
3232

3333
namespace Rcpp {
3434

35-
// this will not be on by default
35+
// this is on by default since Rcpp 0.12.14
3636
#if defined(RCPP_NEW_DATE_DATETIME_VECTORS)
3737

3838
typedef newDateVector DateVector;

inst/include/Rcpp/sugar/functions/is_na.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ class IsNa_Vector_is_na : public Rcpp::VectorBase<LGLSXP, false, IsNa_Vector_is_
7272
IsNa_Vector_is_na( const T& x) : ref(x){}
7373

7474
inline int operator[]( R_xlen_t i) const {
75+
#if defined(RCPP_NEW_DATE_DATETIME_VECTORS)
76+
return ::Rcpp::traits::is_na<T>(ref[i]);
77+
#else
7578
return ref[i].is_na() ;
79+
#endif
7680
}
7781

7882
inline R_xlen_t size() const { return ref.size() ; }
@@ -84,16 +88,25 @@ class IsNa_Vector_is_na : public Rcpp::VectorBase<LGLSXP, false, IsNa_Vector_is_
8488
} // sugar
8589

8690
template <int RTYPE, bool NA, typename T>
87-
inline sugar::IsNa<RTYPE,NA,T> is_na( const Rcpp::VectorBase<RTYPE,NA,T>& t){
88-
return sugar::IsNa<RTYPE,NA,T>( t ) ;
91+
inline sugar::IsNa<RTYPE,NA,T> is_na( const Rcpp::VectorBase<RTYPE,NA,T>& t) {
92+
return sugar::IsNa<RTYPE,NA,T>(t);
8993
}
9094

91-
inline sugar::IsNa_Vector_is_na<DatetimeVector> is_na( const DatetimeVector& x){
92-
return sugar::IsNa_Vector_is_na<DatetimeVector>( x ) ;
95+
inline sugar::IsNa_Vector_is_na<oldDatetimeVector> is_na(const oldDatetimeVector& x) {
96+
return sugar::IsNa_Vector_is_na<oldDatetimeVector>( x ) ;
9397
}
94-
inline sugar::IsNa_Vector_is_na<DateVector> is_na( const DateVector& x){
95-
return sugar::IsNa_Vector_is_na<DateVector>( x ) ;
98+
99+
inline sugar::IsNa_Vector_is_na<oldDateVector> is_na(const oldDateVector& x) {
100+
return sugar::IsNa_Vector_is_na<oldDateVector>(x);
101+
}
102+
103+
inline sugar::IsNa_Vector_is_na<NumericVector> is_na(newDatetimeVector& x) {
104+
return sugar::IsNa_Vector_is_na<NumericVector>(x);
96105
}
106+
inline sugar::IsNa_Vector_is_na<NumericVector> is_na(newDateVector& x) {
107+
return sugar::IsNa_Vector_is_na<NumericVector>(x);
108+
}
109+
97110

98111
} // Rcpp
99112
#endif

inst/unitTests/cpp/dates.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,15 @@ Date test_gmtime(double d) {
234234
Date nd(tm.tm_year, tm.tm_mon + 1, tm.tm_mday);
235235
return nd;
236236
}
237+
238+
// [[Rcpp::export]]
239+
bool has_na_dv(const Rcpp::DateVector d) {
240+
return Rcpp::is_true(Rcpp::any(Rcpp::is_na(d)));
241+
}
242+
243+
// [[Rcpp::export]]
244+
bool has_na_dtv(const Rcpp::DatetimeVector d) {
245+
return Rcpp::is_true(Rcpp::any(Rcpp::is_na(d)));
246+
}
247+
248+

inst/unitTests/runit.Date.R

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env r
22
# -*- mode: R; tab-width: 4; -*-
33
#
4-
# Copyright (C) 2010 - 2016 Dirk Eddelbuettel and Romain Francois
4+
# Copyright (C) 2010 - 2017 Dirk Eddelbuettel and Romain Francois
55
#
66
# This file is part of Rcpp.
77
#
@@ -264,4 +264,16 @@ if (.runThisTest) {
264264
checkEquals(test_gmtime(-489024000), as.Date("1954-07-04"), msg="Date.test_gmtime.1954")
265265
Sys.setenv(TZ=oldTZ)
266266
}
267+
268+
test.NA <- function() {
269+
dv <- Sys.Date() + 0:2
270+
checkTrue(has_na_dv(dv) == FALSE, msg="DateVector.NAtest.withoutNA")
271+
dv[1] <- NA
272+
checkTrue(has_na_dv(dv) == TRUE, msg="DateVector.NAtest.withNA")
273+
274+
dvt <- Sys.time() + 0:2
275+
checkTrue(has_na_dtv(dvt) == FALSE, msg="DatetimeVector.NAtest.withoutNA")
276+
dvt[1] <- NA
277+
checkTrue(has_na_dtv(dvt) == TRUE, msg="DatetimeVector.NAtest.withNA")
278+
}
267279
}

0 commit comments

Comments
 (0)