Rcpp包找不到功能

时间:2015-04-15 20:04:58

标签: r rcpp

我使用Rcpp写了我的第一个R包;该软件包是一个简单的实用程序,用于读取应用程序TransCAD编写的二进制数据文件。当我在Rstudio中构建包时,一切都正常运行。

我将工作推送到GitHub,并从另一个R会话安装。一切都是应有的。

devtools::install_github("gregmacfarlane/tcadr")

Downloading github repo gregmacfarlane/tcadr@master
Installing tcadr
'/Library/Frameworks/R.framework/Resources/bin/R' --vanilla CMD INSTALL  \
  '/private/var/folders/wr/zlxhfd3d3591b4b9zgq4c5xr0000gn/T/Rtmpa2Xoyi/devtools3ca351cdd2e/gregmacfarlane-tcadr-65fc460'  \
  --library='/Library/Frameworks/R.framework/Versions/3.1/Resources/library'  \
  --install-tests 

* installing *source* package ‘tcadr’ ...
** libs
clang++ -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG  -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -I"/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/include"   -fPIC  -Wall -mtune=core2 -g -O2  -c RcppExports.cpp -o RcppExports.o
clang++ -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o tcadr.so RcppExports.o get_df_from_binary.o -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
installing to /Library/Frameworks/R.framework/Versions/3.1/Resources/library/tcadr/libs
** R
** data
*** moving datasets to lazyload DB
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (tcadr)

但是当我尝试运行我的包的主要R函数时,它找不到我写的c ++函数。

library(tcadr)
df <- read_tcad_bin(dir, file)
Error in as.data.frame(get_df_from_binary(bin_file, dcb$name, dcb$type,  : 
  could not find function "get_df_from_binary"

我已经检查过[rcpp export]函数是否由Rstudio构建正确编译。还有其他我应该检查的事情吗?

mwe data/文件夹中有一个示例数据集,您可以根据需要进行测试。


更新

我认为这不应该是一个新问题,但可能更有助于表明问题所在。

在三台不同的计算机上(两台OS X 10.10.3,一台Windows 7),会发生以下情况:

  • 如果我使用devtools::install_github()安装软件包,则R函数无法找到C函数。

  • 如果我从github克隆存储库并在RStudio中构建它,那么库可以完美地运行。

根据以下编译器的评论,Mac上的RStudio似乎使用gcc,但在控制台中使用clang。 Windows在两者中都使用g++

1 个答案:

答案 0 :(得分:3)

以下是我在Ubuntu 14.10下遇到的一组错误:

/tmp$ cat /tmp/tcadr.Rcheck/00install.out
* installing *source* package ‘tcadr’ ...
** libs
ccache g++ -I/usr/share/R/include -DNDEBUG   -I"/usr/local/lib/R/site-library/Rcpp/include"   -fpic  -g -O3 -Wall -pipe -Wno-unused -pedantic -c RcppExports.cpp -o RcppExports.o
ccache g++ -I/usr/share/R/include -DNDEBUG   -I"/usr/local/lib/R/site-library/Rcpp/include"   -fpic  -g -O3 -Wall -pipe -Wno-unused -pedantic -c get_df_from_binary.cpp -o get_df_from_binary.o
get_df_from_binary.cpp: In function ‘Rcpp::List get_df_from_binary(Rcpp::String, Rcpp::CharacterVector, Rcpp::CharacterVector, Rcpp::NumericVector, Rcpp::NumericVector, int)’:
get_df_from_binary.cpp:31:53: error: no matching function for call to ‘std::basic_fstream<char>::open(Rcpp::String&, std::_Ios_Openmode)’
   bf.open(bin_file, ios::in | ios::binary | ios::ate);
                                                     ^
get_df_from_binary.cpp:31:53: note: candidate is:
In file included from get_df_from_binary.cpp:4:0:
/usr/include/c++/4.9/fstream:889:7: note: void std::basic_fstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
       open(const char* __s,
       ^
/usr/include/c++/4.9/fstream:889:7: note:   no known conversion for argument 1 from ‘Rcpp::String’ to ‘const char*’
/usr/lib/R/etc/Makeconf:142: recipe for target 'get_df_from_binary.o' failed
make: *** [get_df_from_binary.o] Error 1
ccache g++ -I/usr/share/R/include -DNDEBUG   -I"/usr/local/lib/R/site-library/Rcpp/include"   -fpic  -g -O3 -Wall -pipe -Wno-unused -pedantic -c get_df_from_binary.cpp -o get_df_from_binary.o
get_df_from_binary.cpp: In function ‘Rcpp::List get_df_from_binary(Rcpp::String, Rcpp::CharacterVector, Rcpp::CharacterVector, Rcpp::NumericVector, Rcpp::NumericVector, int)’:
get_df_from_binary.cpp:31:53: error: no matching function for call to ‘std::basic_fstream<char>::open(Rcpp::String&, std::_Ios_Openmode)’
   bf.open(bin_file, ios::in | ios::binary | ios::ate);
                                                     ^
get_df_from_binary.cpp:31:53: note: candidate is:
In file included from get_df_from_binary.cpp:4:0:
/usr/include/c++/4.9/fstream:889:7: note: void std::basic_fstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
       open(const char* __s,
       ^
/usr/include/c++/4.9/fstream:889:7: note:   no known conversion for argument 1 from ‘Rcpp::String’ to ‘const char*’
/usr/lib/R/etc/Makeconf:142: recipe for target 'get_df_from_binary.o' failed
make: *** [get_df_from_binary.o] Error 1
ERROR: compilation failed for package ‘tcadr’
* removing ‘/tmp/tcadr.Rcheck/tcadr’
/tmp$ 

我认为你需要解决一些问题。

编辑:如果你这样做,它似乎至少可以编译:

diff --git a/src/get_df_from_binary.cpp b/src/get_df_from_binary.cpp
index 13d1b4a..15cd340 100644
--- a/src/get_df_from_binary.cpp
+++ b/src/get_df_from_binary.cpp
@@ -28,7 +28,7 @@ List get_df_from_binary(

   // Open the binary data file and make sure it exists
   std::fstream bf;
-  bf.open(bin_file, ios::in | ios::binary | ios::ate);
+  bf.open(bin_file.get_cstring(), ios::in | ios::binary | ios::ate);
   if(!bf.is_open()){
     throw std::range_error("could not open binary file");
   }