小编Rom*_*ois的帖子

C++和R接口,获得输出

我刚开始学习R,因此陷入了琐碎的问题.我试图找出如何在R中输出值.在C++中我们只使用返回变量,但这似乎不是R的情况.假设我有一个带有4个输入参数的函数,将这些参数传递给c ++函数执行所需的计算,现在如果我想在R中加载这个myfun并从c ++函数获取输出,我需要做什么?以下是我尝试使用的模板.

extern "C" {
   SEXP myfun(SEXP S, SEXP A, SEXP B, SEXP C) { //will call this function from R.
     SEXP rate, dir, list, list_names; //declare variables

     PROTECT( rate = allocMatrix(REALSXP, 10, 2) ); //allocate 10x2 matrix of double type?
     PROTECT( dir = allocVector(INTSXP, 10) ); //allocated vector(10) of int type?

     double* p_rate = REAL(rate); //why do I need pointers? 
     int* p_dir = INTEGER(dir);
Run Code Online (Sandbox Code Playgroud)

//这里我调用一个C++函数来计算vector<vector<double> > someValvector<int> someVal2.

现在我想将这些值传递给rate和dir.

 for(int i =0; i < …
Run Code Online (Sandbox Code Playgroud)

c++ r rcpp

6
推荐指数
2
解决办法
924
查看次数

Rcpp +内联 - 创建和调用其他函数

我想知道是否有一种方法可以Rcpp使用inlinemain函数中的 包创建函数.这是我想要做的一个例子:

library(inline)
library(Rcpp)
a = 1:10
cpp.fun = cxxfunction(signature(data1="numeric"), 
                      plugin="Rcpp",
                      body="
int fun1( int a1)
{int b1 = a1;
 b1 = b1*b1;
 return(b1);
}

NumericVector fun_data  = data1;
int n = data1.size();
for(i=0;i<n;i++){
fun_data[i] = fun1(fun_data[i]);
}
return(fun_data);
                           ")
Run Code Online (Sandbox Code Playgroud)

这应该导致:

> cpp.fun(a)
[1]  1  4  9  16  25  36  49  64  81  100
Run Code Online (Sandbox Code Playgroud)

但我知道编译器不会接受在main方法中创建自己的函数.如何创建和调用另一个Rcpp函数inline而不必将其传递给R?

r rcpp

6
推荐指数
1
解决办法
3628
查看次数

如果声明在R中有9个其他条件

我有一个功能,它可以查看9种不同的可能性,并选择相应的动作,具有以下形式:

我正在做的是查找向量并确定向量中的每个条目

IF the value in the vector is 1 THEN start function B
IF the value in the vector is 2 THEN start function C
IF the value in the vector is 3 THEN start function D
IF the value in the vector is 4 THEN start function E
Run Code Online (Sandbox Code Playgroud)

等等

我想用R写这个.我只是为每一个案例加上"其他"吗?

我试过switch以下方式:

condition<-6
FUN<-function(condition){
    switch(condition,
    1 = random1(net)
    2 = random2(net)
    3 = random3(net)
    4 = random4(net)
    5 = random5(net)
    6 = random6(net)
    7 = random7(net)
    8 …
Run Code Online (Sandbox Code Playgroud)

r

6
推荐指数
3
解决办法
573
查看次数

比较R中不同长度的两个向量

我一直在努力解决这个问题,并决定在一些失败之后寻求一些帮助.

这是我的问题,我想根据当天划分这两个向量,例如2012-12-11将是3/17而2012-12-12应该是0/7.但是我似乎无法弄清楚如何做到这一点..

> ili

2012-12-11 2012-12-13 2012-12-14 2012-12-17 
     3          6          7          1 
> no.ili

2012-12-11 2012-12-12 2012-12-13 2012-12-14 2012-12-15 2012-12-16 2012-12-17 
    17          7        232        322         38         21         36 
Run Code Online (Sandbox Code Playgroud)

最后的尝试是循环遍历两个向量并将值或零添加到新向量但是当我使用%in%它时不会按顺序放置值(显然)但是如果我使用==它也不起作用..

 days.ili <- unique(one.three$timestamp)
 days <- unique(one.week$timestamp)
 ili.vec <- rep(0, length(days))

 for (i in 1:length(days)) {
     if (days.ili[i] %in% days) {
         ili.vec[i] <- ili[i]
     } else {
         ili.vec[i] <- 0
     }
 }
Run Code Online (Sandbox Code Playgroud)

我必须忘记一些事情,因为我无法看清这个问题.任何人都可以告诉我在R中实现这个目标的最佳方法吗?

也许一个选项将使用merge..

merge r vector

6
推荐指数
1
解决办法
1万
查看次数

`*tmp*`[[k]]中的错误:下标超出R的范围

我想问一下为什么我在初始化例如矢量或其他类型的列表时会出现此错误,我该如何解决?

> l <- list()
> l[[1]][1] <- 1
Error in `*tmp*`[[1]] : subscript out of bounds
Run Code Online (Sandbox Code Playgroud)

这是我需要的整个代码,实际上我想要一个这样的向量列表:

mcorrelation <- list()
for(k in 1:7){
    for (ind in 1:7){
        mcorrelation[[k]][ind] <- co$estimate
    }
}
Run Code Online (Sandbox Code Playgroud)

我应该提前初始化整个列表还是有其他方法可以避免出现此错误?

r subscript

6
推荐指数
1
解决办法
2万
查看次数

计算一系列csv文件的行数

我正在研究一个R教程并怀疑我必须使用其中一个函数,但我不确定哪个(是的,我研究了它们,但直到我对R术语更加流利,它们都很混乱).

在我的工作目录中有一个文件夹"specdata".Specdata包含数百个名为001.csv - 300.csv的CSV文件.

我正在处理的函数必须计算输入数量的csv文件的总行数.因此,如果函数中的参数是,1:10并且每个文件都有十行,则返回100.

这是我到目前为止所拥有的:

complete <- function(directory,id = 1:332) {
    setpath <- paste("/Users/gcameron/Desktop",directory,sep="/")
    setwd(setpath)
    csvfile <- sprintf("%03d.csv", id)
    file <- read.csv(csvfile)
    nrow(file)
 }
Run Code Online (Sandbox Code Playgroud)

当ID参数是一个数字(例如17)时,这可以工作.但是,如果我输入说10:50作为参数,我收到一个错误:

Error in file(file, "rt") : invalid 'description' argument
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能计算输入ID参数的总行数?

r apply lapply

6
推荐指数
1
解决办法
2万
查看次数

在R编译错误中使用C++:"RcppArmadillo.h:没有这样的文件或目录"

$ export PKG_CPPFLAGS=`Rscript -e 'Rcpp:::CxxFlags()'`
$ export PKG_LIBS=`Rscript -e 'Rcpp:::LdFlags()'`
$ R CMD SHLIB my.cpp 
g++ -I/usr/share/R/include -DNDEBUG -I/usr/local/lib/R/site-library/Rcpp/include     -fpic  -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -Werror=format-security -D_FORTIFY_SOURCE=2 -g  -c my.cpp -o my.o
my.cpp:3:27: fatal error: RcppArmadillo.h: No such file or directory
compilation terminated.
make: *** [my.o] Error 1
Run Code Online (Sandbox Code Playgroud)

RcppArmadillo.h

$ locate -i RcppArmadillo.h
/usr/local/lib/R/site-library/RcppArmadillo/include/RcppArmadillo.h
Run Code Online (Sandbox Code Playgroud)

我想知道如何指定其编译器的路径?

my.cpp 好像:

#include <RcppArmadillo.h>
#include <math.h> 

// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
using namespace arma;
// [[Rcpp::export]]
...
Run Code Online (Sandbox Code Playgroud)

我的操作系统是Ubuntu 12.04.R是R版本3.1.0(2014-04-10).我刚安装RcppRcppArmadillo …

c++ r armadillo rcpp

6
推荐指数
1
解决办法
4530
查看次数

自定义刷新实现

我试图遵循的逻辑这个问题,创建一个自定义streambufRcpp.有人贡献了基本行为,允许我们写出类似的东西

Rcout << "some text" ;
Run Code Online (Sandbox Code Playgroud)

我们在哪里实现xsputnoverflow重定向到Rprintf功能.

std::streamsize Rcpp::Rstreambuf::xsputn(const char *s, std::streamsize num ) {
    Rprintf( "%.*s", num, s );
    return num;
}

int Rcpp::Rstreambuf::overflow(int c ) {
    if (c != EOF) {
        Rprintf( "%.1s", &c );
    }
    return c;
}
Run Code Online (Sandbox Code Playgroud)

我也想实现刷新,即支持这种语法:

Rcout << "some text" << std::flush ;
Run Code Online (Sandbox Code Playgroud)

我需要实现哪种方法,以便flush操纵器在我的自定义流上工作?

c++ iostream

5
推荐指数
1
解决办法
553
查看次数

Windows 7上的内联函数的doParallel问题(适用于Linux)

我在Windows 7和Linux(SUSE Server 11(x86_64))上都使用R 3.0.1.以下示例代码在Windows上产生错误,但在Linux上不产生错误.列出的所有工具箱在两台机器中都是最新的.Windows错误是:

Error in { : task 1 failed - "NULL value passed as symbol address"
Run Code Online (Sandbox Code Playgroud)

如果我更改%dopar% to %do%,Windows代码运行没有任何错误.我最初的猜测是,这与Windows中的一些配置问题有关,我尝试重新安装Rcpp和R,但这没有帮助.该错误似乎与作用域有关 - 如果我在f1中定义并编译函数cFunc,那么%dopar%可以正常工作,但是因为我们为每个任务调用一次编译器,所以它非常慢.

有没有人对错误发生的原因或如何解决它的建议有一些见解?

library(inline)
sigFunc <- signature(x="numeric", size_x="numeric")
code <- ' double tot =0;
for(int k = 0; k < INTEGER(size_x)[0]; k++){
tot += REAL(x)[k];
};
return ScalarReal(tot);
' 
cFunc <- cxxfunction(sigFunc, code)

f1 <- function(){
x <- rnorm(100)
a <- cFunc(x=x, size_x=as.integer(length(x)))
return(a)
}

library(foreach)
library(doParallel)
registerDoParallel()
# this produces an error in …
Run Code Online (Sandbox Code Playgroud)

r inline

5
推荐指数
1
解决办法
1930
查看次数

如何使用R和Rcpp删除NumericVector中的元素以进行递归

我试图了解更多关于如何使用Rcpp包的信息.所以我开始使用Rcpp测试基本的排序算法.我在这里开始了Hadley Wickham教程.

我以这种方式递归地成功实现了插入排序:

library(Rcpp)

vetor<-sample(100)
vetor
cppFunction("
    NumericVector insertionsortRC(NumericVector vetor, int n) {
        double aux;
        int i;

        if(n>1) {
            insertionsortRC(vetor,n-1);
            aux=vetor[n-1];
            i=n-1;
            while(vetor[i-1]>aux && i>=0 ) {
                vetor[i]=vetor[i-1];
                i--;
                }
            vetor[i]=aux;
            }

        return vetor;
        }
    ")
Run Code Online (Sandbox Code Playgroud)

但函数要求2个参数,然后我尝试这样:

cppFunction("
    NumericVector insertionsortRC(NumericVector vetor) {
        int n = vetor.size();

        double aux;
        int i;

        if(n>1) {
            vetor.erase(n-1);
            insertionsortRC(vetor);
            aux=vetor[n-1];
            i=n-1;
            while(vetor[i-1]>aux && i>=0 ) {
                vetor[i]=vetor[i-1];
                i--;
                }
            vetor[i]=aux;
            }

        return vetor;
        }
    ")
Run Code Online (Sandbox Code Playgroud)

我认为擦除在这里不是一个好主意,似乎我从内存中擦除了元素,并且在递归调用之后无法恢复它.我还想到问题可能在于vetor.erase(n-1); line,试过vetor.erase(n); 它编译了,但根本没用.

使用vetor.erase(n); 我在R中遇到以下错误:

insertionsortRC(vetor) …

c++ recursion r insertion-sort rcpp

5
推荐指数
1
解决办法
1861
查看次数

标签 统计

r ×9

c++ ×4

rcpp ×4

apply ×1

armadillo ×1

inline ×1

insertion-sort ×1

iostream ×1

lapply ×1

merge ×1

recursion ×1

subscript ×1

vector ×1