<div dir="ltr">Thanks Serguei for clearing up the data structures. I did test the function in R but when I was printing out the results, rather than returning a vector; sorry about that. Will be much more careful. </div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jul 27, 2017 at 9:47 AM, Serguei Sokol <span dir="ltr"><<a href="mailto:serguei.sokol@gmail.com" target="_blank">serguei.sokol@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Sharat,<br>
<br>
First, few preliminary remarks before answering your question.<br>
1. Just like every one asking for assistance, your should provide a minimal reproducible example.<br>
2. Before asking about Rcpp issue, it would be reasonable if you test your comps() function in R,<br>
to be sure that the problem is not in comps() itself (and yes, there is a problem in comps(), cf. hereafter)<br>
3. It is counter-intelligent to use Rcpp just for calling an R function. Sometimes such calling is<br>
unavoidable but doing _only_ this is a waste of time. Simply call your function directly from R.<br>
Main interest of Rcpp is to easily interface functions written in C++ (which are usually<br>
much faster then their counterparts in R) with R session.<br>
<br>
Now, about your problem.<br>
In fact, your version of comps() returns always NULL which causes the error message.<br>
It is because of the line:<br>
append(res,paste(prop1,"<-->"<wbr>, prop2))<br>
which should be<br>
res=append(res,paste(prop1,"<<wbr>-->", prop2))<br>
<br>
Furthermore, when corrected and in presence of matches, comps() returns a string vector, not a matrix.<br>
So your have to invert your declarations of getGoing() making him return a StringVector and taking<br>
as argument a StringMatrix. In shorter writing, it gives:<br>
<br>
// [[Rcpp::export]]<br>
StringVector getGoing(StringMatrix vec){<br>
Function f = Environment::global_env()["com<wbr>ps"];<br>
return f(vec);<br>
}<br>
<br>
Best,<br>
Serguei.<span class=""><br>
<br>
Le 27/07/2017 à 15:30, Sharat a écrit :<br>
</span><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
Hi:<br>
<br>
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.<br>
<br></span>
The error is: *Error in getGoing(product) : Not compatible with STRSXP: [type=NULL]. *<div><div class="h5"><br>
<br>
Please assist.<br>
<br>
#include<Rcpp.h><br>
using namespace Rcpp;<br>
<br>
/*** R<br>
comps= function(vec)<br>
{<br>
streets <- vec<br>
cnt <- nrow(streets)<br>
res <- c()<br>
print(paste0("Comparing ", (cnt)," pairs..."))<br>
for (i in 1:cnt)<br>
{<br>
matched <- TRUE<br>
prop1 = streets[i,1]<br>
prop2 = streets[i,2]<br>
<br>
prop1_parts = strsplit(trimws(prop1), ' ')<br>
prop2_parts = strsplit(trimws(prop2), ' ')<br>
prop1_parts_count = length(prop1_parts[[1]])<br>
prop2_parts_count = length(prop2_parts[[1]])<br>
if (prop1_parts_count == prop2_parts_count)<br>
{<br>
for (x in 1:prop1_parts_count)<br>
{<br>
part1 = prop1_parts[[1]][x]<br>
part2 = prop2_parts[[1]][x]<br>
if (part1 == part2) {<br>
matched = matched & TRUE<br>
}<br>
else if (adist(part1, part2, partial = T)==0 | adist(part2,part1, partial = T)==0){<br>
matched = matched & TRUE<br>
}<br>
else {<br>
matched = matched & FALSE<br>
break<br>
}<br>
}#forloop ends<br>
if(matched){<br>
append(res,paste(prop1,"<-->"<wbr>, prop2))<br>
}<br>
}#if loops ends<br>
}#primary for loops ends<br>
res<br>
}#function ends<br>
*/<br>
<br>
// [[Rcpp::export]]<br>
Rcpp::StringMatrix getGoing(Rcpp::StringVector vec){<br>
Rcpp::Environment env = Rcpp::Environment::global_env(<wbr>);<br>
Rcpp::Function f = env["comps"];<br>
Rcpp::StringMatrix result = f(vec);<br>
return result;<br>
}<br>
<br>
<br>
<br></div></div>
______________________________<wbr>_________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-pro<wbr>ject.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" rel="noreferrer" target="_blank">https://lists.r-forge.r-projec<wbr>t.org/cgi-bin/mailman/listinfo<wbr>/rcpp-devel</a><br>
<br>
</blockquote>
<br>
</blockquote></div><br></div>