This question is related to my other question: Process list from R to C and access it.
I want to process a list of strings: <-list(c("ab", "ac"), c("ab", "zd", "fd"), c("de", "re", "te", "zz"))
.
I struggle with processing strings instead of integers. I know how to declare a string:
char string[] = "example";
But i obviously still have to learn a lot to process the data - my attempt:
char stor[] = CHAR(VECTOR_ELT(lst, i));
// and then store in in a list --> target[i] = stor;
The c Code - d.c
:
/* Including some headers to show the results*/
#include <Rinternals.h>
#include <Rdefines.h>
#include <R.h>
#include <stdlib.h>
#include <stdio.h>
SEXP processlist(SEXP lst){
int i;
int len = length(lst);
char **target = malloc(sizeof(char *)*len);
for (i = 0;i < l; i++) {
// use char stor[] to store string instead of Array!?
char stor[] = CHAR(VECTOR_ELT(lst, i));
// would i have to modify target[] too?
target[i] = stor;
}
printf("target[0]: %s\n",target[0]);
printf("target[1]: %s\n",target[1]);
printf("target[2]: %s\n",target[2]);
free(target);
return R_NilValue;
}
Equivilant to other post: d.R
(after d.c has been compiled):
dyn.load("d.so")
mylist<-list(c("ab", "ac"), c("ab", "zd", "fd"), c("de", "re", "te", "zz"))
# should be character already? to be sure,...
mylist<-lapply(mylist,as.character)
.Call("processlist", mylist)