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
Problem with .C
10 messages · Grigory Alexandrovich, Jeff Newmiller, Rolf Turner +3 more
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
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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20111004/1baf2dc1/attachment.pl>
On 05/10/11 01: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.
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
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 Tue, Oct 4, 2011 at 8:04 AM, Grigory Alexandrovich
<alexandrovich at mathematik.uni-marburg.de> 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.
Steve Lianoglou Graduate Student: Computational Systems Biology ?| Memorial Sloan-Kettering Cancer Center ?| Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
1 day later
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.
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.
On 06.10.2011 14:51, Jan van der Laan wrote:
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" )
Oh, come on, this is now really too much of overkill.
Just make the original source
void test(double *b, int *l)
{
int i;
for(i=0; i < *l ; i++) b[i] += i;
}
which you would have know after reading the Wriiting R Extensions manual.
Best,
Uwe Ligges
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.
Hi, 2011/10/6 Uwe Ligges <ligges at statistik.tu-dortmund.de>:
On 06.10.2011 14:51, Jan van der Laan wrote:
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" )
Oh, come on, this is now really too much of overkill.
I don't agree that it's overkill -- you get to sidestep the whole `R CMD SHLIB ...` and `dyn.load` dance this way while you experiment with C(++) code 'live" using the inline package. It's really handy.
Just make the original source
void test(double *b, int *l)
{
? ? int i;
? ? for(i=0; i < *l ; i++) b[i] += i;
}
which you would have know after reading the Wriiting R Extensions manual.
I agree that this step is unavoidable no matter which avenue (Rcpp or otherwise) one decides to take. -steve
Steve Lianoglou Graduate Student: Computational Systems Biology ?| Memorial Sloan-Kettering Cancer Center ?| Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
On 06.10.2011 15:41, Steve Lianoglou wrote:
Hi, 2011/10/6 Uwe Ligges<ligges at statistik.tu-dortmund.de>:
On 06.10.2011 14:51, Jan van der Laan wrote:
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" )
Oh, come on, this is now really too much of overkill.
I don't agree that it's overkill -- you get to sidestep the whole `R CMD SHLIB ...` and `dyn.load` dance this way while you experiment with C(++) code 'live" using the inline package.
You need two additional packages now where you have to rely on the fact those are available. Moreover, you have to get used to that syntax, and part of it seems to be C++ now? At least I do not know why the above should work at all, while I know the simple C function does. Uwe
It's really handy.
Just make the original source
void test(double *b, int *l)
{
int i;
for(i=0; i< *l ; i++) b[i] += i;
}
which you would have know after reading the Wriiting R Extensions manual.
I agree that this step is unavoidable no matter which avenue (Rcpp or otherwise) one decides to take. -steve