I am trying to use a custom S4 object in my C code and I cannot get the access to its slots working. The following is a toy example, but even this crashes. In R I have: setClass("pd", representation(data="numeric")) x <- new("pd", data=1:5) test <- function(pd.obj) { res <- .C("TestFun", pd.obj) res} test(x) (Of couse I load the DLL as well.) The corresponding C file has: SEXP TestFun(SEXP pd_obj) { SEXP t=R_NilValue; PROTECT(t = GET_SLOT(pd_obj, install("data"))); UNPROTECT(1); return(t); } What I hope to get as a result is the (1:5) vector. In the long term, the vector will be a multi-dimensional array and I will want to do calculations using its contents in the C program. Thanks for any help, Aniko Huntsman Cancer Institute wishes to promote open communication while protecting confidential and/or privileged information. If you have received this message in error, please inform the sender and delete all copies. [[alternative HTML version deleted]]
You need to use .Call() rather than .C() to pass the R object to the TestFun routine. D. Aniko Szabo wrote:> I am trying to use a custom S4 object in my C code and I cannot get the > access to its slots working. > > > > The following is a toy example, but even this crashes. > > > > In R I have: > > > > setClass("pd", representation(data="numeric")) > > x <- new("pd", data=1:5) > > > > test <- function(pd.obj) { > > res <- .C("TestFun", pd.obj) > > res} > > > > test(x) > > > > (Of couse I load the DLL as well.) > > > > The corresponding C file has: > > > > SEXP TestFun(SEXP pd_obj) > > { > > SEXP t=R_NilValue; > > PROTECT(t = GET_SLOT(pd_obj, install("data"))); > > UNPROTECT(1); > > return(t); > > } > > > > > > What I hope to get as a result is the (1:5) vector. > > In the long term, the vector will be a multi-dimensional array and I > will want to do calculations using its contents in the C program. > > > > Thanks for any help, > > > > Aniko > > > Huntsman Cancer Institute wishes to promote open communication while protecting confidential and/or privileged information. If you have received this message in error, please inform the sender and delete all copies. > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html-- Duncan Temple Lang duncan at wald.ucdavis.edu Department of Statistics work: (530) 752-4782 371 Kerr Hall fax: (530) 752-7099 One Shields Ave. University of California at Davis Davis, CA 95616, USA -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : https://stat.ethz.ch/pipermail/r-help/attachments/20050817/320752cd/attachment.bin
> I am trying to use a custom S4 object in my C code and I cannot get the > access to its slots working. > > > > The following is a toy example, but even this crashes. > > > > In R I have: > > > > setClass("pd", representation(data="numeric")) > > x <- new("pd", data=1:5) > > > > test <- function(pd.obj) { > > res <- .C("TestFun", pd.obj).Call("TestFun", pd.obj) should work. Torsten> > res} > > > > test(x) > > > > (Of couse I load the DLL as well.) > > > > The corresponding C file has: > > > > SEXP TestFun(SEXP pd_obj) > > { > > SEXP t=R_NilValue; > > PROTECT(t = GET_SLOT(pd_obj, install("data"))); > > UNPROTECT(1); > > return(t); > > } > > > > > > What I hope to get as a result is the (1:5) vector. > > In the long term, the vector will be a multi-dimensional array and I > will want to do calculations using its contents in the C program. > > > > Thanks for any help, > > > > Aniko > > > Huntsman Cancer Institute wishes to promote open communication while protecting confidential and/or privileged information. If you have received this message in error, please inform the sender and delete all copies. > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >
On 8/17/05, Aniko Szabo <aniko.szabo at hci.utah.edu> wrote:> I am trying to use a custom S4 object in my C code and I cannot get the > access to its slots working. > > > > The following is a toy example, but even this crashes. > > > > In R I have: > > > > setClass("pd", representation(data="numeric")) > > x <- new("pd", data=1:5) > > > > test <- function(pd.obj) { > > res <- .C("TestFun", pd.obj) > > res} > > > > test(x) > > > > (Of couse I load the DLL as well.) > > > > The corresponding C file has: > > > > SEXP TestFun(SEXP pd_obj) > > { > > SEXP t=R_NilValue; > > PROTECT(t = GET_SLOT(pd_obj, install("data"))); > > UNPROTECT(1); > > return(t); > > }Your C code is set for the .Call interface, not the .C interface. Change the R code to res <- .Call("TestFun", pd.obj)> > > > > > What I hope to get as a result is the (1:5) vector. > > In the long term, the vector will be a multi-dimensional array and I > will want to do calculations using its contents in the C program. > > > > Thanks for any help, > > > > Aniko > > > Huntsman Cancer Institute wishes to promote open communication while protecting confidential and/or privileged information. If you have received this message in error, please inform the sender and delete all copies. > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Hi Aniko, On 17 Aug 2005, aniko.szabo at hci.utah.edu wrote:> I am trying to use a custom S4 object in my C code and I cannot get > the access to its slots working. > > The following is a toy example, but even this crashes. > res <- .C("TestFun", pd.obj)I'm pretty sure you want to use the .Call interface for this sort of thing, not .C. Other than referring you to the Writing R Extensions manual, you might want to look at the Ruuid package in Bioconductor which is quite simple, but demonstrates accessing S4 classes from C. See http://bioconductor.org/ to grab a source package of Ruuid. Best, + seth PS: Questions of this nature (C code type stuff) are better suited to the R-devel mail list.