[Rcpp-devel] data.frame from R to C++?
Dirk Eddelbuettel
edd at debian.org
Fri Apr 8 15:27:26 CEST 2011
On 8 April 2011 at 07:20, Dirk Eddelbuettel wrote:
|
| On 8 April 2011 at 00:27, deqiang sun wrote:
| | Hi all,
| |
| | Suppose I want to read a csv file including TEXT and NUMBER fields in R by function read.table.
| | Is there a way to use the result in C++? Like the way I use in R, say, 2nd column is x[,2], 3rd row is x[3,].
| | I have gone through the examples in RInside and did not find anything helpful.
|
| You will want to look at _Rcpp_ not RInside for these things.
|
| It is also a good idea to check the mailing list archive. Here I quickly
| search Google for "gmane rcpp-devel Rcpp::DataFrame" and found e.g.
|
| http://article.gmane.org/gmane.comp.lang.r.rcpp/1205
| -- returning a data.frame from R to C++
|
| and a bunch more.
|
| But I think you can't pass an entire dataframe from R to C++ _as a single
Actually, you can as a DataFrame is really just a list.
Here is a complete example of passing a data.frame in, accessing each column
and returning. Notice, though, how both the original data gets modified for
int and char (but not date). That was discussed here at length too; we pass
pointers which is why the change propagates. Notice also how I effectively
split the data.frame back up into its columns -- by name.
Dirk
edd at max:/tmp$ cat deqiang.R
library(inline)
D <- data.frame(a=1:3, b=LETTERS[1:3], c=Sys.Date()+0:2, stringsAsFactors=FALSE)
print(D)
src <- '
Rcpp::DataFrame DL = Rcpp::DataFrame(x);
Rcpp::IntegerVector a = DL["a"];
Rcpp::CharacterVector b = DL["b"];
Rcpp::DateVector c = DL["c"];
// do something, et
a[2] = 42;
b[1] = "foo";
c[0] = c[0] + 7; // move up a week
return(Rcpp::List::create(Rcpp::Named("DataFrame")=DL,
Rcpp::Named("IntVec")=a,
Rcpp::Named("CharVec")=b,
Rcpp::Named("DateVec")=c));
'
fun <- cxxfunction(signature(x="misc"), body=src, plugin="Rcpp")
print(fun(D))
edd at max:/tmp$ r deqiang.R
Loading required package: methods
a b c
1 1 A 2011-04-08
2 2 B 2011-04-09
3 3 C 2011-04-10
$DataFrame
a b c
1 1 A 2011-04-08
2 2 foo 2011-04-09
3 42 C 2011-04-10
$IntVec
[1] 1 2 42
$CharVec
[1] "A" "foo" "C"
$DateVec
[1] "2011-04-15" "2011-04-09" "2011-04-10"
edd at max:/tmp$
--
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
More information about the Rcpp-devel
mailing list