-
-
Notifications
You must be signed in to change notification settings - Fork 878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: rcpp code chunk compilation of {.h, .cpp} pairs or .cpp files that include other user-defined header files #2367
Comments
Thanks for the feedback. Just pointing out that the engine is defined here Lines 292 to 314 in 2dc1e4a
As always, PR welcome if you wish to contribute. Otherwise, we'll consider this for future development probably. I'll let @yihui comment on this. |
I don't understand C++ or Rcpp well enough, but have you tried to input a vector of file paths to the chunk option #| file: ["foo.h", "foo.cpp"] or ```{Rcpp, file = c("foo.h", "foo.cpp")}
``` |
Had the same issue, but my reasoning was since I already bothered to extract the header file I would just write the actual code in a file too, and use a hidden manual sourceCpp() chunk using the actual file name to get around this. It could be nicer but I think this serves the intended purpose: a code chunk is not designed to pull in code from elsewhere. @yihui This will compile two things separated unfortunately. |
Okay, again I'm not an expert on this, but will be happy to review a PR if it's not too complicated. Thank you! |
Working on it, thanks! |
Currently, an Rcpp code chunk expects a single standalone
.cpp
file.Compilation of {.h, .cpp} pairs
If you have a habit of separating function declaration in a header file with
.h
extension and function implementation in the eponymous.cpp
file, thenincluding the
cpp
file or the.h
file in a Rcpp code chunk will result infailure to compile. This is because Rcpp code chunks make use of
Rcpp::sourceCpp()
function, which only accepts a single.cpp
file by design.Compilation of .cpp files that include other user-defined header files
If you have multiple files to include in a single Rcpp code chunk, then you
cannot include them directly in the Rcpp code chunk. This is because, behind the
scene,
Rcpp::sourceCpp()
creates a -- possibly temporary -- cache directoryinto which it copies only the single input file provided in the
file
argument.Workarounds
I have found workarounds for the two above mentioned problems which I made
a repository of to experiment: https://github.com/astamm/rcpp-code-cell-processing.
In short:
myfile.h
,myfile.cpp
)into a single file (
myfile.hpp
) and then include the.hpp
file in the Rcpp code chunk.Rcpp code chunk to the cache directory created by
Rcpp::sourceCpp()
. This canbe done by setting the
cacheDir
argument of theRcpp::sourceCpp()
functionto the desired cache directory via the
cacheDir
optional argument. Behind thescene,
Rcpp::sourceCpp()
subsequently creates a sub-directory in thatdirectory whose name is platform-dependent and retrieved via
Rcpp:::.sourceCppPlatformCacheDir()
. We can therefore decide beforehand of thelocation of that cache directory, create it and copy the files we want to
include in the Rcpp code chunk to that directory.
Could it be a nice feature to have this directly handled by knitr upon knitting?
Thanks!
By filing an issue to this repo, I promise that
xfun::session_info('knitr')
. I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version:remotes::install_github('yihui/knitr')
.I understand that my issue may be closed if I don't fulfill my promises.
The text was updated successfully, but these errors were encountered: