2016-04-02 9 views
0

Ich habe eine C++ Funktion, die innerhalb einer R-Funktion mit Rcpp Packgae aufgerufen wird. Die R-Funktion akzeptiert inputDataFrame und verwendet die C++ - Funktion (akzeptiert auch einen DataFrame), um die Arzneimittelmengen (A1) als Funktion der Zeit zu berechnen. R gibt dann die inputDataFrame mit addierter Spalte für die berechneten Mengen A1 zurück.Probleme beim Erstellen von Rpackage mit R/C++ Funktionen

Ich habe Probleme, ein Rpackage für diese Funktion. Ich folgte RStudio instruction, aber ich lief beim Erstellen des Pakets in einen Fehler. Der Fehler ist in der Datei RcppExport.cpp und besagt, dass 'OneCompIVbolusCpp' was not declared in this scope.

Hier sind die Codes für die C++ - und R-Funktionen. Sie funktionieren tadellos in R, wenn ich einen Beispieldatenrahmen verarbeite.

Rfunction 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++ Funktion 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); 
} 

Irgendwelche Hinweise auf das, was ich falsch hier? Wie kann ich dieses Problem lösen?

bearbeiten:

Hier ist ein Beispiel für die zusammengesetzte Funktion OneCompIVbolusRCpp in R ausgeführt wird:

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) 
+0

github Link zum Paket, mebbe? – hrbrmstr

+0

@hrbrmstr wie ?? – Amer

Antwort

1

Sie einfach die falsche Reihenfolge haben. Statt

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

tun

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

als [[Rcpp::export]] Tag muss kommen direkt vor der Funktion exportiert.

+0

Ja! Danke! Ich habe eine andere Frage, wenn möglich. Es scheint nicht, dass ich das Paket 'roxygen2' für R-Pakete verwenden kann, die Rcpp verwenden? Es funktionierte nicht mit mir, aber ich arbeite gut, wenn ich meine Funktion als '* .R' Dateien habe? – Amer

+0

Ja kannst du! Fügen Sie roxygen2 stuff zur C++ Funktion hinzu, dies wird in die R Datei übertragen, wo roxygen2 das Kommando übernimmt. –

+0

Ich habe versucht, einige roxygen Kommentar in den Cpp-Funktionscode hinzuzufügen, aber es hat nicht funktioniert. Richtig etwas vermisst? – Amer

Verwandte Themen