Skip to content

R-beta: Characters in .C

4 messages · Ian Wilson, Ross Ihaka, Thomas Lumley +1 more

#
I am having some difficulties using dynamically loaded
C functions on a Sparc 10 with R compiled
using cc  (both R-0.49 and R-0.50).  I get sporadic errors with the 
error message:
Error: character variables must be duplicated in .C/.Fortran,
with C functions which pass character variables.

my C functions look like:  anyfunc(char **filename, other variables )

This error message appears about 25% of the time.

What is the problem ? 

Ian

Dr Ian Wilson
Dept of Biological Sciences
Queen Mary and Westfield College
Mile End Road
London E1 4NS
Tel: 0171 775 3055
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
Ian Wilson writes:
 > 
 > I am having some difficulties using dynamically loaded
 > C functions on a Sparc 10 with R compiled
 > using cc  (both R-0.49 and R-0.50).  I get sporadic errors with the 
 > error message:
 > Error: character variables must be duplicated in .C/.Fortran,
 > with C functions which pass character variables.
 > 
 > my C functions look like:  anyfunc(char **filename, other variables )
 > 
 > This error message appears about 25% of the time.
 > 
 > What is the problem ? 
 
This is an uninitialized variable problem.  The fix is as follows:

In the file RHOME/src/main/dotcode.c, find the function declaration
for "naoktrim" and make the change noted below.

static SEXP naoktrim(SEXP s, int * len, int *naok, int *dup)
{
        SEXP value;

        if(s == R_NilValue) {
                value = R_NilValue;
                *naok = 0;
                *dup = 0;	/* !!!! Add this line !!!! */
                *len = 0;
        }

This will be in the next lot of patches we send out for 0.50.
Sorry 'bout that.
	Ross
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
The current examples of the scoping rules of R in the faq and in R&R's
JCGS paper are clear but a bit artificial.  The following example may be
of interest as it
a) does something useful in R
b) doesn't work in S
c) was written in S independently by myself and at least three of my
fellow students over the past couple of years, causing literally hours of
confusion on each occasion.

jackknife.lm<-function (lmobj) 
{
        n <- length(resid(lmobj))
        jval <- t(apply(as.matrix(1:n), 1, function(i) coef(update(lmobj, 
                subset = -i))))
        (n - 1)* (n-1) * var(jval)/n
}

In order to make it work in S you need to explicitly pass the linear model
object into the function nested in apply(). If you don't and you are lucky
you will get  
Error: Object "lmobj" not found
If you are unlucky enough to have a linear model called lmobj in your
global environment you will get the wrong answer with no warning. 

This version works in S

jackknife.S.lm<-function (lmobj) 
{
        n <- length(resid(lmobj))
        jval <- t(apply(as.matrix(1:n), 1, function(i,lmobj)
 coef(update(lmobj, subset = -i)),lmobj=lmobj))
        (n - 1)* (n-1) * var(jval)/n
}


Thomas Lumley
------------------------------------------------------+------
Biostatistics		: "Never attribute to malice what  :
Uni of Washington	:  can be adequately explained by  :
Box 357232		:  incompetence" - Hanlon's Razor  :
Seattle WA 98195-7232	:				   :
------------------------------------------------------------

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
Further to my message ...

Firstly for some reason (possibly the time of day) x11 now appears to work
OK with R0.50a1.  I did try it lower case but some funny stuff was
possibly lying around in the workspace (apologies for the 1960s
programming terminology).

Anyway, back to funfits.

Previously, even on startup, if I loaded the .RData file, the graphics
wouldn't work.  The possible key was that there is a plot.window function
in funfits which is also of course a plotting primitive.  So I checked in
all the fortran code (which could have passed the name back), found
nothing and trusted the authors not to have used nasty fortran 66 r*4's
for characters, and changed the S-code to grot.window etc. 

I also tidied up the fortran and hey presto the graphics now appears to
work when the funfits objects are loaded.  However NAs are not allowed in
xlim in plot.window so there is still a problem.  Also there is no maps
library and I couldn't find the thin plate surface fitting routine tps for
some reason.

The story goes on but the problem does not appear to be as fundamental as
I  thought it was.  Welcome to hacker's heaven.

John

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=