简体   繁体   English

cppFunction:编译Rcout <<“换行\\ n”

[英]cppFunction: Compiling Rcout << “line break\n”

Why does a Rcpp function with a \\n line break sent to Rcpp::Rcout not compile? 为什么带有\\n换行符的Rcpp函数发送到Rcpp::Rcout不能编译?

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. R的字符串转义正妨碍您。 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 \\ . 而不是将原义的换行符传输到生成的C ++脚本,您希望自己发送字符\\n ,因此,您需要转义\\

To avoid issues like this, you should prefer using the attributes interface -- see Rcpp-attributes . 为了避免出现此类问题,您应该更喜欢使用attributes接口-请参阅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. 请注意,如果您尝试运行cppFunction('void testing() {Rcout<<"hello\\n";}', verbose=TRUE) ,则该错误会变得更加明显-您可以看到生成的脚本具有文字换行符而不是\\n字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM