Skip to content

Commit ffc9ed1

Browse files
committed
Adds size attribute aliases for DataFrame (closes #630)
1 parent 271148a commit ffc9ed1

File tree

5 files changed

+86
-40
lines changed

5 files changed

+86
-40
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2017-01-21 James J Balamuta <[email protected]>
2+
3+
* inst/include/Rcpp/DataFrame.h: Added alias for column and row size
4+
* inst/unitTests/runit.DataFrame.R: Added unit tests for aliases
5+
* inst/unitTests/cpp/DataFrame.cpp: Idem
6+
17
2017-01-17 Dirk Eddelbuettel <[email protected]>
28

39
* DESCRIPTION (Version, Date): Roll minor version and Date

inst/NEWS.Rd

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
\newcommand{\ghpr}{\href{https://github.com/RcppCore/Rcpp/pull/#1}{##1}}
44
\newcommand{\ghit}{\href{https://github.com/RcppCore/Rcpp/issues/#1}{##1}}
55

6+
\section{Changes in Rcpp version 0.13.0 (2017-03-xx)}{
7+
\itemize{
8+
\item Changes in Rcpp API:
9+
\itemize{
10+
\item Added new size attribute aliases for number of rows and columns in
11+
DataFrame (James Balamuta in \ghpr{638} addressing \ghit{630}).
12+
}
13+
}
14+
}
15+
616
\section{Changes in Rcpp version 0.12.9 (2017-01-14)}{
717
\itemize{
818
\item Changes in Rcpp API:
@@ -20,9 +30,9 @@
2030
}
2131
\item Changes in Rcpp Sugar:
2232
\itemize{
23-
\item Added new Sugar function \code{sample()} (Nathan Russell in
33+
\item Added new Sugar function \code{sample()} (Nathan Russell in
2434
\ghpr{610} and \ghpr{616}).
25-
\item Added new Sugar function \code{Arg()} (James Balamuta in
35+
\item Added new Sugar function \code{Arg()} (James Balamuta in
2636
\ghpr{626} addressing \ghit{625}).
2737
}
2838
\item Changes in Rcpp unit tests
@@ -53,8 +63,8 @@
5363
\itemize{
5464
\item String and vector elements now use extended \code{R_xlen_t} indices
5565
(Qiang in PR \ghpr{560})
56-
\item Hashing functions now return unsigned int (Qiang in PR \ghpr{561})
57-
\item Added static methods \code{eye()}, \code{ones()}, and \code{zeros()}
66+
\item Hashing functions now return unsigned int (Qiang in PR \ghpr{561})
67+
\item Added static methods \code{eye()}, \code{ones()}, and \code{zeros()}
5868
for select matrix types (Nathan Russell in PR \ghpr{569})
5969
\item The exception call stack is again correctly reported; print methods
6070
and tests added too (Jim Hester in PR \ghpr{582} fixing \ghit{579})
@@ -66,7 +76,7 @@
6676
\item Changes in Rcpp Sugar:
6777
\itemize{
6878
\item Added new Sugar functions \code{rowSums()}, \code{colSums()},
69-
\code{rowMeans()}, \code{colMeans()} (PR \ghpr{551} by Nathan Russell
79+
\code{rowMeans()}, \code{colMeans()} (PR \ghpr{551} by Nathan Russell
7080
fixing \ghit{549})
7181
\item \code{Range} Sugar now used \code{R_xlen_t} type for start/end
7282
(PR \ghpr{568} by Qiang Kou)
@@ -96,7 +106,7 @@
96106
can activated. We intend to make the new classes the default no sooner
97107
than twelve months from this release.
98108
\item The \code{capabilities()} function can also be used for presence of
99-
this feature
109+
this feature
100110
}
101111
}
102112
}
@@ -106,16 +116,16 @@
106116
\item Changes in Rcpp API:
107117
\itemize{
108118
\item The \code{NORET} macro is now defined if it was not already defined
109-
by R itself (Kevin fixing issue \ghit{512}).
119+
by R itself (Kevin fixing issue \ghit{512}).
110120
\item Environment functions get() & find() now accept a Symbol
111121
(James Balamuta in \ghpr{513} addressing issue \ghit{326}).
112122
\item Several uses of \code{Rf_eval} were replaced by the preferred
113123
\code{Rcpp::Rcpp_eval} (Qiang in PR \ghpr{523} closing \ghit{498}).
114124
\item Improved Autogeneration Warning for RcppExports
115125
(James Balamuta in \ghpr{528} addressing issue \ghit{526}).
116-
\item Fixed invalid C++ prefix identifiers in auto-generated code
126+
\item Fixed invalid C++ prefix identifiers in auto-generated code
117127
(James Balamuta in \ghpr{528} and \ghpr{531} addressing issue
118-
\ghit{387}; Simon Dirmeier in \ghpr{548}).
128+
\ghit{387}; Simon Dirmeier in \ghpr{548}).
119129
\item String constructors now set default UTF-8 encoding (Qiang Kou in
120130
\ghpr{529} fixing \ghit{263}).
121131
\item Add variadic variants of the \code{RCPP_RETURN_VECTOR} and
@@ -130,15 +140,15 @@
130140
}
131141
\item Changes in Rcpp unit tests
132142
\itemize{
133-
\item New unit tests for random number generators the R namespace which
134-
call the standalone Rmath library. (James Balamuta in \ghpr{514}
143+
\item New unit tests for random number generators the R namespace which
144+
call the standalone Rmath library. (James Balamuta in \ghpr{514}
135145
addressing issue \ghit{28}).
136146
}
137147
\item Changes in Rcpp Examples:
138148
\itemize{
139149
\item Examples that used cxxfunction() from the inline package have been
140-
rewritten to use either sourceCpp() or cppFunction()
141-
(James Balamuta in \ghpr{541}, \ghpr{535}, \ghpr{534}, and \ghpr{532}
150+
rewritten to use either sourceCpp() or cppFunction()
151+
(James Balamuta in \ghpr{541}, \ghpr{535}, \ghpr{534}, and \ghpr{532}
142152
addressing issue \ghit{56}).
143153
}
144154
}
@@ -156,9 +166,9 @@
156166
\item String replacement was corrected (Qiang in \ghpr{479} following
157167
mailing list bug report by Masaki Tsuda)
158168
\item Allow for UTF-8 encoding in error messages via
159-
\code{RCPP_USING_UTF8_ERROR_STRING} macro (Qin Wenfeng in \ghpr{493})
169+
\code{RCPP_USING_UTF8_ERROR_STRING} macro (Qin Wenfeng in \ghpr{493})
160170
\item The R function \code{Rf_warningcall} is now provided as well (as
161-
usual without leading \code{Rf_}) (\ghpr{497} fixing \ghit{495})
171+
usual without leading \code{Rf_}) (\ghpr{497} fixing \ghit{495})
162172
}
163173
\item Changes in Rcpp Sugar:
164174
\itemize{
@@ -175,7 +185,7 @@
175185
}
176186
\item Changes in Rcpp Attributes:
177187
\itemize{
178-
\item Add \code{cacheDir} argument to \code{sourceCpp()} to enable caching of
188+
\item Add \code{cacheDir} argument to \code{sourceCpp()} to enable caching of
179189
shared libraries across R sessions (JJ in \ghpr{504}).
180190
\item Code generation now deals correctly which packages containing a dot
181191
in their name (Qiang in \ghpr{501} fixing \ghit{500}).
@@ -194,7 +204,7 @@
194204
}
195205
}
196206
}
197-
207+
198208
\section{Changes in Rcpp version 0.12.5 (2016-05-14)}{
199209
\itemize{
200210
\item Changes in Rcpp API:
@@ -207,7 +217,7 @@
207217
\item Changes in Rcpp Attributes:
208218
\itemize{
209219
\item R 3.3.0 Windows with Rtools 3.3 is now supported (Qin Wenfeng in PR
210-
\ghpr{451}).
220+
\ghpr{451}).
211221
\item Correct handling of dependent file paths on Windows (use winslash = "/").
212222
}
213223
\item Changes in Rcpp Modules:
@@ -227,7 +237,7 @@
227237
}
228238
}
229239
}
230-
240+
231241
\section{Changes in Rcpp version 0.12.4 (2016-03-22)}{
232242
\itemize{
233243
\item Changes in Rcpp API:
@@ -276,13 +286,13 @@
276286
\ghpr{406} by Qiang fixing \ghit{365}).
277287
\item A missing \code{std::hash} function interface for
278288
\code{Rcpp::String} has been addded (PR \ghpr{408} by Qiang fixing
279-
\ghit{84}).
289+
\ghit{84}).
280290
}
281291
\item Changes in Rcpp Attributes:
282292
\itemize{
283293
\item Avoid invalid function names when generating C++ interfaces (PR
284294
\ghpr{403} by JJ fixing \ghit{402}).
285-
\item Insert additional space around \code{&} in function interface (PR
295+
\item Insert additional space around \code{&} in function interface (PR
286296
\ghpr{400} by Kazuki Fukui fixing \ghit{278}).
287297
}
288298
\item Changes in Rcpp Modules:
@@ -306,7 +316,7 @@
306316
by Florian)
307317
\item Before creating a single String object from a \code{SEXP}, ensure
308318
that it is from a vector of length one (PR \ghpr{376} by Dirk, fixing
309-
\ghit{375}).
319+
\ghit{375}).
310320
\item No longer use \code{STRING_ELT} as a left-hand side, thanks to a
311321
heads-up by Luke Tierney (PR \ghpr{378} by Dirk, fixing \ghit{377}).
312322
\item Rcpp Module objects are now checked more carefully (PR \ghpr{381}
@@ -318,7 +328,7 @@
318328
\item \code{operator<<()} now always shows decimal points (PR \ghpr{396}
319329
by Dan)
320330
\item Matrix classes now have a \code{transpose()} function (PR \ghpr{397}
321-
by Dirk fixing \ghit{383})
331+
by Dirk fixing \ghit{383})
322332
\item \code{operator<<()} for complex types was added (PRs \ghpr{398} by
323333
Qiang and \ghpr{399} by Dirk, fixing \ghit{187})
324334
}
@@ -336,20 +346,20 @@
336346
\item Changes in Rcpp Documentation:
337347
\itemize{
338348
\item The \code{NEWS} file now links to GitHub issue tickets and pull
339-
requests.
349+
requests.
340350
\item The \code{Rcpp.bib} file with bibliographic references was updated.
341351
}
342352
}
343353
}
344-
354+
345355
\section{Changes in Rcpp version 0.12.1 (2015-09-10)}{
346356
\itemize{
347357
\item Changes in Rcpp API:
348358
\itemize{
349-
\item Correct use of WIN32 instead of _WIN32 to please Windows 10
359+
\item Correct use of WIN32 instead of _WIN32 to please Windows 10
350360
\item Add an assignment operator to \code{DimNameProxy} (PR \ghpr{339} by Florian)
351361
\item Add vector and matrix accessors \code{.at()} with bounds checking
352-
(PR \ghpr{342} by Florian)
362+
(PR \ghpr{342} by Florian)
353363
\item Correct character vector conversion from single char (PR \ghpr{344} by
354364
Florian fixing issue \ghit{343})
355365
\item Correct on use of \code{R_xlen_t} back to \code{size_t} (PR \ghpr{348} by
@@ -397,13 +407,13 @@
397407
R expressions; this should resolve errors where calling handlers (e.g.
398408
through \code{suppressMessages()}) were not properly respected.
399409
\item All internal length variables have been changed from \code{R_len_t}
400-
to \code{R_xlen_t} to support vectors longer than 2^31-1 elements (via
410+
to \code{R_xlen_t} to support vectors longer than 2^31-1 elements (via
401411
PR \ghpr{303} by Qiang Kou).
402412
\item The sugar function \code{sapply} now supports lambda functions
403413
(addressing \ghit{213} thanks to Matt Dziubinski)
404414
\item The \code{var} sugar function now uses a more robust two-pass
405415
method, supports complex numbers, with new unit tests added (via PR
406-
\ghpr{320} by Matt Dziubinski)
416+
\ghpr{320} by Matt Dziubinski)
407417
\item \code{String} constructors now allow encodings (via PR \ghpr{310}
408418
by Qiang Kou)
409419
\item \code{String} objects are preserving the underlying \code{SEXP}
@@ -412,7 +422,7 @@
412422
\item DataFrame constructors are now a little more careful (via PR
413423
\ghpr{301} by Romain Francois)
414424
\item For R 3.2.0 or newer, \code{Rf_installChar()} is used instead of
415-
\code{Rf_install(CHAR())} (via PR \ghpr{332}).
425+
\code{Rf_install(CHAR())} (via PR \ghpr{332}).
416426
}
417427
\item Changes in Rcpp Attributes:
418428
\itemize{
@@ -444,7 +454,7 @@
444454
matrices (via a pull request by Dmitrii Meleshko).
445455
\item A new \code{empty()} string constructor was added (via another pull
446456
request).
447-
\item Better support for Vectors with a storage policy different from the
457+
\item Better support for Vectors with a storage policy different from the
448458
default, i.e. \code{NoProtectStorage}, was added.
449459
}
450460
\item Changes in Rcpp Attributes:
@@ -469,16 +479,16 @@
469479
\item The \code{Rcpp::Environment} constructor can now use a supplied
470480
parent environment.
471481
\item The \code{Rcpp::Function} constructor can now use a supplied
472-
environment or namespace.
482+
environment or namespace.
473483
\item The \code{attributes_hidden} macro from R is used to shield internal
474484
functions; the \code{R_ext/Visibility.h} header is now included as well.
475-
\item A \code{Rcpp::print} function was added as a wrapper around \code{Rf_PrintValue}.
485+
\item A \code{Rcpp::print} function was added as a wrapper around \code{Rf_PrintValue}.
476486
}
477487
\item Changes in Rcpp Attributes:
478488
\itemize{
479489
\item The \code{pkg_types.h} file is now included in \code{RcppExports.cpp}
480-
if it is present in either the \code{inst/include} or \code{src}.
481-
\item \code{sourceCpp} was modified to allow includes of local files
490+
if it is present in either the \code{inst/include} or \code{src}.
491+
\item \code{sourceCpp} was modified to allow includes of local files
482492
(e.g. \code{#include "foo.hpp"}). Implementation files (*.cc; *.cpp) corresponding
483493
to local includes are also automatically built if they exist.
484494
\item The generated attributes code was simplified with respect to

inst/include/Rcpp/DataFrame.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ namespace Rcpp{
6666
// getAttrib(df, R_RowNamesSymbol) will force an expansion of that
6767
// compact form thereby allocating a huge vector when we just want
6868
// the row.names. Hence this workaround.
69-
inline int nrows() const {
69+
inline int nrow() const {
7070
SEXP rn = R_NilValue ;
7171
SEXP att = ATTRIB( Parent::get__() ) ;
7272
while( att != R_NilValue ){
@@ -83,6 +83,26 @@ namespace Rcpp{
8383
return LENGTH(rn);
8484
}
8585

86+
// Leave in place for a year
87+
// before making defunct
88+
inline int nrows() const {
89+
return DataFrame_Impl::nrow();
90+
}
91+
92+
// Adds size attribute accessors that mimic
93+
// those available in R and the Matrix class
94+
inline int rows() const {
95+
return DataFrame_Impl::nrow();
96+
}
97+
98+
inline int ncol() const {
99+
return DataFrame_Impl::length();
100+
}
101+
102+
inline int cols() const {
103+
return DataFrame_Impl::length();
104+
}
105+
86106
static DataFrame_Impl create(){
87107
return DataFrame_Impl() ;
88108
}

inst/unitTests/cpp/DataFrame.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ DataFrame createTwoStringsAsFactors(){
8585
}
8686

8787
// [[Rcpp::export]]
88-
int DataFrame_nrows( DataFrame df){
89-
return df.nrows() ;
88+
IntegerVector DataFrame_nrow( DataFrame df){
89+
return IntegerVector::create(df.nrow(), df.rows()) ;
9090
}
9191

92+
93+
// [[Rcpp::export]]
94+
IntegerVector DataFrame_ncol( DataFrame df){
95+
return IntegerVector::create(df.ncol(), df.cols());
96+
}

inst/unitTests/runit.DataFrame.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,14 @@ if (.runThisTest) {
7676
checkEquals( createTwoStringsAsFactors(), DF, msg = "DataFrame create2 stringsAsFactors = false")
7777
}
7878

79-
test.DataFrame.nrows <- function(){
79+
test.DataFrame.nrow <- function(){
8080
df <- data.frame( x = 1:10, y = 1:10 )
81-
checkEquals( DataFrame_nrows( df ), nrow(df) )
81+
checkEquals( DataFrame_nrow( df ), rep(nrow(df), 2) )
82+
}
83+
84+
test.DataFrame.ncol <- function(){
85+
df <- data.frame( x = 1:10, y = 1:10 )
86+
checkEquals( DataFrame_ncol( df ), rep(ncol(df), 2) )
8287
}
8388

8489

0 commit comments

Comments
 (0)