R Markdown 中宏“RcppExport”扩展导致的各种 C++ 语法错误

问题描述 投票:0回答:1

我有以下

.cpp
文件,我想在 R 中使用它:

#include <Rcpp.h>
#include <RcppEigen.h>

using Eigen::Map;
using Eigen::EigenBase;
using Eigen::HouseholderQR;
using Eigen::MatrixXd;
using Eigen::SparseMatrix;
using Eigen::SparseQR;
using Eigen::COLAMDOrdering;

// [[Rcpp::depends(RcppEigen)]]

// [[Rcpp::export]]
SEXP qr_dense_residop(Map<MatrixXd> X, Map<MatrixXd> Y){
  const HouseholderQR<MatrixXd> QR(X);
  return Rcpp::wrap(Y - (X * QR.solve(Y)));
}

// [[Rcpp::export]]
SEXP qr_sparse_residop(Map<SparseMatrix<double>> X, Map<SparseMatrix<double>> Y){
  const SparseQR<SparseMatrix<double>, COLAMDOrdering<int>> QR(X);
  return Rcpp::wrap(Y - (X * QR.solve(Y)));
}

如果我用

Rcpp::sourceCpp()
编译它,它会正确编译。但是,我想将源代码包含在 Rmarkdown 文档中,所以我改为这样包含它:

```{Rcpp, file="resid.cpp"}
```

然后,当我编织文档时,出现以下错误:

using C++ compiler: ‘g++ (GCC) 11.3.0’
/stornext/System/data/apps/gcc/gcc-11.3.0/bin/g++ -std=gnu++17 -I"/stornext/System/data/apps/R/R-4.3.2/lib64/R/include" -DNDEBUG   -I"/home/users/allstaff/milton.m/R/x86_64-pc-linux-gnu-library/4.3/Rcpp/include" -I"/home/users/allstaff/milton.m/R/x86_64-pc-linux-gnu-library/4.3/RcppEigen/include" -I"/vast/scratch/users/milton.m/tmp/RtmpABumCC/sourceCpp-x86_64-pc-linux-gnu-1.0.12" -I/usr/local/include    -fpic  -g -O2  -w -c file153c34131d6db.cpp -o file153c34131d6db.o
In file included from /home/users/allstaff/milton.m/R/x86_64-pc-linux-gnu-library/4.3/RcppEigen/include/RcppEigenForward.h:26,
                 from /home/users/allstaff/milton.m/R/x86_64-pc-linux-gnu-library/4.3/RcppEigen/include/RcppEigen.h:25,
                 from file153c34131d6db.cpp:1:
file153c34131d6db.cpp: In function ‘SEXPREC* qr_sparse_residop(Eigen::Map<Eigen::SparseMatrix<double, 0, int> >, Eigen::Map<Eigen::SparseMatrix<double, 0, int> >)’:
/home/users/allstaff/milton.m/R/x86_64-pc-linux-gnu-library/4.3/Rcpp/include/RcppCommon.h:140:27: error: expected unqualified-id before string constant
  140 | #define RcppExport extern "C" attribute_visible
      |                           ^~~
file153c34131d6db.cpp:33:1: note: in expansion of macro ‘RcppExport’
   33 | RcppExport SEXP sourceCpp_1_qr_dense_residop(SEXP XSEXP, SEXP YSEXP) {
      | ^~~~~~~~~~
/home/users/allstaff/milton.m/R/x86_64-pc-linux-gnu-library/4.3/Rcpp/include/RcppCommon.h:140:27: error: expected unqualified-id before string constant
  140 | #define RcppExport extern "C" attribute_visible
      |                           ^~~
file153c34131d6db.cpp:45:1: note: in expansion of macro ‘RcppExport’
   45 | RcppExport SEXP sourceCpp_1_qr_sparse_residop(SEXP XSEXP, SEXP YSEXP) {
      | ^~~~~~~~~~
file153c34131d6db.cpp:54:2: error: expected ‘}’ at end of input
   54 | }
      |  ^
file153c34131d6db.cpp:20:81: note: to match this ‘{’
   20 | SEXP qr_sparse_residop(Map<SparseMatrix<double>> X, Map<SparseMatrix<double>> Y){
      |                                                                                 ^
make: *** [file153c34131d6db.o] Error 1

这里发生了什么,如何避免这个错误?

c++ r rcpp
1个回答
0
投票

你做错了某件事,但不清楚是什么。如果您查看 knitr 及其 Rcpp 引擎的现有文档,很明显拥有

// [[Rcpp::depends(RcppEigen)]]
使用的
sourceCpp()
应该足够了......因为这就是
knitr
使用的!

这里是一个完整的 Rmd 文件,设置了一些更简单的东西:来自

getEigenValues()
存储库自述文件中的
RcppEigen
。有用。也许你可以从此开始。

---
title: "RcppEigen in RMarkdown"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

Standard `getEigenValue` example from README


```{Rcpp, eigenValues}
#include <RcppEigen.h>

// [[Rcpp::depends(RcppEigen)]]

using Eigen::Map;                       // 'maps' rather than copies
using Eigen::MatrixXd;                  // variable size matrix, double precision
using Eigen::VectorXd;                  // variable size vector, double precision
using Eigen::SelfAdjointEigenSolver;    // one of the eigenvalue solvers

// [[Rcpp::export]]
VectorXd getEigenValues(Map<MatrixXd> M) {
    SelfAdjointEigenSolver<MatrixXd> es(M);
    return es.eigenvalues();
}
```

From `?eigen`:


```{r}
M <- cbind(c(1,-1), c(-1,1))
eigen(M)   # base R
getEigenValues(M)
```

它确实有效。 (特征值对于排序是不变的,因此

c(2,0)
相当于
c(0,2)
。)

© www.soinside.com 2019 - 2024. All rights reserved.