3 # Copyright (C) 2009 - 2010 Dirk Eddelbuettel and Romain Francois
5 # This file is part of Rcpp.
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.
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.
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/>.
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
24 suppressMessages(library(Rcpp))
25 suppressMessages(library(inline))
29 double d = Rcpp::as<double>(x);
30 std::cout << "Returning twice the value of " << d << " : ";
31 return(Rcpp::wrap( 2*d ) );
33 funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
34 cat(funx(x=2.123), "\n")
36 ##funx(x='2') ## throws as expected
37 ##funx(x=2:3) ## throws as expected
42 int i = Rcpp::as<int>(x);
43 std::cout << "Returning twice the value of " << i << " : ";
44 return(Rcpp::wrap( 2*i ) );
46 funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
48 cat(funx(x=2.2), "\n")
49 funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE)
50 cat(funx(x=as.raw(2)), "\n")
54 std::string s = Rcpp::as<std::string>(x);
55 std::cout << "Returning twice the value of " << s << " : ";
56 return(Rcpp::wrap( s+s ) );
58 funx <- cfunction(signature(x="character"), foo, Rcpp=TRUE, verbose=FALSE)
59 cat(funx(x="abc"), "\n")
61 cat("\n===Raw (bytes)\n")
63 Rbyte i = Rcpp::as<Rbyte>(x) ;
64 std::cout << "Returning twice the value of " << (int)i << " : ";
65 return(Rcpp::wrap( (Rbyte)(2*i) ) );
67 funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
69 funx <- cfunction(signature(x="integer"), foo, Rcpp=TRUE, verbose=FALSE)
70 cat( funx(x=2L), "\n")
71 funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE)
72 cat( funx(x=as.raw(2)), "\n")
74 cat("\n=== logical \n")
76 bool b = Rcpp::as<bool>(x);
77 std::cout << "flip " << ( b ? "TRUE" : "FALSE" ) << " : ";
78 return(Rcpp::wrap( !b ));
80 funx <- cfunction(signature(x="logical"), foo, Rcpp=TRUE, verbose=FALSE)
81 cat( res <- funx(x=TRUE) , "\n") ; stopifnot( !res )
82 cat( res <- funx(x=FALSE), "\n" ) ; stopifnot( res)
83 funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
84 cat( res <- funx(x=2) , "\n") ; stopifnot( !res )
85 cat( res <- funx(x=0.0), "\n") ; stopifnot( res)
86 funx <- cfunction(signature(x="integer"), foo, Rcpp=TRUE, verbose=FALSE)
87 cat( res <- funx(x=2L), "\n") ; stopifnot( !res )
88 cat( res <- funx(x=0L), "\n") ; stopifnot( res)
89 funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE)
90 cat( res <- funx(x=as.raw(2)), "\n") ; stopifnot( !res )
91 cat( res <- funx(x=as.raw(0)), "\n") ; stopifnot( res)
95 cat("\n===Int Vector via wrap\n")
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++) {
102 return(Rcpp::wrap(iv));
104 funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
106 funx <- cfunction(signature(x="integer"), foo, Rcpp=TRUE, verbose=FALSE)
108 funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE)
109 print(funx(x=as.raw(2:5)))
111 cat("\n===Int Vector\n")
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++) {
118 return(Rcpp::wrap( iv ) );
120 funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
121 print(funx(x=2:5+.1))
122 funx <- cfunction(signature(x="integer"), foo, Rcpp=TRUE, verbose=FALSE)
124 funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE)
125 print(funx(x=as.raw(2:5)))
128 cat("\n===Double Vector\n")
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++) {
135 return(Rcpp::wrap( iv ));
137 funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
138 print(funx(x=0.1+2:5))
139 funx <- cfunction(signature(x="integer"), foo, Rcpp=TRUE, verbose=FALSE)
141 funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE)
142 print(funx(x=as.raw(2:5)))
144 cat("\n===Raw Vector\n")
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++) {
151 return(Rcpp::wrap( iv ));
153 funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE)
154 print(funx(x=as.raw(0:9)))
155 funx <- cfunction(signature(x="integer"), foo, Rcpp=TRUE, verbose=FALSE)
157 funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
158 print(funx(x=0:9+.1))
160 cat("\n=== vector<bool>\n")
162 std::vector<bool> bv = Rcpp::as< std::vector<bool> >(x) ;
163 std::cout << "Flip the value of vector : ";
164 for (size_t i=0; i<bv.size(); i++) {
167 return(Rcpp::wrap( bv ));
169 funx <- cfunction(signature(x="logical"), foo, Rcpp=TRUE, verbose=FALSE)
170 print(funx(x=c(TRUE,FALSE)))
171 funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE)
172 print(funx(x=as.raw(0:9)))
173 funx <- cfunction(signature(x="integer"), foo, Rcpp=TRUE, verbose=FALSE)
175 funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
176 print(funx(x=as.numeric(0:9)))
179 cat("\n===String Vector\n")
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];
186 return(Rcpp::wrap( iv ));
188 funx <- cfunction(signature(x="character"), foo, Rcpp=TRUE, verbose=FALSE)
189 print(funx(x=c("foo", "bar")))
192 cat("\n=== set<int>\n")
198 return Rcpp::wrap( iv );'
199 funx <- cfunction(signature(), foo, Rcpp=TRUE, verbose=FALSE, includes = "#include <set>" )
201 stopifnot( identical( res, 0:1 ) )
203 cat("\n=== set<double>\n")
209 return(Rcpp::wrap( ds )); '
210 funx <- cfunction(signature(), foo, Rcpp=TRUE, verbose=FALSE, includes = "#include <set>")
211 print( res <- funx() )
212 stopifnot( identical( res, as.numeric(0:1)))
214 cat("\n=== set<raw>\n")
217 bs.insert( (Rbyte)0 ) ;
218 bs.insert( (Rbyte)1 ) ;
219 bs.insert( (Rbyte)0 ) ;
220 return(Rcpp::wrap( bs )); '
221 funx <- cfunction(signature(), foo, Rcpp=TRUE, verbose=FALSE, includes = "#include <set>")
222 print( res <- funx() )
223 stopifnot( identical( res, as.raw(0:1)))
225 cat("\n=== set<string> \n")
227 std::set<std::string> ss ;
231 return(Rcpp::wrap( ss )); '
232 funx <- cfunction(signature(), foo, Rcpp=TRUE, verbose=FALSE, include = "#include <set>" )
233 print( res <- funx() )
234 stopifnot( identical( res, c("bar","foo")) )
237 #========= attributes
240 signature(x="data.frame"), '
241 std::vector<std::string> iv = Rcpp::RObject(x).attributeNames();
242 return(Rcpp::wrap( iv ));
243 ', Rcpp=TRUE, verbose=FALSE)
245 stopifnot( all( c("names", "row.names", "class" ) %in% res ) )
247 funx <- cfunction(signature(x="data.frame"), '
248 bool has_class = Rcpp::RObject(x).hasAttribute( "class" ) ;
249 return Rcpp::wrap( has_class ) ;
250 ', Rcpp=TRUE, verbose=FALSE)
254 funx <- cfunction(signature(x="data.frame"), '
255 return Rcpp::RObject(x).attr( "row.names" ) ;
256 ', Rcpp=TRUE, verbose=FALSE)
258 stopifnot( identical(res, 1:150) )
261 funx <- cfunction(signature(x="ANY"), '
262 bool is_null = Rcpp::RObject(x).isNULL() ;
263 return Rcpp::wrap( is_null ) ;
264 ', Rcpp=TRUE, verbose=FALSE)