我想从 中获取最常见的值(例如模式)IntegerVector。我只能使用 Rcpp 糖函数。
如何将输出从 转换String为int?
我的代码:
// [[Rcpp::export]]
String pier(NumericVector x) {
IntegerVector wyniki;
int max;
wyniki = Rcpp::table(x);
max = which_max(wyniki);
CharacterVector wynik_nazwy = wyniki.attr("names");
String wynik = wynik_nazwy[max];
return wynik;
}
/***R
pier(c(3,2,2,2,2,4,4,5))
*/
Run Code Online (Sandbox Code Playgroud)
维尼克:
> pier(c(3,2,2,2,2,4,4,5))
[1] "2"
Run Code Online (Sandbox Code Playgroud)
这是正确的,但我需要数字值2而不是"2"我目前收到的字符串值。此外,我需要在 Rcpp 中转换它,而不是在将函数导出到 R 之后,
我有一个包含 300 个元素的 R 嵌套列表对象(称为Rlist),每个元素都包含一个包含 1-100 个元素的“内部列表”。我想将Rlist传递给我在 Rcpp 中编写的函数,但是我无法弄清楚如何在 Rcpp 中提取内部列表。
如何将Rlist传递给我的 Rcpp 函数?特别是,假设我想访问第 10 个内部列表对象中的所有元素。我怎样才能做到这一点?我尝试将Rlist作为“列表”对象传递给 Rcpp,然后尝试Rlist(9)但这并没有给我想要的东西。
谢谢
是否可以使用 Rcpp 创建具有属性的列表?如果是这样,如何?
我需要这样一个shinyTree包列表,它需要这种结构,而我的 R 代码很慢,因为我需要几个嵌套循环来遍历所有列表级别。
这是我需要的结构:
list(Name1 = structure("", type = "root", sticon = "fa-icon", stclass = "color"))
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)$Name1 [1] "" attr(,"type") [1] "root" attr(,"sticon") [1] "fa-icon" attr(,"stclass") [1] "color"
我正在尝试编写一个函数,该函数可以将函数作为 Rcpp 中的参数。我在 R 中编写了一个示例函数,它显示了我想要的功能类型:
simulate_and_evaluate <- function(simulate, evaluate) {
y <- simulate(1)
eval <- evaluate(y)
return(eval)
}
simulate_fun <- function(n) rnorm(n, 0, 1)
evaluate_fun <- function(x) dnorm(x, 0, 1)
simulate_and_evaluate(simulate = simulate_fun,
evaluate = evaluate_fun)
Run Code Online (Sandbox Code Playgroud)
在这个函数中simulate_and_evaluate,它接受两个都是函数的参数,一个模拟一个数字,一个用这个模拟数字计算一个函数。例如,我们可以从标准法线模拟一个值,并评估该点标准法线的密度。有谁知道在 Rcpp 中是否有办法做到这一点?
下午好 ,
我们知道在 R 中,我们可以以这种方式检索 A = { 1 , 2 , ... , n } 之间 k 个元素的所有可能组合:
示例: A = { 1 , 2, ,3 ,4 ,5 } 和 K = 3
> C_wo <- combn(1:5, 3)
> C_wo
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 1 1 1 1 1 2 2 2 3
[2,] 2 2 2 3 3 4 3 3 4 4
[3,] 3 4 5 4 5 5 …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Rcpp 在 R 中运行 C 代码,但不确定如何转换用于保存文件数据的缓冲区。在下面的第三行代码中,我分配了一个 unsigned char 缓冲区,我的问题是我不知道要使用什么 Rcpp 数据类型。将数据读入缓冲区后,我想出了如何使用 Rcpp::NumericMatrix 来保存最终结果,而不是字符缓冲区。我已经看到 Dirk Eddelbuettel 对类似问题的几个回答,他建议用 Rcpp 初始化命令替换所有“malloc”调用。我尝试使用 Rcpp::CharacterVector,但最后在循环中存在类型不匹配:Rcpp::CharacterVector 无法读取为 unsigned long long int。该代码为某些 C 编译器运行,但为其他编译器抛出“内存损坏”错误,
FILE *fp = fopen( filename, "r" );
fseek( fp, index_data_offset, SEEK_SET );
unsigned char* buf = (unsigned char *)malloc( 3 * number_of_index_entries * sizeof(unsigned long long int) );
fread( buf, sizeof("unsigned long long int"), (long)(3 * number_of_index_entries), fp );
fclose( fp );
// Convert "buf" into a 3-column matrix.
unsigned long long int …Run Code Online (Sandbox Code Playgroud) 我在 Rcpp 中写了以下代码(抱歉,如果它有点马虎。这是我在 Rcpp 中的第一次体验):
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
using namespace arma;
// [[Rcpp::export]]
Rcpp::NumericMatrix rr (Rcpp::NumericMatrix edge,Rcpp::NumericMatrix riskset,
Rcpp::NumericVector nodes, double memory,int type) {
int n = nodes.size();
int nedge = n*(n-1);
Rcpp::NumericMatrix ref = riskset;
//rownames(x) = CharacterVector::create("a", "b", "c");
colnames(ref) = CharacterVector::create("sender", "receiver");
Rcpp::NumericMatrix RE(edge.nrow(),ref.nrow());
for (int ee1 = 1; ee1 < edge.nrow(); ee1++) {
Rcpp::NumericVector recencySend (nedge, 0);
Rcpp::NumericVector recencyReceiv (nedge, 0);
Rcpp::NumericVector recencyContinue (nedge, 0);
for (int ee2 = 0; ee2 < …Run Code Online (Sandbox Code Playgroud) 我是 Rcpp 的新手,这可能是一个愚蠢的问题,但我似乎无法让 seq_len 在 rcpp 中工作,即使我知道它应该是Rcpp 具有的类似 R 的函数。这是我的代码:
cppFunction("NumericVector foo2(int n){
IntegerVector x = Rcpp::seq_len(n);
return x;
}")
Run Code Online (Sandbox Code Playgroud)
这会导致错误消息:“错误:无法从类型为‘Vector<13>’的返回值转换为函数返回类型‘Vector<14>’ return x;”
我有一个包https://github.com/tfrostig/RSEE,其中包含几 (3) 个 RcppArmadillo 函数。该软件包在其他计算机上运行良好。当我构建包时没有出现错误,但是每当我调用任何 RCPP 函数时,它都会导致 R 崩溃。
当我尝试使用我的单元测试时,我收到错误: "Exited with status -1073741819" 。
如果我使用Rcpp::sourceCpp()然后调用函数,一切正常。其他带有 Rcpp 函数的包运行良好。
例如:
`// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;
using namespace arma;
// [[Rcpp::export]]
arma::mat localRegression(arma::mat weightmat, arma::mat modelmat, arma::vec xtemp) {
return inv(modelmat.t() * weightmat * modelmat) * modelmat.t() * weightmat * xtemp;
}
Run Code Online (Sandbox Code Playgroud)
使用RSEE:::localRegression会导致它崩溃。如果我使用加载源代码sourceCpp然后调用localRegression它就可以了。
什么会导致这种类型的问题?
The session info is:
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 …Run Code Online (Sandbox Code Playgroud) 考虑 R 中的可重现示例:
test <- c(1:12)
> test
[1] 1 2 3 4 5 6 7 8 9 10 11 12
Run Code Online (Sandbox Code Playgroud)
预期结果:
test.list <- split(test, gl(2, 3))
> test.list
$`1`
[1] 1 2 3 7 8 9
$`2`
[1] 4 5 6 10 11 12
Run Code Online (Sandbox Code Playgroud)
我正在尝试用 C++ 编写等效的代码来生成并返回由 test.list 产生的两个向量。请注意,我在 C++ 中处于尴尬的新手阶段。