[Rcpp-devel] Rcpp exception with UTF-8 strings on Windows

Qin Wenfeng wenfeng.qin at qq.com
Sun Jun 12 16:43:08 CEST 2016


Hi all,

If an Rcpp function throw an exception with UTF-8 encoding strings, the error message in R will lose the encoding setting. And the error message will be displayed as some other characters on Windows.

Here is an example:

    library(Rcpp)
    sourceCpp(code = "
    #include <Rcpp.h>
     
     // [[Rcpp::export]]
     void error_with_unicode(std::string input){
         Rcpp::stop(input);
    }
     ")

    error_with_unicode(enc2utf8("测试"))
    #> Error: 娴嬭瘯

    tmp = try(error_with_unicode(enc2utf8("测试")))
    #> Error : 娴嬭瘯

    Encoding(tmp)
    #> [1] "unknown"

    tmp
    #> [1] "Error : 娴嬭瘯\n"
    #> attr(,"class")
    #> [1] "try-error"
    #> attr(,"condition")
    #> <Rcpp::exception: 娴嬭瘯>

    Encoding(tmp) = "UTF-8"
    tmp
    #> [1] "Error : 测试\n"
    #> attr(,"class")
    #> [1] "try-error"
    #> attr(,"condition")
    #> <Rcpp::exception: 娴嬭瘯>

I see this related post before http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2013-August/006286.html . But it seems that we can not set encoding for the error string.

Hope to get any help on this. Thanks.

Best,
Qin Wenfeng.


More information about the Rcpp-devel mailing list