Re: [R] allocating memory in C, not in R

2004-09-03 Thread Sundar Dorai-Raj
Sigal Blay wrote:
Thanks Sundar - but how do I return several vector objects of 
different modes wrapped in a list object using .Call?

Sigal

Have you looked at the documentation as I suggested? It's there in 
Section 4.8 of Writing R Extensions. Take a look at the examples for 
lapply or look at Rdefines.h and Rinternals.h in the src/include 
directory.

You're lucky I have an already worked example I have used for myself in 
the past which I include below.

/* junk.c */
#include R.h
#include Rdefines.h
SEXP junk(SEXP n) {
  SEXP *p_b, *p_nm;
  SEXP val, a, b, c, val_nm;
  double *p_a;
  int *p_c;
  int i, len, val_len;
  char *nm[3] = {numeric, character, integer};
  char *LETTERS[26] = {A, B, C, D, E, F,
   G, H, I, J, K, L,
   M, N, O, P, Q, R,
   S, T, U, V, W, X,
   Y, Z};
  val_len = 3;
  len = INTEGER_VALUE(n);
  PROTECT(val = allocVector(VECSXP, val_len));
  PROTECT(a = NEW_NUMERIC(len));
  PROTECT(b = NEW_CHARACTER(len));
  PROTECT(c = NEW_INTEGER(len));
  PROTECT(val_nm = NEW_CHARACTER(val_len));
  p_a = NUMERIC_POINTER(a);
  p_b = CHARACTER_POINTER(b);
  p_c = INTEGER_POINTER(c);
  p_nm = CHARACTER_POINTER(val_nm);
  for(i = 0; i  len; i++) {
p_a[i] = 1/(double)(i + 1);
p_b[i] = mkChar(LETTERS[i % 26]);
p_c[i] = i + 1;
  }
  for(i = 0; i  val_len; i++)
p_nm[i] = mkChar(nm[i]);
  SET_VECTOR_ELT(val, 0, a);
  SET_VECTOR_ELT(val, 1, b);
  SET_VECTOR_ELT(val, 2, c);
  setAttrib(val, R_NamesSymbol, val_nm);
  UNPROTECT(5);
  return val;
}
/* ## in R  */
/* dyn.load(junk) */
/* junk - function(n = 1) .Call(junk, n) */
/* junk(4)  */
--sundar
__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] allocating memory in C, not in R

2004-09-02 Thread Sigal Blay
 Below is a simplified version of my c function that I am 
 currenctly using with a .C() call.
 The values that has to be returned to R are the three outVectors.
 If I need to convert .C to .Call,
 How do I do it based on myFunction below?
 
 Thank you for your help.
  
 
 void myFunction(char **argv, int *inputVector, 
   int *RoutVector1, double *RoutVector2, char **RoutVector3) {
 int*outVector1;
 double *outVector2;
 char   **outVector3;
 int roof = 0;
 roof = calculate_values ( argv[0], 
   inputVector, 
   outVector1,
   outVector2, 
   outVector3   )
 
 for(i=0;iroof;i++) {
 RoutVector1[i] = outVector1[i];
 RoutVector2[i] = outVector2[i]; 
 } 
 return;
 }
 
 
 
 Well, you could read Writing R Extensions (Section 4.8). Your 
 function will have to change significantly, though. I would 
 create a list within the C-function (called val below) and 
 populate the RoutVectors with it.
 
 (I can't remember whether you'll need more than #include R.h. 
 Again, I direct you to the documentation.)
 
 SEXP myFunction(SEXP argv, SEXP inputVector) {
   SEXP val;
   PROTECT(val = allocVector(VECSXP, 3));
   /* some code you'll have to write */
   UNPROTECT(1);
   return val;
 }
 
 # in R
 RoutVectors - .Call(myFunction, argv, inputVector)
 
 --sundar

Thanks Sundar - but how do I return several vector objects of 
different modes wrapped in a list object using .Call?

Sigal

__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] allocating memory in C, not in R

2004-09-01 Thread S. Blay
Dear R helpers,

I need to retrieve several vectors of various types from a call 
to .C(), but I don't know their length in advance. 
Is there a way to do this without allocating an excessive amount 
of memory? 
If so, an example would be very helpful.


S. Blay
Department of Statistics and Actuarial Science
Simon Fraser University, Vancouver, British Columbia

__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] allocating memory in C, not in R

2004-09-01 Thread Sundar Dorai-Raj

S. Blay wrote:
Dear R helpers,
I need to retrieve several vectors of various types from a call 
to .C(), but I don't know their length in advance. 
Is there a way to do this without allocating an excessive amount 
of memory? 
If so, an example would be very helpful.


You should probably use the .Call interface instead of .C in this case.
--sundar
__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] allocating memory in C, not in R

2004-09-01 Thread Prof Brian Ripley
On Wed, 1 Sep 2004, S. Blay wrote:

 I need to retrieve several vectors of various types from a call 
 to .C(), but I don't know their length in advance. 
 Is there a way to do this without allocating an excessive amount 
 of memory? 
 If so, an example would be very helpful.

It would be very much easier to use .Call rather than .C.

Alternatively, generate and allocate in C on one C call and retrieve on a
second, as rpart does.

-- 
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] allocating memory in C, not in R

2004-09-01 Thread Sigal Blay
Thank you for the fast reply.

Below is a simplified version of my c function that I am 
currenctly using with a .C() call.
The values that has to be returned to R are the three outVectors.
If I need to convert .C to .Call,
How do I do it based on myFunction below?

Thank you for your help.
 

void myFunction(char **argv, int *inputVector, 
  int *RoutVector1, double *RoutVector2, char **RoutVector3) {
int*outVector1;
double *outVector2;
char   **outVector3;
int roof = 0;
roof = calculate_values ( argv[0], 
  inputVector, 
  outVector1,
  outVector2, 
  outVector3   )

for(i=0;iroof;i++) {
RoutVector1[i] = outVector1[i];
RoutVector2[i] = outVector2[i]; 
} 
return;
}


On Wed, Sep 01, 2004 at 10:16:59PM +0100, Prof Brian Ripley wrote:
 On Wed, 1 Sep 2004, S. Blay wrote:
 
  I need to retrieve several vectors of various types from a call 
  to .C(), but I don't know their length in advance. 
  Is there a way to do this without allocating an excessive amount 
  of memory? 
  If so, an example would be very helpful.
 
 It would be very much easier to use .Call rather than .C.
 
 Alternatively, generate and allocate in C on one C call and retrieve on a
 second, as rpart does.
 
 -- 
 Brian D. Ripley,  [EMAIL PROTECTED]
 Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
 University of Oxford, Tel:  +44 1865 272861 (self)
 1 South Parks Road, +44 1865 272866 (PA)
 Oxford OX1 3TG, UKFax:  +44 1865 272595

__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] allocating memory in C, not in R

2004-09-01 Thread Sundar Dorai-Raj

Sigal Blay wrote:
Thank you for the fast reply.
Below is a simplified version of my c function that I am 
currenctly using with a .C() call.
The values that has to be returned to R are the three outVectors.
If I need to convert .C to .Call,
How do I do it based on myFunction below?

Thank you for your help.
 

void myFunction(char **argv, int *inputVector, 
  int *RoutVector1, double *RoutVector2, char **RoutVector3) {
int*outVector1;
double *outVector2;
char   **outVector3;
int roof = 0;
roof = calculate_values ( argv[0], 
  inputVector, 
  outVector1,
  outVector2, 
  outVector3   )

for(i=0;iroof;i++) {
RoutVector1[i] = outVector1[i];
RoutVector2[i] = outVector2[i]; 
} 
return;
}


Well, you could read Writing R Extensions (Section 4.8). Your function 
will have to change significantly, though. I would create a list within 
the C-function (called val below) and populate the RoutVectors with it.

(I can't remember whether you'll need more than #include R.h. Again, I 
direct you to the documentation.)

SEXP myFunction(SEXP argv, SEXP inputVector) {
  SEXP val;
  PROTECT(val = allocVector(VECSXP, 3));
  /* some code you'll have to write */
  UNPROTECT(1);
  return val;
}
# in R
RoutVectors - .Call(myFunction, argv, inputVector)
--sundar
On Wed, Sep 01, 2004 at 10:16:59PM +0100, Prof Brian Ripley wrote:
On Wed, 1 Sep 2004, S. Blay wrote:

I need to retrieve several vectors of various types from a call 
to .C(), but I don't know their length in advance. 
Is there a way to do this without allocating an excessive amount 
of memory? 
If so, an example would be very helpful.
It would be very much easier to use .Call rather than .C.
Alternatively, generate and allocate in C on one C call and retrieve on a
second, as rpart does.
--
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html