[R] Calling C funtion from R help Needed

naresh4836 at yahoo.co.in naresh4836 at yahoo.co.in
Fri Aug 28 20:14:42 CEST 2009


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




More information about the R-help mailing list