rcpp

How to get a big sparse matrix in R? (> 2^31-1)

依然范特西╮ 提交于 2019-12-05 08:04:40
I use some C++ code to take a text file from a database and create a dgcMatrix type sparse matrix from the Matrix package. For the first time, I'm trying to build a matrix that has more than 2^31-1 non-sparse members, which means that the index vector in the sparse matrix object must also be longer than that limit. Unfortunately, vectors seem to use 32-bit integer indices, as do NumericVectors in Rcpp. Short of writing an entire new data type from the ground up, does R provide any facility for this? I don't think I can use too exotic a solution as I need glmnet to recognize the resultant

Read variables from global environment with inline Rcpp?

本小妞迷上赌 提交于 2019-12-05 06:33:23
I'm following the example from the Rcpp intro Vignette, trying it with inline. f<-cxxfunction(signature(), plugin="Rcpp", body=" Environment global = Environment::global_env(); std::vector<double> vx = global['x']; ") but I get a compile error. file12384509.cpp: In function 'SEXPREC* file12384509()': file12384509.cpp:31: error: invalid use of incomplete type 'struct SEXPREC' C:/PROGRA~1/R/R-211~1.1/include/Rinternals.h:333: error: forward declaration of 'struct SEXPREC' file12384509.cpp:31: error: conversion from 'SEXPREC' to non-scalar type 'std::vector<double, std::allocator<double> >'

Applying the optim function in R in C++ with Rcpp

梦想的初衷 提交于 2019-12-05 04:03:18
问题 I am trying to call R function optim() in Rcpp . I saw an example in Calling R's optim function from within C++ using Rcpp, but I am unable to modify it correctly for my use case. Basically, the objective function depends on the x and y but I want to optimize it with respect to b . Here is the R code that does what I want: example_r = function(b, x, y) { phi = rnorm(length(x)) tar_val = (x ^ 2 + y ^ 2) * b * phi objftn_r = function(beta, x, y) { obj_val = (x ^ 2 + y ^ 2) * beta return(obj_val

Raising exceptions in Rcpp

ⅰ亾dé卋堺 提交于 2019-12-05 03:58:51
I am trying to report errors from my rcpp code. I am using the constructor exception (const char *message_, const char *file, int line) from http://dirk.eddelbuettel.com/code/rcpp/html/classRcpp_1_1exception.html . To isolate the problem, I wrote the following bar.cpp : #include <Rcpp.h> RcppExport SEXP bar( SEXP x){ throw(Rcpp::exception("My Error Message","bar.cpp",4)); return x ; } When I run it in R, this is what I get: > dyn.load("bar.so") > is.loaded("bar") [1] TRUE > .Call("bar",12) Error: SET_VECTOR_ELT() can only be applied to a 'list', not a 'NULL' > You can either use the inline

store and retrieve matrices in memory using xptr

与世无争的帅哥 提交于 2019-12-05 03:48:05
I would like to be able to store a matrix created in R in memory and return the pointer. And then later use the pointer to fetch back the matrix from memory. I am running R version 3.0.1 (2013-05-16) -- "Good Sport" on Ubuntu 13.01 and Rcpp version "0.10.6". I have tried ... // [[Rcpp::export]] SEXP writeMemObject(NumericMatrix mat) { XPtr<NumericMatrix> ptr(&mat, true); return ptr; } // [[Rcpp::export]] NumericMatrix getMemObject(SEXP ptr) { XPtr<NumericMatrix> out(ptr); return wrap(out); } # This returns a pointer x <- writeMemObject(matrix(1.0)) But this fails and crashes R when I try again

Create two R functions with same name but different type of argument

血红的双手。 提交于 2019-12-05 03:01:26
I am creating a package with Rcpp and I want to have a function than can accept as argument a std::string or a int . How can I do it? I have the following functions: int myFunction(std::string varname); RcppExport SEXP myFunction(SEXP varname) { BEGIN_RCPP Rcpp::RObject __result; Rcpp::RNGScope __rngScope; std::string myVarname = as<std::string>(varname); __result = Rcpp::wrap(myFunction(myVarname)); return __result; END_RCPP } int myFunction(int varname); RcppExport SEXP myFunction(SEXP varname) { BEGIN_RCPP Rcpp::RObject __result; Rcpp::RNGScope __rngScope; int myVarname = as<int>(varname);

R - use primitive functions like max(), sum() in Rcpp

橙三吉。 提交于 2019-12-05 02:41:01
问题 The following codes: #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; } won't pass the compiler, since "Language("max",12.3,1.2,13.3,34,10,12.45).eval())" returns SEXP object, which doesn't fit LB[0]'s type "double". I really want to directly use max(), min() and

Can Rcpp replace unif function in R?

99封情书 提交于 2019-12-04 22:22:21
问题 I have just started using the Rcpp package in R, my learning is inspired by the Advanced R course by Hadley Wickham. Within R studio I have the following .cpp file. The question is more general but this example helps. #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] NumericVector runifC(int n, double min=0, double max=1) { NumericVector out(n); for(int i = 0; i < n; ++i) { out[i] = min + ((double) rand() / (RAND_MAX)) * (max - min); } return out; } /*** R library(microbenchmark)

Using Rcpp with Windows-specific includes

落花浮王杯 提交于 2019-12-04 21:31:37
问题 I'm trying to write some C++ code that accesses some OS-level things in Windows, using Rcpp. As soon as I include windows.h or shlobj.h , I get a bunch of compilation errors. When I run this code, it works, so I know I'm getting some of the basics right. But when I uncomment either of the Windows-related #include lines, it doesn't work. library(inline) inc <- ' #include <iostream> #include <stdio.h> // #include <windows.h> // #include <shlobj.h> using namespace std; ' src <- ' cout << "foo\\n

Rcpp: rearrange a vector in an order of another vector

霸气de小男生 提交于 2019-12-04 20:24:10
I am new in Rcpp. I need to rearrange a vector A in an order of another vector B ; For example, A=c(0.5,0.4,0.2,0.9) B=c(9,1,3,5) I want to make C=c(0.4,0.2,0.9,0.5) by Rcpp. I know the simple r code, C=A[order(B)] , but it is necessary for me to use Rcpp code. I found how to find the order of B by using sort_index , but I fail to arrange A with respect to an order of B . How can I make it? You should be able to use arma::sort_index for this, which you mention in your post: #include <RcppArmadillo.h> // [[Rcpp::depends(RcppArmadillo)]] // [[Rcpp::export]] arma::vec arma_sort(arma::vec x, arma: