Why does a Rcpp function with a \\n
line break sent to Rcpp::Rcout
not compile?
This works
cppFunction('void testing() {Rcout<<"hello"<<std::endl;}')
testing()
# hello
but this doesn't
cppFunction('void testing() {Rcout<<"hello\n";}')
R's string escaping is getting in your way. Try:
cppFunction('void testing() {Rcout<<"hello\\n";}')
Note the double \\\\
. Rather than transferring a literal new-line to the generated C++ script, you want to send the characters \\n
themselves -- hence, you need to escape the \\
.
To avoid issues like this, you should prefer using the attributes interface -- see Rcpp-attributes .
Note that this error becomes more obvious if you try running cppFunction('void testing() {Rcout<<"hello\\n";}', verbose=TRUE)
-- you can see the generated script has a literal newline rather than the \\n
characters.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.