[R] how to call function in ../src/main

Wu, Kotien (NIH/NCI) wuko at mail.nih.gov
Tue Nov 2 17:19:33 CET 2004


Hi, This is Kotien Wu from NIH/NCI.

We want to use R function for our web:
http://cgap.nci.nih.gov

It works for functions in R/R-1.9.1/src/nmath/standalone very well.

We have function GetPvalueForT.c which has

#define MATHLIB_STANDALONE 1
#include <Rmath.h>
 
double GetPvalueForT ( double t, double deg ) {
  return 2 * pt(t, deg, 0, 0);
}

We create GetPvalueForT.i, which has

/* File : GetPvalueForT.i */
%module GetPvalueForT
 
extern double GetPvalueForT ( double t, double deg );

We use 

GetPvalueForT: $(srcdir)/GetPvalueForT.c
        swig -perl5 GetPvalueForT.i
        gcc $(ALL_CPPFLAGS) $(ALL_CFLAGS) -c GetPvalueForT.c
GetPvalueForT_wrap.c -I$(DCDFLIB)
 -I/usr/local/lib/perl5/5.8.2/sun4-solaris-thread-multi/CORE -Im
        ld -G GetPvalueForT.o -Im -L. -lRmath -lm GetPvalueForT_wrap.o -o
GetPvalueForT

in Makefile and we got GetPvalueForT.pm and GetPvalueForT and then we can
call it from perl:

test.pl:

#!/usr/local/bin/perl
use strict;
use GetPvalueForT;
 
sub test {
  my ( $t, $deg ) = @ARGV;
  my $result;
  for( my $i=0; $i<1000000; $i++ ) {
    $result = GetPvalueForT::GetPvalueForT($t, $deg);
  }
  my $temp = sprintf "%6.4f\n", $result;
  printf "result: $temp \n";
}
 
test();

It works fine.

But it looks not for calling functions in R/R-1.9.1/src/main

We want to correlation only, so we copy cov.c to cor.c
where use

cor ( int n, double *x, double *y, double *ans ) {
  Rboolean cor, kendall, pair, na_fail, sd_0;
  int ansmat, method, ncx, ncy;
 
  /* compute correlations if PRIMVAL(op) == 0,
             covariances  if PRIMVAL(op) != 0 */
  cor = 0;
 
  ansmat = 0;
 
  ncx = 1;
  ncy = 1;
  pair = TRUE;
 
  kendall = FALSE;
 
  cov_pairwise2(n, ncx, ncy, x, y, ans,
                          &sd_0, cor, kendall);
}

replace

SEXP do_cov(SEXP call, SEXP op, SEXP args, SEXP env)
{
  ...
}

and we have cor.i which has

/* File : cor.i */
%module cor
 
extern cor ( int n, double *x, double *y, double *ans );

We have

cor: cor.c
        swig -perl5 cor.i
        gcc -c cor.c cor_wrap.c -I. -I../../src/include -I/usr/local/include
-I$
(DCDFLIB) -I/usr/local/lib/perl5/5.8.2/sun4-solaris-thread-multi/CORE -Im
        ld -G cor.o -Im -L. -lRmath -lm cor_wrap.o -o cor

in Makefile and We compiled it and we got cor.pm and cor.

We have test.pl which has 

#!/usr/local/bin/perl
use strict;
use cor;
 
sub test {
  my $result;
  my @a = (1,2,3,4,5);
  my @b = (1,2,3,4,5);
  my $n = 5;
  cor( $n, \@a, \@b, \$result );
  my $temp = sprintf "%6.4f\n", $result;
  printf "result: $temp \n";
}
 
test();

We setup LD_LIBRARY_PASTH and then we run test.pl, we got:

$ test.pl
Can't load './cor' for module cor: ld.so.1: /usr/local/bin/perl: fatal:
relocation error: file ./cor: symbol R_NaReal: referenced symbol not found
at /usr/local/lib/perl5/5.8.2/sun4-solaris-thread-multi/DynaLoader.pm line
229.
 at cor.pm line 7
Compilation failed in require at test.pl line 3.
BEGIN failed--compilation aborted at test.pl line 3.

Do you know where is R_NaReal, what I did is wrong?

Thanks a lot!

Kotien




More information about the R-help mailing list