rcpp函数未在r包中导出

cyvaqqii  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(95)

当我试图从R包中加载一些c++函数时,在导出它们时遇到了一点麻烦。

> checking R code for possible problems ... NOTE
  postRiverComp: no visible binding for global variable
    ‘_thmc_postRiverComp’
  preFlopComp: no visible binding for global variable ‘_thmc_preFlopComp’
  preRiverComp: no visible binding for global variable
    ‘_thmc_preRiverComp’
  preTurnComp: no visible binding for global variable ‘_thmc_preTurnComp’
  rcpp_hello_world: no visible binding for global variable
    ‘_thmc_rcpp_hello_world’
  Undefined global functions or variables:
    _thmc_postRiverComp _thmc_preFlopComp _thmc_preRiverComp
    _thmc_preTurnComp _thmc_rcpp_hello_world

字符串
我认为我写Roxy注解的工作做得很好,所以我一定是在使用它们的地方使用了错误的函数。为了清楚起见,我可以library(thmc),并且不会抛出错误,但不会导出任何函数。我可以看到这一点,因为当我键入search()时,它会产生

[1] ".GlobalEnv"        "package:thmc"      "devtools_shims"    "tools:rstudio"     "package:stats"    
 [6] "package:graphics"  "package:grDevices" "package:utils"     "package:datasets"  "package:methods"  
[11] "Autoloads"         "org:r-lib"         "package:base"


下面是我的src/do_comps.cpp文件:

#include <Rcpp.h>
#include "easy_header.h"

using namespace Rcpp;

//' Calculate pre-flop probability estimates
//' 
//' @param your_cards your cards as a length-4 character vector.
//' @param num_sims number of simulations.
//' @param num_folders number of folders.
//' @param num_oppo number of opponents.
//' @export
// [[Rcpp::export]]
CharacterVector preFlopComp(CharacterVector your_cards, IntegerVector num_sims, IntegerVector num_folders,
                            IntegerVector num_oppo) {

  unsigned int _num_folders = num_folders[0];
  unsigned int _num_sims = num_sims[0];
  unsigned int _num_oppo = num_oppo[0];
  std::string val1 = Rcpp::as<std::string>(your_cards[0]);
  std::string suit1 = Rcpp::as<std::string>(your_cards[1]);
  std::string val2 = Rcpp::as<std::string>(your_cards[2]);
  std::string suit2 = Rcpp::as<std::string>(your_cards[3]);

  double percWon, percTie;

  // construct simulator object and add folders  
  Simulator s(_num_sims, _num_oppo); 
  for(size_t i = 0; i < _num_folders; ++i)
    s.addFolder();

  // add your cards
  s.addYouCard(Card::fromString(val1, suit1));
  s.addYouCard(Card::fromString(val2, suit2));

  // get final numbers
  s.doSim(percWon, percTie);

  return CharacterVector::create(s.prettyResults());
}


请注意,它在文档文件中有两行包含单词“export”。

# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

#' Calculate pre-flop probability estimates
#' 
#' @param your_cards your cards as a length-4 character vector.
#' @param num_sims number of simulations.
#' @param num_folders number of folders.
#' @param num_oppo number of opponents.
#' @export
preFlopComp <- function(your_cards, num_sims, num_folders, num_oppo) {
    .Call(`_thmc_preFlopComp`, your_cards, num_sims, num_folders, num_oppo)
}


也许是我的NAMESPACE文件。我已经尝试了一些事情,这些。最近的尝试是使用建议,我发现herehere

# Generated by roxygen2: do not edit by hand

importFrom(Rcpp, sourceCpp)
useDynLib(thmc, .registration=TRUE)
export(postRiverComp)
export(preFlopComp)
export(preRiverComp)
export(preTurnComp)


运行devtools::check()确实改变了这个文件,删除了importFromuseDynLib的调用。我也试过按照official vignette中的建议添加exportPattern("^[[:alpha:]]+")行,但没有用。
此外,我想我已经检查了这些答案here给出的所有框,仍然无济于事。

vpfxa7rd

vpfxa7rd1#

NAMESPACE中的importFromuseDynLib调用非常重要。

#' @useDynLib thmc, .registration = TRUE
#' @importFrom Rcpp evalCpp

字符串
到你的roxygen2代码中。这里有R注解,因为我通常会使用thmc-package.R。你也可以在使用C++注解时将其放入do_comps.cpp中。

2vuwiymt

2vuwiymt2#

不知道你是怎么解决你的问题的。对我来说没有任何效果。我最终要求Rcpp创建一个示例性的anRpackage(其cpp函数被导出),并将我所有的函数一个接一个地转移到那里。

相关问题