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
24suppressMessages(library(Rcpp))
25suppressMessages(library(inline))
29 double d = Rcpp::as<double>(x);
30 std::cout << "Returning twice the value of " << d << " : ";
31 return(Rcpp::wrap( 2*d ) );
33funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
34cat(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 ) );
46funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
49funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE)
50cat(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 ) );
58funx <- cfunction(signature(x="character"), foo, Rcpp=TRUE, verbose=FALSE)
59cat(funx(x="abc"), "\n")
61cat("\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) ) );
67funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
69funx <- cfunction(signature(x="integer"), foo, Rcpp=TRUE, verbose=FALSE)
71funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE)
72cat( funx(x=as.raw(2)), "\n")
74cat("\n=== logical \n")
76bool b = Rcpp::as<bool>(x);
77std::cout << "flip " << ( b ? "TRUE" : "FALSE" ) << " : ";
78return(Rcpp::wrap( !b ));
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)
95cat("\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));
104funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
106funx <- cfunction(signature(x="integer"), foo, Rcpp=TRUE, verbose=FALSE)
108funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE)
109print(funx(x=as.raw(2:5)))
111cat("\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 ) );
120funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
122funx <- cfunction(signature(x="integer"), foo, Rcpp=TRUE, verbose=FALSE)
124funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE)
125print(funx(x=as.raw(2:5)))
128cat("\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 ));
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)
141funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE)
142print(funx(x=as.raw(2:5)))
144cat("\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 ));
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)
157funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
160cat("\n=== vector<bool>\n")
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++) {
167return(Rcpp::wrap( bv ));
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)
175funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
176print(funx(x=as.numeric(0:9)))
179cat("\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 ));
188funx <- cfunction(signature(x="character"), foo, Rcpp=TRUE, verbose=FALSE)
189print(funx(x=c("foo", "bar")))
192cat("\n=== set<int>\n")
198return Rcpp::wrap( iv );'
199funx <- cfunction(signature(), foo, Rcpp=TRUE, verbose=FALSE, includes = "#include <set>" )
201stopifnot( identical( res, 0:1 ) )
203cat("\n=== set<double>\n")
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)))
214cat("\n=== set<raw>\n")
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)))
225cat("\n=== set<string> \n")
227std::set<std::string> ss ;
231return(Rcpp::wrap( ss )); '
232funx <- cfunction(signature(), foo, Rcpp=TRUE, verbose=FALSE, include = "#include <set>" )
233print( res <- funx() )
234stopifnot( identical( res, c("bar","foo")) )
240 signature(x="data.frame"), '
241std::vector<std::string> iv = Rcpp::RObject(x).attributeNames();
242return(Rcpp::wrap( iv ));
243', Rcpp=TRUE, verbose=FALSE)
245stopifnot( all( c("names", "row.names", "class" ) %in% res ) )
247funx <- cfunction(signature(x="data.frame"), '
248bool has_class = Rcpp::RObject(x).hasAttribute( "class" ) ;
249return Rcpp::wrap( has_class ) ;
250', Rcpp=TRUE, verbose=FALSE)
254funx <- cfunction(signature(x="data.frame"), '
255return Rcpp::RObject(x).attr( "row.names" ) ;
256', Rcpp=TRUE, verbose=FALSE)
258stopifnot( identical(res, 1:150) )
261funx <- cfunction(signature(x="ANY"), '
262bool is_null = Rcpp::RObject(x).isNULL() ;
263return Rcpp::wrap( is_null ) ;
264', Rcpp=TRUE, verbose=FALSE)