Grokbase Groups R r-help August 2009
FAQ
Hello Forum,

I'm calling C function from R.It is a small sample trial program. The C function will accept a character and a integer and print them.
It is printing some special character instead of input character. Below are the C function, Wrapper code ,R code and R output.
Please help me in this issue
Thank you in advance

C function (Name : checkstr.c)
#include<stdio.h>
int n_char(char n,int m)
{
int result;
result = 3;
//result = strlen(n);
printf("the string is %c\n",n);
printf("the silly number is %d\n",m);
return result;

}

Wrapper Code (wrapcheckstr.c)
#include <stdio.h>
#include<string.h>

int n_char(char ,int );
void checkstr_R_wrapper(char *n,int *m, int *result)
{ *result = n_char(*n,*m); }

R code (checkstr.c)

checkstr <- function(n,m) {
if (!is.loaded(symbol.C('checkstr_R_wrapper'))) { dyn.load('checkstr.so') }
returned_data = .C('checkstr_R_wrapper', n=as.character(n),m=as.integer(m), result=integer(1))
return(returned_data$result) }

result in R
source('checkstr.R')
checkstr('a',3)
Warning in symbol.C("checkstr_R_wrapper") :
'symbol.C' is not needed: please remove it
the string is ?
the silly number is 3
[1] 3

Search Discussions

  • Naresh kumar at Aug 31, 2009 at 3:42 pm
    Hello Forum,

    I'm calling C function from R.It is a
    small sample trial
    program. The C function will accept a string and a integer
    and print them.
    It is giving error segmentation fault.
    Below are the C function, Wrapper code ,R code and
    R
    output.
    Please help me in this issue
    Thank you in
    advance

    Error:

    *** caught segfault ***
    address 0x68, cause 'memory not mapped'

    Traceback:
    1: .C("checkstr_R_wrapper", n = as.character(n), m = as.integer(m),     NAOK = TRUE, DUP = TRUE, result = integer(1))
    2: checkstr("hi", 2)

    Possible actions:
    1: abort (with core dump, if enabled)
    2: normal R exit
    3: exit R without saving workspace
    4: exit R saving workspace
    Selection: 1
    aborting ...
    Segmentation fault



    C FUNCTION (Name : checkstr.c)
    #include<stdio.h>
    #include<string.h>
    int n_char(char n[10],int m)
    {
    int result;
    result = 3;
    result = strlen(n);
    printf("the string is %s\n",n);
    printf("the silly number is %d\n",m);
    return result;

    }

    WRAPPER CODE
    (wrapcheckstr.c)
    #include <stdio.h>
    #include<string.h>

    int n_char(char,int);
    void checkstr_R_wrapper(char **n,int *m, int *result)
    { *result = n_char(**n,*m); }
    [proddun@venus myrpackages]$

    R CODE(checkstr.R)

    source('checkstr.R')
    checkstr <- function(n,m) {
    if (!is.loaded(symbol.C('checkstr_R_wrapper'))) { dyn.load('checkstr.so') }
    returned_data = .C('checkstr_R_wrapper', n=as.character(n),m=as.integer(m), result=integer(1))
    return(returned_data$result) }

    Thanks and Regards
    Naresh P




    Love Cricket? Check out live scores, photos, video highlights and more. Click here http://cricket.yahoo.com
  • Steve Lianoglou at Aug 31, 2009 at 8:41 pm
    Hi,

    Perhaps you should try just passing in two integers to some toy C
    function just to make sure all the plumbing is correct before you play
    with strings/char arrays.

    In any case, some comments inline:
    On Aug 31, 2009, at 11:42 AM, naresh kumar wrote:

    Hello Forum,

    I'm calling C function from R.It is a
    small sample trial
    program. The C function will accept a string and a integer
    and print them.
    It is giving error segmentation fault.
    Below are the C function, Wrapper code ,R code and
    R
    output.
    Please help me in this issue
    Thank you in
    advance

    Error:

    *** caught segfault ***
    address 0x68, cause 'memory not mapped'

    Traceback:
    1: .C("checkstr_R_wrapper", n = as.character(n), m =
    as.integer(m), NAOK = TRUE, DUP = TRUE, result = integer(1))
    2: checkstr("hi", 2)

    Possible actions:
    1: abort (with core dump, if enabled)
    2: normal R exit
    3: exit R without saving workspace
    4: exit R saving workspace
    Selection: 1
    aborting ...
    Segmentation fault



    C FUNCTION (Name : checkstr.c)
    #include<stdio.h>
    #include<string.h>
    int n_char(char n[10],int m)
    {
    int result;
    result = 3;
    result = strlen(n);
    printf("the string is %s\n",n);
    printf("the silly number is %d\n",m);
    return result;

    }
    Why are you defining the first argument in this function as ``char
    n[10]``. How do you know it's 10 chars long? My C is a bit rusty, but
    I'm guessing this might be causing a problem (like a seg fault!) if
    your n isn't 10 chars long.
    WRAPPER CODE
    (wrapcheckstr.c)
    #include <stdio.h>
    #include<string.h>

    int n_char(char,int);
    void checkstr_R_wrapper(char **n,int *m, int *result)
    { *result = n_char(**n,*m); }
    [proddun at venus myrpackages]$

    R CODE(checkstr.R)

    source('checkstr.R')
    checkstr <- function(n,m) {
    if (!is.loaded(symbol.C('checkstr_R_wrapper')))
    { dyn.load('checkstr.so') }
    returned_data = .C('checkstr_R_wrapper',
    n=as.character(n),m=as.integer(m), result=integer(1))
    return(returned_data$result) }
    If you're going to be passing in arrays of anything (chars, ints,
    etc), you should also pass in a sister variable that is the length of
    the array itself.

    So, your two functions might be:

    void checkstr_R_wrapper(char **n, int n_length, int *m, int *result) {
    *result = n_char(**n,*m);
    }

    and the R call would be:

    .C('checkstr_R_wrapper',
    n=as.character(n),
    n_length=length(as.character(n)),
    m=as.integer(m), result=integer(1))

    ... or something ... perhaps.

    Does that help any?

    -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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupr-help @
categoriesr
postedAug 28, '09 at 6:14p
activeAug 31, '09 at 8:41p
posts3
users2
websiter-project.org
irc#r

2 users in discussion

Naresh kumar: 2 posts Steve Lianoglou: 1 post

People

Translate

site design / logo © 2022 Grokbase