Hi all,
I'm new to this "call C from R" so bear with me.
I'v been writing some C code and compile it to .dll files that can be called
from R.
It was successful until today, when I start a new part of this project.
So, my R code passes arguments to this C code, and these arguments including
some integers and matrices.
it looks like this, and it works fine:
.C("Myfunction", as.matrix(dataset), as.integer(dim(dataset)) )
btw, In the C code, I will read the dataset as double *
Now, I have several datasets that in a list(),
you can think of it as
A = list()
A[[1]] = dataset1
A[[2]] = dataset2
when I run this function again, by using
.C("Myfunction", as.matrix(A[[1]]), as.integer(dim(A[[1]])) )
it does not work anymore. It seems like the C code does not recognize
"as.matrix(A[[1]])" as a pointer to A[[1]]. Anyone knows why is that?
btw, I did some reading on similar issue, and it seems that people use SEXP
more offen to pass arguments from R to C. I tried some of those by trying to
pass the whole list A to the C code and then use something like
REAL(VECTOR_ELT(A, 1))[0] to read each element from the list, however, it
still does not work... if using SEXP as object type is the currect way to do
it, could anyone give an example of how to extract element from the list in
the c code? and also, when I claim the function in C, should it be
SEXP myfunction2(SEXP *list)
or
SEXP myfunction2(SEXP list)
these things are really confusing me....
Thank you for going through this post... Any helps are appreciated.
Ruoqing
--
View this message in context: http://r.789695.n4.nabble.com/pass-an-element-in-a-list-from-R-to-C-tp3900221p3900221.html
Sent from the R help mailing list archive at Nabble.com.
pass an element in a list() from R to C
3 messages · teazrq, William Dunlap
so, I did this:
B= list("a" = 1, "b" = 2, "c" = 3)
.C("myfunction", B)
the c code is :
SEXP myfunction(SEXP matrix_temp)
{
Rprintf("this element is %6.3f", REAL(VECTOR_ELT(matrix_temp,1))[0]);
}
but after runing the R code, it says Error: VECTOR_ELT() can only be applied
to a 'list', not a 'NULL'
I guess this is because I did not acturally pass a list to the c function,
but why is that?
--
View this message in context: http://r.789695.n4.nabble.com/pass-an-element-in-a-list-from-R-to-C-tp3900221p3900426.html
Sent from the R help mailing list archive at Nabble.com.
Functions with prototypes of the form SEXP myfunc(SEXP, SEXP, ..., SEXP) must be called via .Call(), not .C(). Also, you declared myfunction as returning SEXP but returned nothing. Try ending the function with return R_NilValue; You should change the default compiler flags to report all warnings (-Wall if you are using gcc). And you left off a line or two of C code that must have been there, or you code would have compiled due to errors #include <R.h> /* not needed if Rinternals is included */ #include <Rinternals.h> Study 'Writing R Extensions' and work through the examples in it. In particular, look at section 5.9. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of teazrq
Sent: Wednesday, October 12, 2011 7:52 PM
To: r-help at r-project.org
Subject: Re: [R] pass an element in a list() from R to C
so, I did this:
B= list("a" = 1, "b" = 2, "c" = 3)
.C("myfunction", B)
the c code is :
SEXP myfunction(SEXP matrix_temp)
{
Rprintf("this element is %6.3f", REAL(VECTOR_ELT(matrix_temp,1))[0]);
}
but after runing the R code, it says Error: VECTOR_ELT() can only be applied
to a 'list', not a 'NULL'
I guess this is because I did not acturally pass a list to the c function,
but why is that?
--
View this message in context: http://r.789695.n4.nabble.com/pass-an-element-in-a-list-from-R-to-C-
tp3900221p3900426.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.