我实现了一个,但是当给定的键在字典中没有值时,我没有找到一个好的方法来引发错误。调用stop很容易,但是我想通过打印R对象告诉用户没有找到哪个键。现在我有:
Rcpp::Rcout << "Key not found: ";
Rcpp::print(key); # <-- how can I get this on stderr?
Rcpp::stop("Key error!");
这会将消息打印到stdout,但我宁愿将其放在stderr上。也许我只是缺少了Rcpp提供的一个功能?
这是一辆MWE:
library(Rcpp)
source
我有一个Rcpp函数,它读取大型BAM文件(使用htslib1-20 to ),并创建几个非常长的std::vector(最多80M元素)。在阅读之前不知道元素的数量,因此我不能使用Rcpp::IntegerVector和Rcpp::CharacterVector。据我所知,当我进一步使用Rcpp::wrap时,将创建副本。在这种情况下,是否有办法加快从C++到R的数据传输?是否有一个可以在Rcpp函数中创建的数据结构,可以像push_back那样快速地传递到std::vector元素,并通过引用R传递?
以防万一,下面是我当前创建它们的方式:
std::vector<std::stri
我使用Rcpp::Rcpp.package.skeleton()生成一个Rcpp包,或者使用RStudio使用roxygen2文档进行测试,并将文件夹src中生成的test cpp函数更改为:
#include <Rcpp.h>
using namespace Rcpp;
//’ Testing documentation with Rcpp
//’
//’ @param x Character vector for testing
//’ @param y Numeric vector for testing more
//’
//’ @ret
我的Rcpp代码偶尔会失败(SEGFAULT等等)因为我不明白的原因。代码创建一个大型data.frame,然后通过调用R子集函数( [.data.frame),尝试从创建框架的同一方法中获取该data.frame的子集。它的一个非常简化的版本如下:
library(Rcpp)
src <- '// R function to subset data.frame - what will be called to subset
DataFrame test() {
Function subsetinR("[.data.frame");
// Make a dat
当我试图使用操作符*在Rcpp中乘arma::mat和NumericVector时,我得到以下错误:
no match for operator*
这里有一个例子,说明了我想要乘的东西:
NumericVector exampleNumericVector(L-1);
arma::mat exampleMatrix(L-1,L-1);
exampleMatrix*(Rcpp::qnorm(exampleNumericVector,0,1,true,false));
我尝试过查看优秀的Armadillo文档,最接近解决方案的是prod()函数,但这并没有纠正我的问题。我也试着超载操作员*。然而,
这是我的测试代码
#include <Rcpp.h>
using namespace Rcpp;
#include "/Users/jjunju/Documents/R/accum/accum.h"
// Below is a simple example of exporting a C++ function to R. You can
// source this function into an R session using the Rcpp::sourceCpp
// function (or via the Source button on the
我创建了一个C++模板的" sample“函数,它使用Boost随机数生成器从任何作为可迭代对象提供多项式分布的对象中进行采样。
/** Sample a value from a multinomial law with coefficient of modalities provided */
template<typename T>
int sample(const T& proportion)
{
boost::random::uniform_real_distribution<> uni(0.,
任务是重塑三维数组从行、行、切到切片,行,直线。我试着用base::aperm实现Rcpp模拟。
// [[Rcpp::export]]
Rcpp::NumericVector array_perm(const Rcpp::NumericVector& x) {
if (Rf_isNull(x.attr("dim"))) {
throw std::runtime_error("'x' does not have 'dim' attibute.");
}
Rcpp::Dimension
我想知道是否有一种Rcpp方法可以将const CharacterVector&的元素或迭代器转换为std::string。如果我尝试以下代码
void as(const CharacterVector& src) {
std::string glue;
for(int i = 0;i < src.size();i++) {
glue.assign(src[i]);
}
}
将发生编译器时间错误:
no known conversion for argument 1 from ‘const type {aka SEXPREC* const}’ to
我对Rcpp及其功能相当陌生,更不用说C++本身了,所以这对你们中的专家来说可能是微不足道的。然而,没有愚蠢的问题,所以不管怎样:
我想知道是否有一种方法可以使用索引在C++中同时处理NumericVector的多个元素。为了让整个事情变得更清楚,这里是我正在尝试做的R等价物:
# Initial vector
x <- 1:10
# Extract the 2nd, 5th and 8th element of the vector
x[c(2, 5, 8)]
[1] 2 5 8
这是我在使用sourceCpp在R中执行的C++函数中得到的结果。它可以工作,但对我来说似乎很不方便。有