在R中,您可以使用as.X
在向量类型之间进行强制,例如as.character(1)
或as.integer(1)
.然而,我不知道在RCPP中是否有一种本地的、优雅的方式来做同样的事情.
首先,C API确实有一个我试图使用的AS_CHARACTER
宏,但RCPP似乎没有定义它的位置包括Rdefines.h
:
> as_character = Rcpp::cppFunction("CharacterVector as_character(RObject x){ return AS_CHARACTER(x); }")
file113af2570bd06.cpp: In function ‘Rcpp::CharacterVector as_character(Rcpp::RObject)’:
file113af2570bd06.cpp:6:49: error: ‘AS_CHARACTER’ was not declared in this scope
6 | CharacterVector as_character(RObject x){ return AS_CHARACTER(x); }
| ^~~~~~~~~~~~
make: *** [/usr/lib/R/etc/Makeconf:178: file113af2570bd06.o] Error 1
g++ -std=gnu++14 -I"/usr/share/R/include" -DNDEBUG -I"/home/migwell/R/x86_64-pc-linux-gnu-library/4.2/Rcpp/include" -I"/tmp/RtmppCAKV8/sourceCpp-x86_64-pc-linux-gnu-1.0.10" -fpic -g -O2 -ffile-prefix-map=/build/r-base-LhKvHL/r-base-4.2.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -c file113af2570bd06.cpp -o file113af2570bd06.o
Error in sourceCpp(code = code, env = env, rebuild = rebuild, cacheDir = cacheDir, :
Error 1 occurred building shared library.
如果我try 使用CharacterVector
,它也无法编译:
> as_character = Rcpp::cppFunction(
+ "CharacterVector as_character(RObject x){ return CharacterVector::create(x); }"
+ )
In file included from /home/migwell/R/x86_64-pc-linux-gnu-library/4.2/Rcpp/include/Rcpp/Vector.h:50,
from /home/migwell/R/x86_64-pc-linux-gnu-library/4.2/Rcpp/include/Rcpp.h:40,
from file113af65cda53c.cpp:1:
/home/migwell/R/x86_64-pc-linux-gnu-library/4.2/Rcpp/include/Rcpp/vector/converter.h: In instantiation of ‘static SEXPREC* Rcpp::internal::string_element_converter<RTYPE>::get(const T&) [with T = Rcpp::RObject_Impl<Rcpp::PreserveStorage>; int RTYPE = 16; SEXP = SEXPREC*]’:
/home/migwell/R/x86_64-pc-linux-gnu-library/4.2/Rcpp/include/Rcpp/generated/Vector__create.h:84:34: required from ‘static Rcpp::Vector<RTYPE, StoragePolicy> Rcpp::Vector<RTYPE, StoragePolicy>::create__dispatch(Rcpp::traits::false_type, const T1&) [with T1 = Rcpp::RObject_Impl<Rcpp::PreserveStorage>; int RTYPE = 16; StoragePolicy = Rcpp::PreserveStorage; Rcpp::traits::false_type = Rcpp::traits::integral_constant<bool, false>]’
/home/migwell/R/x86_64-pc-linux-gnu-library/4.2/Rcpp/include/Rcpp/generated/Vector__create.h:71:26: required from ‘static Rcpp::Vector<RTYPE, StoragePolicy> Rcpp::Vector<RTYPE, StoragePolicy>::create(const T1&) [with T1 = Rcpp::RObject_Impl<Rcpp::PreserveStorage>; int RTYPE = 16; StoragePolicy = Rcpp::PreserveStorage]’
file113af65cda53c.cpp:6:72: required from here
/home/migwell/R/x86_64-pc-linux-gnu-library/4.2/Rcpp/include/Rcpp/vector/converter.h:49:37: error: no matching function for call to ‘std::__cxx11::basic_string<char>::basic_string(const Rcpp::RObject_Impl<Rcpp::PreserveStorage>&)’
49 | std::string out(input) ;
|
另一方面,它确实允许Rf_coerceVector
,但这似乎不是最好的方法,因为如果我使用STRSXP
和Rf_
函数,我对纯C语言有相当深的了解:
> as_character = Rcpp::cppFunction(
+ "CharacterVector as_character(RObject x){ return Rf_coerceVector(x, STRSXP); }"
+ )
> as_character(1)
[1] "1"
在RCPP中执行此操作的最自然/最简单的方法是什么?
我正在使用:
- R 4.2.3
- RCPP 1.0.10