[R] Problem with .C

Jan van der Laan rhelp at eoos.dds.nl
Thu Oct 6 14:51:05 CEST 2011


An obvious reason might be that your second argument should be a  
pointer to int.

As others have mentioned, you might want to have a look at Rccp and/or  
inline. The documentation is good and I find it much easier to work  
with.

For example, your example could be written as:

library(Rcpp)
library(inline)

test <- cxxfunction(signature(x = "numeric" ) , '
     Rcpp::NumericVector v(x);
     Rcpp::NumericVector result(v.length());
     for (int i = 0; i < v.length(); ++i) {
         result[i] = v[i] + i;
     }
     return(result);
     ', plugin = "Rcpp" )


HTH,

Jan


Quoting Grigory Alexandrovich <alexandrovich at mathematik.uni-marburg.de>:

> Hello,
>
> first thank you for your answers.
>
> I did not read the whole pdf Writing R Extension, but I read this
> strongly shortened introduction to this subject:
>
> http://www.math.kit.edu/stoch/~lindner/media/.c.call%20extensions.pdf
>
> I get the same error with this C-function:
>
> void test(double * b, int l)
> {
>      int i;
>      for(i=0; i < l ; i++) b[i] +=i;
> }
>
>
>
> I call it from R like this:
>
> parameter = c(0,0,1,1,1,0,1.5,0.7,0,1.2,0.3);
> .C("test", as.double(parameter), as.integer(11))
>
> The programm crashes even in this simple case.
> Where can be the error?
>
> Thanks
> Grigory Alexandrovich
>
>
>
>
>
>
>
> Answer 1
>> Without knowing that C code, we cannot know. Have you read Writing   
>> R Extensions carefully? I.e. take care with memory allocation and   
>> printing as mentioned in the manual.
>>
>> Uwe Ligges
>
>
> Answer 2
>> This looks like a classic case of not reading the manual, and then   
>> compounding it by not reading the posting guide. The manual would   
>> be the "Writing R Extensions" pdf that comes with R or you can   
>> google it. The posting guide is referenced at the bottom of this   
>> and every other posting on this mailing list.
>> There are nearly an infinite variety of errors that can lead to a   
>> "crash", so it is really unreasonable of you to pose this question   
>> this way and expect constructive assistance.
>> ---------------------------------------------------------------------------
>> Jeff Newmiller The ..... ..... Go Live...
>> DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
>> Live: OO#.. Dead: OO#.. Playing
>> Research Engineer (Solar/Batteries O.O#. #.O#. with
>> /Software/Embedded Controllers) .OO#. .OO#. rocks...1k
>> ---------------------------------------------------------------------------
>> Sent from my phone. Please excuse my brevity.
>
>
> Answer 3
>
>> It's impossible to say, with such minimal information, but a reasonable
>> guess is that there is a problem with the declaration of "x" and "y" in
>> foo.c.  These would (I think) need to be declared as double *, not double,
>> when foo is called from .C().
>>
>>    cheers,
>>
>>        Rolf Turner
>
>
> Answer 4
>
>> Hi,
>>
>> As other have said, it's very difficult to help you without an example
>> + code to know what you are talking about.
>>
>> That having been said, it seems as if you are just getting your feet
>> wet in this R <--> C bridge, and I'd recommend you checkout the "Rcpp"
>> and "inline" package to help make your life a lot easier ...
>>
>> -steve
>
>
>
>
>
>
>
>
>> On 04.10.2011 14:04, Grigory Alexandrovich wrote:
>>> Hello,
>>>
>>> I wrote a function in C, which works fine if called from the
>>> main-function in C.
>>>
>>> But as soon as I try to call this function from R like .C('foo',
>>> as.double(x), as.integer(y)), the programm crashes.
>>>
>>> I created a dll with the cmd command R --arch x64 CMD SHLIB foo.c and
>>> loaded it into R with dyn.load().
>>>
>>> What can be the cause of such behaviour?
>>> Again, the C-funcion itself works, but not if called from R.
>>>
>>> Thanks
>>> Grigory Alexandrovich
>>>
>>> ______________________________________________
>>> 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.
>
> ______________________________________________
> 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.



More information about the R-help mailing list