python代码中的knitr重用Rcpp函数

2024-09-30 18:34:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道是否有某种方法可以“重新打包”Rcpp返回的函数,以便在python代码块中使用。你知道吗

在附加的(玩具)示例中,我想重用“Npi”函数。换言之,我希望能够取消对该行的评论:

## threePi = Npi(3)

并让python代码运行Rcpp返回给R的重新打包版本的“Npi”。你知道吗

谢谢。你知道吗

——迈克

```{Rcpp defineFn}

#include <Rcpp.h>

// [[Rcpp::plugins(cpp11)]]


// [[Rcpp::export]]
double Npi(const int N) {

  return (double(N)) * 3.14;
}

```{r useFnR}

print("This is from R")

twoPi <- Npi(2L)
print(twoPi)


```

```{python useFnPy, eval=TRUE}

sourceMsg = "This is from Python"
print(sourceMsg)

## threePi = Npi(3)
threePi = 3 * 3.14 # just to let the code render
print(threePi)

```

Tags: 方法函数代码from示例isthisdouble