我试图Rcpp11在它的源代码中使用r中的包:
devtools::install_github("Rcpp11/Rcpp11")
Run Code Online (Sandbox Code Playgroud)
我在这里阅读:http://blog.r-enthusiasts.com/2014/05/27/disambiguating-rcpp11-and-rcpp/
我可以在我的文件的头部使用include <Rcpp11>+ 但我在采购时遇到此错误:using namespace Rcpp11.cpp
egfile.cpp:1:10: fatal error: 'Rcpp11' file not found
#include <Rcpp11>
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
其中egfile.cpp:
#include <Rcpp11>
using namespace Rcpp11;
// 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 editor toolbar)
// For more on using …Run Code Online (Sandbox Code Playgroud) 假设一个人使用NumericVector,另一个人使用vector<double>他们的Rcpp代码.两种用法之间是否存在显着差异,尤其是在性能方面?
我有两组积分,叫做path和centers.对于每个点path,我想要一种有效的方法来找到最近点的ID centers.我想在R中这样做.下面是一个简单的可重复的例子.
set.seed(1)
n <- 10000
x <- 100*cumprod(1 + rnorm(n, 0.0001, 0.002))
y <- 50*cumprod(1 + rnorm(n, 0.0001, 0.002))
path <- data.frame(cbind(x=x, y=y))
centers <- expand.grid(x=seq(0, 500,by=0.5) + rnorm(1001),
y=seq(0, 500, by=0.2) + rnorm(2501))
centers$id <- seq(nrow(centers))
Run Code Online (Sandbox Code Playgroud)
x并且y是坐标.我想在pathdata.frame中添加一列,它具有给定x和y坐标的最近中心的id.然后我想获得所有独特的ID.
我的解决方案目前确实有效,但当问题的规模增加时,它的速度非常慢.我想要更高效的东西.
path$closest.id <- sapply(seq(nrow(path)), function(z){
tmp <- ((centers$x - path[z, 'x'])^2) + ((centers$y - path[z, 'y'])^2)
as.numeric(centers[tmp == min(tmp), 'id'])
})
output <- unique(path$closest.id)
Run Code Online (Sandbox Code Playgroud)
任何有关加快这一点的帮助将不胜感激.
我认为data.table可能有所帮助,但理想情况下我所寻找的是一种算法,在搜索方面可能更聪明,即不是计算到每个中心的距离,而是只选择最小的...获取id ... …
在C++中,我们可以将变量声明为引用.
int a = 10;
int& b = a;
Run Code Online (Sandbox Code Playgroud)
如果我们设置b=15,a也会改变.
我想在Rcpp做类似的事情.
List X = obj_from_R["X"];
IntegerVector x_i = X[index];
x_i = value;
Run Code Online (Sandbox Code Playgroud)
我想X通过在其向量之一中插入一个值来更新R中的对象.上面的代码不起作用,所以我尝试了这个:
IntegerVector& x_i = X[index];
Run Code Online (Sandbox Code Playgroud)
并收到错误.
error: non-const lvalue reference to type 'IntegerVector'
(aka 'Vector<13>') cannot bind to a temporary of type 'Proxy' (aka 'generic_proxy<19>')
Run Code Online (Sandbox Code Playgroud) 作为在进一步处理之前利用随机抽取的网络数据的一部分,我试图igraph在每次迭代开始时从包中调用几个函数。我使用的代码如下:
#define ARMA_64BIT_WORD
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::plugins(cpp11)]]
using namespace Rcpp;
using arma::sp_mat;
// [[Rcpp::export]]
sp_mat adj_mat(int n, double p) {
Environment igraph("package:igraph");
Function game_er = igraph["erdos.renyi.game"];
Function get_adjacency = igraph["get.adjacency"];
List g = game_er(Named("n", n), Named("p", p));
NumericMatrix A_m = get_adjacency(Named("g", g));
sp_mat A = as<sp_mat>(A_m);
return A;
}
/*** R
set.seed(20130810)
library(igraph)
adj_mat(100, 0.5)
*/
Run Code Online (Sandbox Code Playgroud)
因此,虽然 C++ 编译时没有警告,但会抛出以下错误:
> sourceCpp("Hooking-R-in-cpp.cpp")
> set.seed(20130810)
> library(igraph)
> adj_mat(100, 0.5)
Error in adj_mat(100, 0.5) :
Not compatible with …Run Code Online (Sandbox Code Playgroud) 根据Rcpp FAQ(2.15。新的 \xe2\x80\x98no-linking\xe2\x80\x99 功能怎么样),从 Rcpp 版本 0.11.0 开始,我们可以避免在 Makevars 文件中指定 LAPACK/BLAS/Fortran,并且事实上,如果我们遵循这些说明,就可以完全避免 Makevar。
\n\n\n... only two things are required:\n\xe2\x80\xa2 an entry in DESCRIPTION such as Imports: Rcpp (which\nmay be versioned as in Imports: Rcpp (>= 0.11.0)), and\n\xe2\x80\xa2 an entry in NAMESPACE to ensure Rcpp is correctly instantiated,\nfor example importFrom(Rcpp, evalCpp).\n
但是,除了Imports为 Rcpp 添加一行之外,LinkingTo还可以使用 a 吗?也就是说,我可以使用:
LinkingTo: Rcpp (>= 0.11.0)\nRun Code Online (Sandbox Code Playgroud)\n\n代替:
\n\nImports: Rcpp (>= 0.11.0)\nRun Code Online (Sandbox Code Playgroud)\n\n还是两者都需要?
\n我正在尝试安装 Rfast 包。它给了我以下错误,
/usr/lib/R/etc/Makeconf:168: recipe for target 'Norm.o' failed
make: *** [Norm.o] Error 1
Run Code Online (Sandbox Code Playgroud)
完整的错误信息是这样的,
> install.packages("Rfast", dependencies = TRUE)
* installing *source* package 'Rfast' ...
** package 'Rfast' successfully unpacked and MD5 sums checked
** libs
g++ -std=gnu++11 -I/usr/share/R/include -DNDEBUG -I"/home/haseeb/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include" -I"/home/haseeb/R/x86_64-pc-linux-gnu-library/3.4/RcppArmadillo/include" -fopenmp -fpic -g -O2 -fdebug-prefix-map=/build/r-base-AitvI6/r-base-3.4.4=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c Diag.cpp -o Diag.o
g++ -std=gnu++11 -I/usr/share/R/include -DNDEBUG -I"/home/haseeb/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include" -I"/home/haseeb/R/x86_64-pc-linux-gnu-library/3.4/RcppArmadillo/include" -fopenmp -fpic -g -O2 -fdebug-prefix-map=/build/r-base-AitvI6/r-base-3.4.4=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c Norm.cpp -o Norm.o
In …Run Code Online (Sandbox Code Playgroud)