我想使用Rcpp来提高我的一些R代码的速度.但是,我对C++的了解很少.因此,我检查了Rcpp提供的文档以及Dirk Eddelbuttel网站上提供的其他文档.在阅读完所有内容之后,我尝试执行一个我在R中写的简单循环.不幸的是,我无法做到.这是R函数:
Inverse Wishart
beta = matrix(rnorm(15),ncol=3)
a = rnorm(3)
InW = function(beta,a) {
n = nrow(beta)
p = ncol(beta)
I = diag(rep(1,times = p))
H = matrix(0,nrow=p,ncol=p)
for(i in 1:n){
subBi = beta[i,]
H = H + tcrossprod(a - subBi)
}
H = H + p * I
T = t(chol(chol2inv(chol(H))))
S = 0
for(i in 1:(n+p)){
u <- rnorm(p)
S = S + tcrossprod(T %*% u)
}
D = chol2inv(chol((S)))
ans = list(Dinv = S,D=D)
}
Run Code Online (Sandbox Code Playgroud)
我真的很感激,如果有人可以帮助我,因为它将成为学习Rcpp的起点.
#include "rtest.h"
#include <iostream>
SEXP rcpp_hello_world ()
{
using namespace Rcpp ;
CharacterVector x = CharacterVector::create( "foo", "bar" );
NumericVector y = NumericVector::create( 0.0, 1.0 );
List z = List::create (x, y);
return z;
}
void funcA ()
{
std :: cout << "\nsdfsdfsdf\n";
}
int main () {return 0;}
Run Code Online (Sandbox Code Playgroud)
如何
在上面的代码中放置
library(RgoogleMaps)
和
png (filename="Rg.png", width=480, height=480)
内部?
我把它当作: R CMD SHLIB rtest.cpp
> sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: x86_64-unknown-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 …Run Code Online (Sandbox Code Playgroud) 我正在使用共享内存(由C提供的shmget系统调用)从RCpp程序内部分配共享内存.这里因为我没有使用与R的内存管理相关的标准Calloc功能,我应该注意哪些危险?R的内存管理是否会将shmget分配的空间视为可用空间并尝试过度写入任何内容?如果是这种情况,可以采取哪些措施来避免这种情况?
谢谢Vineeth
我有以下cpp函数,并希望在R中编写它
我使用rcpp包编译并使用它但发生了一些错误
实际上我在R中使用指针时遇到问题
void creationPI(double *distC, int mC, int tailleC, double *PIC,int aC)
//distC: distribution de X ; mC= smax-smin+1 ie u+v+1; tailleC=a+1; PIC la matrice PI comme resultat
{
double *f;//=NULL; /*f(k)=P[X<=k]*/
int t1,k,b,c,l;
int top_un,top_deux;
//FILE *matrix;
t1=2*(aC-1)+1; // taille du vecteur des f ca va de 1-a à a-1 ; k va de [0 à 2*(a-1)]
/* ALLOCATION DES MATRICES*/
//if (!(f = (double *)calloc(t1, sizeof(double))))
//exit(ALLOC_ERROR);
f = (double *)calloc(t1, sizeof(double));
/* CREATION DES f */ …Run Code Online (Sandbox Code Playgroud) 在Rcpp中以数字方式计算以下内容的最佳方法是什么?
exp(-1500)/(exp(-1500)+exp(-1501))
在许多情况下,计算可能需要多精度(对于exp),但最终结果可以舍入到通常的double.
通过quadmath?通过提升?
如果你留在R(在Rcpp之外),那里有非常舒适的包装工作:
library(Rmpfr)
a = mpfr(-1500,100)
b = mpfr(-1501,100)
exp(a)/(exp(a)+exp(b))
Run Code Online (Sandbox Code Playgroud)
但是如何使用rcpp访问?
我使用滚动加权移动平均函数,其代码如下所示.它通过Rcpp用C++编写.此功能适用于大多数时间系列,没有循环问题或类似的东西.我在下面提供了一系列长度为2的系列,有时会触发致命错误.我找不到错误的原因.
谢谢你的帮助!=)
这是R代码:
# Install packages
sourceCpp("partialMA.cpp")
spencer_weights=c( -3, -6, -5, 3, 21, 46, 67, 0, 67, 46, 21, 3, -5, -6, -3)
spencer_ma <- function(x) roll_mean(x,spencer_weights)
x=c(11.026420323685528,0.25933761651337001)
spencer_ma(x) # works
for(i in 1:1000) spencer_ma(x) # triggers the fatal error
Run Code Online (Sandbox Code Playgroud)
我在下面包含了我的roll_mean函数的C++代码:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector roll_mean(const NumericVector& x,
const NumericVector& w) {
int n = x.size();
int w_size = w.size();
int size = (w_size - 1) / 2;
NumericVector res(n);
int i, ind_x, ind_w;
double w_sum …Run Code Online (Sandbox Code Playgroud) 我有输入向量,如:
x1 <- c('NA', 'NA', 'NA')
x2 <- c(NA, NA, NA)
Run Code Online (Sandbox Code Playgroud)
我想测试(没有循环),如果这些向量包含NA值或字符值.
我在尝试:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector timesTwo(NumericVector x) {
if (all(is_na(x)) || (x == NA_STRING))
{
Rcout << "NA";
}
return x * 2;
}
/*** R
x1 <- c('NA','NA','NA')
x2 <- c(NA,NA,NA)
timesTwo(x1)
timesTwo(x2)
*/
Run Code Online (Sandbox Code Playgroud)
但它让我失望:
passing 'const LHS_TYPE {aka const Rcpp::sugar::SingleLogicalResult....discards qualifiers
我知道错误来自于我必须使用x作为字符串向量并访问每个元素x(1) == NA_STRING.
所以,我想让x作为一个数字向量,但检查它是否是一个字符向量,就像在R中:
all(is.character(x)
我想"width"在Rcpp中获得一个选项(例如)的值.我试过了:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
int test_option() {
Environment base("package:base");
Function get_option = base["getOption"];
return get_option("width");
}
// [[Rcpp::export]]
int test_option2() {
Environment base("package:base");
Function get_option = base["options"];
List l_width = get_option("width");
return l_width[1];
}
Run Code Online (Sandbox Code Playgroud)
第一个函数不编译,第二个函数崩溃会话.
知道怎么做吗?
我正在尝试在Rcpp中实现命名列表调用
在R
b<-list("bgroups"=c(1,1,1,1,0,0,0,0))
> b$bgroups
[1] 1 1 1 1 0 0 0 0
cppFunction(
"
NumericVector split(Rcpp::List & b){
Rcpp::NumericVector c= b['bgroups'];
return c;
}")
split(b)
Run Code Online (Sandbox Code Playgroud)
但这导致我的R会话中止.
我正试图实现这个程序,如Dirk的一个演示中所示,但我遗漏了一些东西.
我正在尝试用Rcpp编写R包。除发出的警告外,一切都正常R CMD check my_package。
Status: 1 WARNING
checking for missing documentation entries ... WARNING
Undocumented code objects:
‘shiny_function’
All user-level objects in a package should have documentation entries.
Run Code Online (Sandbox Code Playgroud)
这shiny_function是用C ++实现的,并使用Rcpp属性导出
// [[Rcpp::export]]
int shiny_function(int arg) {
return arg;
}
Run Code Online (Sandbox Code Playgroud)
问题是,我想“重命名”它shiny.function出口至R时,所以R/shiny_function.R我有
shiny.function <- function(arg) {
.Call("_my_package_shiny_function", arg)
}
Run Code Online (Sandbox Code Playgroud)
收到此警告后,我进行了修改NAMESPACE,exportPattern("^[^_]+")以尝试避免导出名称中带有下划线的函数,但该警告仍然存在。
如何“重命名”用C ++编写的Rcpp函数?