我有2个文件,Rfile.R和Cppfile.cpp.
Cppfile.cpp中的内容:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
int CPPF(int k){return ++k;}
Run Code Online (Sandbox Code Playgroud)
Rfile.R中的内容:
RF<-function(k){return(CPPF(k))}
Run Code Online (Sandbox Code Playgroud)
我想基于2个文件构建一个R包.我使用最新版本的Rstudio和Roxygen2.
我尝试了3种方法来构建包含或不包含Roxygen2的包,并且有不同的结果:
New Project-> New Directory-> R package-> Type:Package w/Rcpp,将Rfile.R和Cppfile.cpp都添加为源文件.构建和重新加载,一切正常.这些功能可以正常工作.
New Project-> New Directory-> R package-> Type:Package w/Rcpp,将Rfile.R和Cppfile.cpp都添加为源文件.选择"使用Roxygen生成文档",检查其所有选项.构建和重新加载,功能不起作用.输入"RF"给出RF的内容,输入"CPPF"弹出"未找到对象".
New Project-> New Directory-> R package-> Type:Package w/Rcpp,只添加Cppfile.cpp作为源文件.选择"使用Roxygen生成文档",检查其所有选项.Build&Reload,功能正常.然后将Rfile.R直接复制到项目文件夹 - > R文件夹中.Build&Reload,一切都很好,功能很好.
我使用Roxygen错了还是Roxygen有虫子?我需要它来记录.我可以坚持第三种方式,这需要花费很多精力才能找到,但有线.
谢谢!
解决问题的一种方法:选择"使用Roxygen生成文档"时,请勿选中"NAMESPACE文件"选项.
在C/C++中,当我有一堆函数(指针)时,我可以将它们存储在数组或向量中,并按特定顺序将它们中的一些调用在一起.可以在VBA中做类似的事情吗?
谢谢!
嗨,每个喜欢讨厌的人R:
假设您想要转换矩阵M.
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
Run Code Online (Sandbox Code Playgroud)
到N.
[,1] [,2] [,3]
[1,] 3 2 1
[2,] 6 5 4
[3,] 9 8 7
Run Code Online (Sandbox Code Playgroud)
你需要做的就是
N<-M[,c(3:1)]
Run Code Online (Sandbox Code Playgroud)
N的结构仍然是一个矩阵
但是,当你想转矩阵M时
[,1] [,2] [,3]
[1,] 1 2 3
Run Code Online (Sandbox Code Playgroud)
到N.
[,1] [,2] [,3]
[1,] 3 2 1
Run Code Online (Sandbox Code Playgroud)
如果你做N <-M [,c(3:1)] R会给你
N
[1] 3 2 1
Run Code Online (Sandbox Code Playgroud)
N现在是一个向量!不是矩阵!
我的解决方案是N <-M%*%diag(3)[,c(3:1)],但是需要很大的空间来存储单位矩阵.
有什么好主意吗?
我试图找到在R中制作链接列表的方法.
我发现tracemem()返回一个对象的内存地址,所以有什么方法可以通过它的内存地址找到一个对象?
假设我有一个长排序列表 L={1, 2,.., 999, 1000} 和一个短排序列表 S={22, 255, 623, 732, 876}。
在 L 中,我想搜索 S 的每个元素。最有效的方法是什么?
到目前为止我想出的方法是:
1. Binary search for 22. Record the lower bound=23
2. Binary search for 876. Record the upper bound=875
3. Binary search for 255 in the range [lower bound=23, upper bound=875].
4. Set lower bound=256, and go on..
Run Code Online (Sandbox Code Playgroud)
这是最有效的方法吗?有没有其他方法可以比这个方法收敛得更快呢?
谢谢!
我试图找出一个错误,该错误阻止我使用 RcppArmadillo 制作 R 包“whyNotWork”。该软件包仅包含一个 .cpp 文件,其中包含一个返回矩阵特征值的简单函数:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
// [[Rcpp::export]]
arma::vec getEigenValues(arma::mat M)
{
return arma::eig_sym(M);
}
Run Code Online (Sandbox Code Playgroud)
在描述文件中我添加了:
Imports:
Rcpp (>= 0.12.1), RcppArmadillo
LinkingTo:
Rcpp, RcppArmadillo
Depends: RcppArmadillo
Run Code Online (Sandbox Code Playgroud)
在 NAMESPACE 文件中我添加了:
exportPattern("^[[:alpha:]]+")
useDynLib(whyNotWork)
import(RcppArmadillo)
importFrom(Rcpp, evalCpp)
Run Code Online (Sandbox Code Playgroud)
如果我没有尝试创建一个包,该函数就可以正常工作——“源”按钮将成功加载该函数并且它可以正常工作。但是,在制作包时,点击“Build & Reload”按钮会出现以下错误:
arma.o:arma.cpp(.text $ _zn4arma6auxlib7eig_symidns_3matideeebrns_3colit_eerkns_4baseiss_4baseis5_t0_EE ):对“ dsyev_”的不确定引用
我用谷歌搜索“dsyev”,它似乎与 LAPCAK 有关。
一个办法:
在 src 文件夹中新建一个 .txt 文件。添加
PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
Run Code Online (Sandbox Code Playgroud)
在文件中。
将txt文件重命名为Makevars.win(如果不是Windows系统,则没有.win)。包制作成功。
RcppArmadillo.package.skeleton()通过比较Rstudio 生成的文件和 Rstudio 生成的文件找到了解决方案
我在 R 函数中有一个 Rcpp 函数。R 函数生成一些对象(比如一个大列表)并将其提供给 Rcpp 函数。在 Rcpp 函数中,我处理 R 对象,将结果加载到多个 C++ 类中。现在 R 对象变得无用了。我想清除 R 对象,为主要算法创建一个内存充足的环境。
这个想法是:
// [[Rcpp::export]]
void cppFun(List structuredData)
{
// copy structuredData to C++ classes
// Now I want structuredData gone to save memory
// main algorithms ...
}
/***R
rFun(input)
{
# R creates structuredData from input
cppFun(structuredData)
}
*/
Run Code Online (Sandbox Code Playgroud)
我尝试在 C++ 中调用 R 的“rm()”,但它只能识别 R 全局环境中的对象名称。例如:
// [[Rcpp::export]]
void cppFun()
{
Language("rm", "globalDat").eval();
Language("gc").eval();
}
/***R
globalDat = 1:10
ls() # shows …Run Code Online (Sandbox Code Playgroud) 我有两个 64 位整数x和y. 每个代表5个无符号短整数:前10位代表第一个整数,接下来的13位代表第二个整数,接下来的16位代表第三个整数,接下来的14位代表第四个整数,其余位代表第 5 个整数。
设x0,x1,x2,x3,x4是构成5个短整型x。设y0,y1,y2,y3,y4是构成5个短整型y。我需要知道是否x0 < y0AND x1 < y1AND x2 < y2AND x3 < y3AND x4 < y4。
我认为最简单的解决方案是转移:
bool allLess(std::size_t x, std::size_t y)
{
if(x >= y) return 0;
int shift[] = {10, 13, 16, 14};
for(int i = 0; i < …Run Code Online (Sandbox Code Playgroud) 例如,在标题之后,我想得到(1,4,5,2,7,45,8)中的第一个偶数.我可以做循环,我可以使用which(),但它们似乎不够有效.你有更好的想法吗?
谢谢!
以下代码:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector FFF(){
NumericVector LB(3);
LB[0]=Language("max",12.3,1.2,13.3,34,10,12.45).eval();
LB[1]=Language("min",12.31,1.24,13.35,340,109,121.45).eval();
LB[2]=Language("sum",12.37,1.21,13.43,34).eval();
return LB;
}
Run Code Online (Sandbox Code Playgroud)
不会通过编译器,因为"语言("max",12.3,1.2,13.3,34,10,12.45).eval())"返回SEXP对象,它不符合LB [0]的类型"双".我真的想直接使用R base中的max(),min()和sum()而不是编写额外的C++函数.你有什么好主意吗?
谢谢!
C++中有这样的要求吗
sizeof(std::vector<T>) == sizeof(std::vector<S>)
Run Code Online (Sandbox Code Playgroud)
哪里S和T是任意可复制分配和可复制构造的类型?例如,在我的 64 位 Windows 笔记本电脑上,GCC我们有
sizeof(std::vector<int>) ==
sizeof(std::vector<std::tuple<std::vector<double>,
int, std::map<int, std::string> > >)
== 24
Run Code Online (Sandbox Code Playgroud) 请注意接受的答案指出问题在于重新种植.重播不是原因.没有重新种植的测试在发布前产生了很高的相关性.见注1.
我在R中生成了1,000,000个统一随机数,对序列进行了排序,并调用std::random_shuffle()此序列的副本100次.100个置换序列非常相关.但是,如果我不首先对统一数字进行排序,那么100个置换序列或多或少是不相关的.以下是代码.
// [[Rcpp::export]]
IntegerVector testRandomShuffle(IntegerVector x, int rd) // rd is the seed
{
IntegerVector y(x.begin(), x.end()); // copy
std::srand(rd); // seeding
std::random_shuffle(y.begin(), y.end());
return y;
}
/***R
v = runif(1000000)
vSorted = sort(v)
sqc = 1L : length(v) # indexes
rd = sample.int(length(v), 100) # random seeds
# Compute correlation matrices
corMatForUnsorted = cor(as.data.frame(lapply(rd, function(x)
v[testRandomShuffle(sqc, x)])))
corMatForSorted = cor(as.data.frame(lapply(rd, function(x)
vSorted[testRandomShuffle(sqc, x)])))
# plot histograms
par(mfrow = c(1, 2))
hist(corMatForUnsorted[abs(corMatForUnsorted) < 1], breaks = 200, …Run Code Online (Sandbox Code Playgroud)