[Rcpp-devel] Fwd: Calling R function in Rcpp returns compatibility issues

Balamuta, James Joseph balamut2 at illinois.edu
Thu Jul 27 16:14:45 CEST 2017


Greetings and Salutations Sharat,

Could you supply some test data?

Sincerely,

JJB

From: rcpp-devel-bounces at lists.r-forge.r-project.org [mailto:rcpp-devel-bounces at lists.r-forge.r-project.org] On Behalf Of Sharat
Sent: Thursday, July 27, 2017 8:31 AM
To: rcpp-devel at lists.r-forge.r-project.org
Subject: [Rcpp-devel] Fwd: Calling R function in Rcpp returns compatibility issues

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;
}


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20170727/9b32c4a2/attachment.html>


More information about the Rcpp-devel mailing list