【问题标题】:Having trouble in building Rpackage using R/C++ functions使用 R/C++ 函数构建 Rpackage 时遇到问题
【发布时间】:2016-04-02 04:38:42
【问题描述】:

我有一个使用 Rcpp packgae 在 R 函数中调用的 C++ 函数。 R 函数接受 inputDataFrame 并使用 C++ 函数(也接受 DataFrame)计算药物量 (A1) 作为时间函数。 R 然后返回 inputDataFrame 并添加计算金额 A1 的列。

我在为这个函数制作 Rpackage 时遇到了麻烦。我关注了RStudio instruction,但在构建包时遇到了错误。错误在RcppExport.cpp 文件中,并指出'OneCompIVbolusCpp' was not declared in this scope

这里是 C++ 和 R 函数的代码。当我处理示例数据框时,它们在 R 中工作得非常好。

函数OneCompIVbolus_Rfunction.R:

library(Rcpp)
sourceCpp("OneCompIVbolusCppfunction.cpp")

OneCompIVbolusRCpp <- function(inputDataFrame){

  inputDataFrame$A1[inputDataFrame$TIME==0] <- inputDataFrame$AMT[inputDataFrame$TIME==0]
  OneCompIVbolusCpp( inputDataFrame )

  inputDataFrame
}

C++ 函数OneCompIVbolusCppfunction.cpp:

#include <Rcpp.h>
#include <math.h>
#include <iostream>
using namespace Rcpp;
using namespace std;

// [[Rcpp::export]]
// input Dataframe from R
DataFrame OneCompIVbolusCpp(DataFrame inputFrame){

  //  Create vectors of each element used in function and for constructing output dataframe
  Rcpp::DoubleVector TIME = inputFrame["TIME"];
  Rcpp::DoubleVector AMT = inputFrame["AMT"];
  Rcpp::DoubleVector k10 = inputFrame["k10"];
  Rcpp::DoubleVector A1 = inputFrame["A1"];

  double currentk10, currentTime, previousA1, currentA1;

  // in C++ arrays start at index 0, so to start at 2nd row need to set counter to 1
  // for counter from 1 to the number of rows in input data frame
  for(int counter = 1; counter < inputFrame.nrows(); counter++){
  // pull out all the variables that will be used for calculation
    currentk10  = k10[ counter ];
    currentTime = TIME[ counter ] - TIME[ counter - 1];
    previousA1  = A1[ counter - 1 ];

    // Calculate currentA1
    currentA1 = previousA1*exp(-currentTime*currentk10);

    // Fill in Amounts and check for other doses
    A1[ counter ] = currentA1 + AMT[ counter ];

  } // end for loop
  return(0);
}

关于我在这里做错了什么的任何提示?我该如何解决这个问题?

编辑

这是在 R 中运行复合函数 OneCompIVbolusRCpp 的示例:

library(plyr)
library(Rcpp)

source("OneCompIVbolus_Rfunction.R")

#-------------
# Generate df 
#-------------
#Set dose records:
dosetimes <- c(0,12)
#set number of subjects
ID <- 1:2
#Make dataframe
df <- expand.grid("ID"=ID,"TIME"=sort(unique(c(seq(0,24,1),dosetimes))),"AMT"=0,"MDV"=0,"CL"=2,"V"=10)
doserows <- subset(df, TIME%in%dosetimes)
#Dose = 100 mg, Dose 1  at time 0
doserows$AMT[doserows$TIME==dosetimes[1]] <- 100
#Dose 2 at 12
doserows$AMT[doserows$TIME==dosetimes[2]] <- 50
#Add back dose information
df <- rbind(df,doserows)
df <- df[order(df$ID,df$TIME,df$AMT),]       # arrange df by TIME (ascending) and by AMT (descending)
df <- subset(df, (TIME==0 & AMT==0)==F) # remove the row that has a TIME=0 and AMT=0
df$k10 <- df$CL/df$V
#-------------
# Apply the function 
#-------------
simdf <- ddply(df, .(ID), OneCompIVbolusRCpp)

【问题讨论】:

  • github 链接到包,mebbe?

标签: c++ r rcpp r-package


【解决方案1】:

您可能只是订购错误。而不是

// [[Rcpp::export]]
// input Dataframe from R
DataFrame OneCompIVbolusCpp(DataFrame inputFrame){
   // ...

// input Dataframe from R
// [[Rcpp::export]]
DataFrame OneCompIVbolusCpp(DataFrame inputFrame){
   // ...

因为[[Rcpp::export]] 标签必须直接出现在它导出的函数之前。

【讨论】:

  • 是的!谢谢你!如果可能的话,我还有一个问题。似乎我不能将roxygen2 包用于使用 Rcpp 的 R 包?它对我不起作用,但当我将我的功能用作 *.R 文件时,我工作正常?
  • 是的,你可以!将 roxygen2 的东西添加到 C++ 函数中,这将被带到 roxygen2 接管的 R 文件中?
  • 我尝试在 Cpp 函数代码中添加一些 roxygen 注释,但没有成功。适当地遗漏了什么?
  • 这是我提交的just today
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-30
  • 1970-01-01
  • 2012-09-21
  • 1970-01-01
  • 2012-05-11
  • 1970-01-01
相关资源
最近更新 更多