Hi Sharat,
First, few preliminary remarks before answering your question.
1. Just like every one asking for assistance, your should provide a minimal
reproducible example.
2. Before asking about Rcpp issue, it would be reasonable if you test your
comps() function in R,
to be sure that the problem is not in comps() itself (and yes, there is a
problem in comps(), cf. hereafter)
3. It is counter-intelligent to use Rcpp just for calling an R function.
Sometimes such calling is
unavoidable but doing _only_ this is a waste of time. Simply call your function
directly from R.
Main interest of Rcpp is to easily interface functions written in C++ (which
are usually
much faster then their counterparts in R) with R session.
Now, about your problem.
In fact, your version of comps() returns always NULL which causes the error
message.
It is because of the line:
append(res,paste(prop1,"<-->", prop2))
which should be
res=append(res,paste(prop1,"<-->", prop2))
Furthermore, when corrected and in presence of matches, comps() returns a
string vector, not a matrix.
So your have to invert your declarations of getGoing() making him return a
StringVector and taking
as argument a StringMatrix. In shorter writing, it gives:
// [[Rcpp::export]]
StringVector getGoing(StringMatrix vec){
Function f = Environment::global_env()["comps"];
return f(vec);
}
Best,
Serguei.
Le 27/07/2017 à 15:30, Sharat a écrit :
Hi:
I'm trying to call a R function (that compares strings) in Rcpp and return the result to make the comparisons run faster. Am a noob with Rcpp; so please bear.
The input to the R function is a matrix of 2 columns.
The error is: *Error in getGoing(product) : Not compatible with STRSXP:
[type=NULL]. *
Please assist.
#include<Rcpp.h>
using namespace Rcpp;
/*** R
comps= function(vec)
{
streets <- vec
cnt <- nrow(streets)
res <- c()
print(paste0("Comparing ", (cnt)," pairs..."))
for (i in 1:cnt)
{
matched <- TRUE
prop1 = streets[i,1]
prop2 = streets[i,2]
prop1_parts = strsplit(trimws(prop1), ' ')
prop2_parts = strsplit(trimws(prop2), ' ')
prop1_parts_count = length(prop1_parts[[1]])
prop2_parts_count = length(prop2_parts[[1]])
if (prop1_parts_count == prop2_parts_count)
{
for (x in 1:prop1_parts_count)
{
part1 = prop1_parts[[1]][x]
part2 = prop2_parts[[1]][x]
if (part1 == part2) {
matched = matched & TRUE
}
else if (adist(part1, part2, partial = T)==0 | adist(part2,part1,
partial = T)==0){
matched = matched & TRUE
}
else {
matched = matched & FALSE
break
}
}#forloop ends
if(matched){
append(res,paste(prop1,"<-->", prop2))
}
}#if loops ends
}#primary for loops ends
res
}#function ends
*/
// [[Rcpp::export]]
Rcpp::StringMatrix getGoing(Rcpp::StringVector vec){
Rcpp::Environment env = Rcpp::Environment::global_env();
Rcpp::Function f = env["comps"];
Rcpp::StringMatrix result = f(vec);
return result;
}
_______________________________________________
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
_______________________________________________
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel