问题标签 [rcpp11]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
rcpp - Rcpp - 公开一个包含指向 R 的指针的 C++ 函数
我正在使用 Rcpp 在 R 中创建一个利用 C++ 代码的包。我已阅读所有 Rcpp 小插曲,但我无法找到以下问题的解决方案。
C++
我尝试使用的类之一包含一个指针。我正在使用模块公开课程。当我尝试在 R 中安装软件包时,出现以下错误。
我究竟做错了什么?
类包含指针的代码
对应RCPP模块的代码
c++ - RCPP - 不知道如何利用 Rcpp::XPtr 来包装指针
我正在编写一个使用 rcpp 的 r 包。
我在 C++ 中有以下类:
我编写了以下 RCPP 模块,以便将该类公开给 r:
当我尝试在 R 中安装包时,我遇到了问题,因为ffm_float *w不能直接转换为 SEXP。根据我在这里提出的上一个问题,建议我尝试使用 XPtr 来包装指针。但是,我很难找到有用的文档,我什至不知道从哪里开始。
为了解决这个问题,我写了下面的前向声明,但我不知道我是否朝着正确的方向前进。
有人可以指出我正确的方向吗?我已经阅读了 RCPP 扩展小插图。
std - 具有 Rcpp 的多臂强盗
我正在从这里翻译多臂强盗的 epsilon-greedy 算法。这是对 Rcpp 的强大和优雅的一个很好的展示。但是,此版本的结果与上面链接中提到的结果不符。我知道这可能是一个非常小众的问题,但没有其他地方可以发布这个!
代码摘要如下。基本上,我们有一组手臂,每个手臂都以预定义的概率支付奖励,我们的工作是证明,通过从手臂中随机抽取,同时间歇性地抽取具有最佳奖励的手臂,最终可以让我们收敛到最好的手臂上。John Myles White提供了对该算法的一个很好的解释。
现在,到代码:
显然,尽管奖励很低,但第一只手臂的选择不成比例!到底是怎么回事?
c++ - 自动生成 Makevars 和配置文件
朋友们,
有自动生成Makevars
和的方法configure
吗?我正在使用带有g++编译器的 bash 终端编译我的 C++ 代码。
示例Rcpp.h
:我使用使用头文件的库编写了以下代码gsl/gsl_sf_bessel.h
,也就是说,我使用了Rcpp外部的库。
在上面的例子中,考虑到我的电脑头文件的配置和路径,我可以在 bash 终端中使用下面的代码编译上面的代码:
根据我计算机上库的设置和路径,我可以使用上面的代码生成目标文件和 .so 文件。然而,正如我们所知,当函数在与包用户不同的机器上编译时,这些路径不一定有效。为此,有Makevars
和configure
文件。
问题:有没有办法根据上述构建语句自动创建Makevars
和文件?configure
我Makevars
用上面的内容创建了文件。但是,我不确定是否有办法使用前面介绍的构建语句自动执行此操作。
r - 我是否需要在 Imports 和 LinkingTo 中指定 Rcpp 以避免 Makevars 文件?
根据Rcpp FAQ (2.15. What about the new 'no-linking' feature),因为 Rcpp 版本 0.11.0 我们可以避免在 Makevars 文件中指定 LAPACK/BLAS/Fortran,实际上如果我们遵循,则完全避免 Makevars这些说明。
... only two things are required:
• an entry in DESCRIPTION such as Imports: Rcpp (which
may be versioned as in Imports: Rcpp (>= 0.11.0)), and
• an entry in NAMESPACE to ensure Rcpp is correctly instantiated,
for example importFrom(Rcpp, evalCpp).
但是不是Imports
为 Rcpp 添加一行,aLinkingTo
也可以吗?也就是说,我可以使用:
代替:
还是两者都需要?
r - Rfast 安装:/usr/lib/R/etc/Makeconf:168:目标“Norm.o”的配方失败
我正在尝试安装 Rfast 包。它给了我以下错误,
完整的错误信息是这样的,
我的 sessioninfo 如下,
有没有人可以帮我解决这个问题?
r - 在 cppFunction 之外使用 R 中的预分配值
我想知道当我编写 cppFunction 时是否有一种简单的方法可以调用 r 中的预分配值。
这是我的代码:
错误:在 sourceCpp(code = code,env = env,rebuild =rebuild,cacheDir = cacheDir,:在构建共享库时发生错误 1。
我知道如果我在函数y
内部分配demo_fun_cpp()
,它会起作用。但是,R中已经分配了很多值,所以如果我不必再次分配它们会很方便。
我是 C++ 新手,我提前感谢任何帮助!
r - RcppArmadillo gamma distribution differs between platforms with same seed
I am working on a package, which uses random numbers from RcppArmadillo. The package runs MCMC algorithms, and to obtain exact reproducibility, the user should be able to set a random number seed. When doing this, it seems like the arma::randg()
function for generating random numbers from the gamma distribution returns different values across platforms. This is not the case for arma::randu()
or arma::randn()
. Could it be related to the fact that arma::randg()
requires C++11?
Here is what I get on macOS 10.13.6, running R3.5.2:
Created on 2019-02-22 by the reprex package (v0.2.1)
Here is what I get on Windows 10, running R3.5.2:
Created on 2019-02-22 by the reprex package (v0.2.1)
As can be seen, the random numbers generated with arma::randg()
are internally consistent, but differ between the platforms.
I tried to set the seed using the instructions in the Armadillo documentation:
Created on 2019-02-22 by the reprex package (v0.2.1)
However, as the warning tells, and the result shows, the seed does not get set this way.
Is there a way to get reproducible results between platforms when using arma::randg()
, or do I need to implement a gamma distribution using some of the other random number generators available in RcppArmadillo?
Update
As pointed out in the comment, using R::rgamma()
solves this problem. The following code returns the same numbers on both Mac and Windows:
Created on 2019-02-22 by the reprex package (v0.2.1)
This solves it for me. However, I am not sure if the issue is solved, since this seems like unexpected behavior, so leaving it open.
r - Rcpp 在两个函数之间共享相同的 roxygen
考虑以下函数:
请注意,对于 foo1 和 foo2,参数a 的描述应该相同。我有许多功能,应该以相同的方式提供参数描述。每次我分别为每个函数需要它时,我都不想复制过去的参数描述。是否可以指定描述并将其应用于我的 Rcpp 包提供的所有功能?
将非常感谢您的帮助!
c++ - 潜在狄利克雷分配中的 Eigen::SparseMatrix 中的快速元素访问
我正在 Rcpp 中实现潜在狄利克雷分配 (LDA)。在 LDA 中,我们需要处理一个巨大的稀疏矩阵(例如 50 x 3000)。
我决定在 Eigen 中使用 SparseMatrix。但是,由于我需要访问每个单元格,计算成本高昂 .coeffRef
会大大减慢我的功能。
有什么方法可以在保持速度的同时使用 SparseMatrix?
我想做的有四个步骤,
- 我知道我想访问哪个单元格 (i,j)。
- 我想知道单元格 (i,j) 是否为 0。
- 如果单元格 (i,j) 不为 0,我想知道它的值。
- 在对第 2 步和第 3 步中的值进行一些分析后,我想更新单元格 (i,j)。在这一步中,我可能需要更新原来为 0 的单元格 (i,j)。
由于行数远小于列数,我考虑访问一列,然后检查行值,但我无法处理SparseMatrix<double>::InnerIterator it(spmat, colid)
.