Rcpp Version 1.0.14
Loading...
Searching...
No Matches
RObject.r
Go to the documentation of this file.
1#!/usr/bin/env r
2#
3# Copyright (C) 2009 - 2010 Dirk Eddelbuettel and Romain Francois
4#
5# This file is part of Rcpp.
6#
7# Rcpp is free software: you can redistribute it and/or modify it
8# under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 2 of the License, or
10# (at your option) any later version.
11#
12# Rcpp is distributed in the hope that it will be useful, but
13# WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
19
20## NB This file is mostly historic and predates the unit tests.
21## Yet it still provides useful examples -- but the unitTests/
22## for vastly larger coverage
23
24suppressMessages(library(Rcpp))
25suppressMessages(library(inline))
26
27cat("===Doubles\n")
28foo <- '
29 double d = Rcpp::as<double>(x);
30 std::cout << "Returning twice the value of " << d << " : ";
31 return(Rcpp::wrap( 2*d ) );
32 '
33funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
34cat(funx(x=2.123), "\n")
35cat(funx(x=2), "\n")
36##funx(x='2') ## throws as expected
37##funx(x=2:3) ## throws as expected
38
39
40cat("\n===Int\n")
41foo <- '
42 int i = Rcpp::as<int>(x);
43 std::cout << "Returning twice the value of " << i << " : ";
44 return(Rcpp::wrap( 2*i ) );
45 '
46funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
47cat(funx(x=2), "\n")
48cat(funx(x=2.2), "\n")
49funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE)
50cat(funx(x=as.raw(2)), "\n")
51
52cat("\n===String\n")
53foo <- '
54 std::string s = Rcpp::as<std::string>(x);
55 std::cout << "Returning twice the value of " << s << " : ";
56 return(Rcpp::wrap( s+s ) );
57 '
58funx <- cfunction(signature(x="character"), foo, Rcpp=TRUE, verbose=FALSE)
59cat(funx(x="abc"), "\n")
60
61cat("\n===Raw (bytes)\n")
62foo <- '
63 Rbyte i = Rcpp::as<Rbyte>(x) ;
64 std::cout << "Returning twice the value of " << (int)i << " : ";
65 return(Rcpp::wrap( (Rbyte)(2*i) ) );
66 '
67funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
68cat( funx(x=2), "\n")
69funx <- cfunction(signature(x="integer"), foo, Rcpp=TRUE, verbose=FALSE)
70cat( funx(x=2L), "\n")
71funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE)
72cat( funx(x=as.raw(2)), "\n")
73
74cat("\n=== logical \n")
75foo <- '
76bool b = Rcpp::as<bool>(x);
77std::cout << "flip " << ( b ? "TRUE" : "FALSE" ) << " : ";
78return(Rcpp::wrap( !b ));
79'
80funx <- cfunction(signature(x="logical"), foo, Rcpp=TRUE, verbose=FALSE)
81cat( res <- funx(x=TRUE) , "\n") ; stopifnot( !res )
82cat( res <- funx(x=FALSE), "\n" ) ; stopifnot( res)
83funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
84cat( res <- funx(x=2) , "\n") ; stopifnot( !res )
85cat( res <- funx(x=0.0), "\n") ; stopifnot( res)
86funx <- cfunction(signature(x="integer"), foo, Rcpp=TRUE, verbose=FALSE)
87cat( res <- funx(x=2L), "\n") ; stopifnot( !res )
88cat( res <- funx(x=0L), "\n") ; stopifnot( res)
89funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE)
90cat( res <- funx(x=as.raw(2)), "\n") ; stopifnot( !res )
91cat( res <- funx(x=as.raw(0)), "\n") ; stopifnot( res)
92
93### vectors
94
95cat("\n===Int Vector via wrap\n")
96foo <- '
97 std::vector<int> iv = Rcpp::as< std::vector<int> >(x) ;
98 std::cout << "Returning twice the value of vector : ";
99 for (size_t i=0; i<iv.size(); i++) {
100 iv[i] = 2*iv[i];
101 }
102 return(Rcpp::wrap(iv));
103 '
104funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
105print(funx(x=2:5))
106funx <- cfunction(signature(x="integer"), foo, Rcpp=TRUE, verbose=FALSE)
107print(funx(x=2:5))
108funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE)
109print(funx(x=as.raw(2:5)))
110
111cat("\n===Int Vector\n")
112foo <- '
113 std::vector<int> iv = Rcpp::as< std::vector<int> >(x) ;
114 std::cout << "Returning twice the value of vector : ";
115 for (size_t i=0; i<iv.size(); i++) {
116 iv[i] = 2*iv[i];
117 }
118 return(Rcpp::wrap( iv ) );
119 '
120funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
121print(funx(x=2:5+.1))
122funx <- cfunction(signature(x="integer"), foo, Rcpp=TRUE, verbose=FALSE)
123print(funx(x=2:5))
124funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE)
125print(funx(x=as.raw(2:5)))
126
127
128cat("\n===Double Vector\n")
129foo <- '
130 std::vector<double> iv = Rcpp::as< std::vector<double> >(x) ;
131 std::cout << "Returning twice the value of vector : ";
132 for (size_t i=0; i<iv.size(); i++) {
133 iv[i] = 2*iv[i];
134 }
135 return(Rcpp::wrap( iv ));
136 '
137funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
138print(funx(x=0.1+2:5))
139funx <- cfunction(signature(x="integer"), foo, Rcpp=TRUE, verbose=FALSE)
140print(funx(x=2:5))
141funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE)
142print(funx(x=as.raw(2:5)))
143
144cat("\n===Raw Vector\n")
145foo <- '
146 std::vector<Rbyte> iv = Rcpp::as< std::vector<Rbyte> >(x) ;
147 std::cout << "Returning twice the value of vector : ";
148 for (size_t i=0; i<iv.size(); i++) {
149 iv[i] = 2*iv[i];
150 }
151 return(Rcpp::wrap( iv ));
152 '
153funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE)
154print(funx(x=as.raw(0:9)))
155funx <- cfunction(signature(x="integer"), foo, Rcpp=TRUE, verbose=FALSE)
156print(funx(x=0:9))
157funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
158print(funx(x=0:9+.1))
159
160cat("\n=== vector<bool>\n")
161foo <- '
162std::vector<bool> bv = Rcpp::as< std::vector<bool> >(x) ;
163std::cout << "Flip the value of vector : ";
164for (size_t i=0; i<bv.size(); i++) {
165 bv[i].flip() ;
166}
167return(Rcpp::wrap( bv ));
168'
169funx <- cfunction(signature(x="logical"), foo, Rcpp=TRUE, verbose=FALSE)
170print(funx(x=c(TRUE,FALSE)))
171funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE)
172print(funx(x=as.raw(0:9)))
173funx <- cfunction(signature(x="integer"), foo, Rcpp=TRUE, verbose=FALSE)
174print(funx(x=0:9))
175funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
176print(funx(x=as.numeric(0:9)))
177
178
179cat("\n===String Vector\n")
180foo <- '
181 std::vector<std::string> iv = Rcpp::as< std::vector<std::string> >(x);
182 std::cout << "Returning twice the value of vector : ";
183 for (size_t i=0; i<iv.size(); i++) {
184 iv[i] = iv[i] + iv[i];
185 }
186 return(Rcpp::wrap( iv ));
187 '
188funx <- cfunction(signature(x="character"), foo, Rcpp=TRUE, verbose=FALSE)
189print(funx(x=c("foo", "bar")))
190
191### using std::set
192cat("\n=== set<int>\n")
193foo <- '
194std::set<int> iv ;
195iv.insert( 0 ) ;
196iv.insert( 1 ) ;
197iv.insert( 0 ) ;
198return Rcpp::wrap( iv );'
199funx <- cfunction(signature(), foo, Rcpp=TRUE, verbose=FALSE, includes = "#include <set>" )
200print(res <- funx())
201stopifnot( identical( res, 0:1 ) )
202
203cat("\n=== set<double>\n")
204foo <- '
205std::set<double> ds;
206ds.insert( 0.0 );
207ds.insert( 1.0 );
208ds.insert( 0.0 );
209return(Rcpp::wrap( ds )); '
210funx <- cfunction(signature(), foo, Rcpp=TRUE, verbose=FALSE, includes = "#include <set>")
211print( res <- funx() )
212stopifnot( identical( res, as.numeric(0:1)))
213
214cat("\n=== set<raw>\n")
215foo <- '
216std::set<Rbyte> bs ;
217bs.insert( (Rbyte)0 ) ;
218bs.insert( (Rbyte)1 ) ;
219bs.insert( (Rbyte)0 ) ;
220return(Rcpp::wrap( bs )); '
221funx <- cfunction(signature(), foo, Rcpp=TRUE, verbose=FALSE, includes = "#include <set>")
222print( res <- funx() )
223stopifnot( identical( res, as.raw(0:1)))
224
225cat("\n=== set<string> \n")
226foo <- '
227std::set<std::string> ss ;
228ss.insert( "foo" ) ;
229ss.insert( "bar" ) ;
230ss.insert( "foo" ) ;
231return(Rcpp::wrap( ss )); '
232funx <- cfunction(signature(), foo, Rcpp=TRUE, verbose=FALSE, include = "#include <set>" )
233print( res <- funx() )
234stopifnot( identical( res, c("bar","foo")) )
235
236
237#========= attributes
238
239funx <- cfunction(
240 signature(x="data.frame"), '
241std::vector<std::string> iv = Rcpp::RObject(x).attributeNames();
242return(Rcpp::wrap( iv ));
243', Rcpp=TRUE, verbose=FALSE)
244res <- funx( iris )
245stopifnot( all( c("names", "row.names", "class" ) %in% res ) )
246
247funx <- cfunction(signature(x="data.frame"), '
248bool has_class = Rcpp::RObject(x).hasAttribute( "class" ) ;
249return Rcpp::wrap( has_class ) ;
250', Rcpp=TRUE, verbose=FALSE)
251res <- funx( iris )
252stopifnot( res )
253
254funx <- cfunction(signature(x="data.frame"), '
255return Rcpp::RObject(x).attr( "row.names" ) ;
256', Rcpp=TRUE, verbose=FALSE)
257res <- funx( iris )
258stopifnot( identical(res, 1:150) )
259
260#============ NULL
261funx <- cfunction(signature(x="ANY"), '
262bool is_null = Rcpp::RObject(x).isNULL() ;
263return Rcpp::wrap( is_null ) ;
264', Rcpp=TRUE, verbose=FALSE)
265res <- funx( iris )
266stopifnot( !res )
267res <- funx( NULL )
268stopifnot( res )
269
270
271