2

I have to convert individual elements of Rcpp::IntegerVector into their string form so I can add another string to them. My code looks like this:

   #include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
Rcpp::String int_to_char_single_fun(int x){
  // Obtain environment containing function
  Rcpp::Environment base("package:base");

  // Make function callable from C++
  Rcpp::Function int_to_string = base["as.character"];

  // Call the function and receive its list output
  Rcpp::String res = int_to_string(Rcpp::_["x"] = x); // example of original param

  // Return test object in list structure
  return (res);
}

//[[Rcpp::export]]
Rcpp::CharacterVector add_chars_to_int(Rcpp::IntegerVector x){
  int n = x.size();
  Rcpp::CharacterVector BASEL_SEG(n);
  for(int i = 0; i < n; i++){
  BASEL_SEG[i] = "B0" +  int_to_char_single_fun(x[i]);
  }
  return BASEL_SEG;
}

/*** R
int_vec <- as.integer(c(1,2,3,4,5))
BASEL_SEG_char <- add_chars_to_int(int_vec)
*/

I get the following error:

no match for 'operator+'(operand types are 'const char[3]' and 'Rcpp::String')

I cannot import any C++ libraries like Boost to do this and can only use Rcpp functionality to do this. How do I add string to integer here in Rcpp?

Gompu
  • 415
  • 1
  • 6
  • 21
  • 1
    This is really bad. You do not need R's `as.character()`. Look somewhere, anywhere really, for integer to string conversion. The C library has it, Boost has it etc pp. – Dirk Eddelbuettel Feb 27 '18 at 20:48

2 Answers2

5

We basically covered this over at the Rcpp Gallery when we covered Boost in an example for lexical_cast (though that one went the other way). So rewriting it quickly yields this:

Code

// We can now use the BH package
// [[Rcpp::depends(BH)]]

#include <Rcpp.h>
#include <boost/lexical_cast.hpp>   

using namespace Rcpp;

using boost::lexical_cast;
using boost::bad_lexical_cast;

// [[Rcpp::export]]
std::vector<std::string> lexicalCast(std::vector<int> v) {

    std::vector<std::string> res(v.size());

    for (unsigned int i=0; i<v.size(); i++) {
        try {
            res[i] = lexical_cast<std::string>(v[i]);
        } catch(bad_lexical_cast &) {
            res[i] = "(failed)";
        }
    }

    return res;
}


/*** R
lexicalCast(c(42L, 101L))
*/

Output

R> Rcpp::sourceCpp("/tmp/lexcast.cpp")

R> lexicalCast(c(42L, 101L))
[1] "42"  "101"
R> 

Alternatives

Because converting numbers to strings is as old as computing itself you could also use:

  • itoa()
  • snprintf()
  • streams
  • and probably a few more I keep forgetting.
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
5

As others have pointed out, there are several ways to do this. Here are two very straightforward approaches.

1. std::to_string

Rcpp::CharacterVector add_chars_to_int1(Rcpp::IntegerVector x){
    int n = x.size();
    Rcpp::CharacterVector BASEL_SEG(n);
    for(int i = 0; i < n; i++){
        BASEL_SEG[i] = "B0" +  std::to_string(x[i]);
    }
    return BASEL_SEG;
}

2. Creating a new Rcpp::CharacterVector

Rcpp::CharacterVector add_chars_to_int2(Rcpp::IntegerVector x){
    int n = x.size();
    Rcpp::CharacterVector BASEL_SEG(n);
    Rcpp::CharacterVector myIntToStr(x.begin(), x.end());
    for(int i = 0; i < n; i++){
        BASEL_SEG[i] = "B0" +  myIntToStr[i];
    }
    return BASEL_SEG;
}

Calling them:

add_chars_to_int1(int_vec) ## using std::to_string
[1] "B01" "B02" "B03" "B04" "B05"

add_chars_to_int2(int_vec) ## converting to CharacterVector
[1] "B01" "B02" "B03" "B04" "B05"
Joseph Wood
  • 7,077
  • 2
  • 30
  • 65