Moving from sourceCpp to a package w/Rcpp

后端 未结 3 1319
面向向阳花
面向向阳花 2020-12-28 09:48

I currently have a .cpp file that I can compile using sourceCpp(). As expected the corresponding R function is created and the code works as expect

相关标签:
3条回答
  • 2020-12-28 09:55

    Here is my "walk through" of how to go from using sourceCpp() to a package that uses Rcpp. If there is an error please feel free to edit this or let me know and I will edit it.

    [NOTE: I HIGHLY recommend using RStudio for this process.]

    So you have the sourceCpp() thing down pat and now you need to build a package. This is not hard, but can be a bit tricky, because the information out there about building packages with Rcpp ranges from the exhaustive thorough documentation you want with any R package (but that is above your head as a newbie), and the newbie sensitive introductions (that may leave out a detail you happen to need).

    Here I use oneCpp.cpp and twoCpp.cpp as the names of two .cpp files you will use in your package.

    Here is what I suggest:

    A. First I assume you have a version of theCppFile.cpp that compiles with sourceCpp() and works as you expect it to. This is not a must, but if you are new to Rcpp OR packages, it is nice to make sure your code works in this simple situation before you move to the more complicated case below.

    B. Now build your package using Rcpp.package.skeleton() or use the Project>Create Project>Package w/Rcpp wizard in RStudio (HIGHLY recommended). You can find details about using Rcpp.package.skeleton() in hadley/devtools or Rcpp Attributes Vignette. The full documentation for writing packages with Rcpp is in Writing a package that uses Rcpp, however this one assumes you know your way around C++ fairly well, and does not use the new "Attributes" way of doing Rcpp. It will be invaluable though if you move toward making more complex packages.

    You should now have a directory structure for your package that looks something like this:

    yourPackageName
    - DESCRIPTION
    - NAMESPACE
    - \R\
        - RcppExports.R 
    - Read-and-delete-me
    - \man\
        - yourPackageName-package.Rd
    - \src\
        - Makevars
        - Makevars.win
        - oneCpp.cpp 
        - twoCpp.cpp
        - RcppExports.cpp
    

    Once everything is set up, do a "Build & Reload" if using RStudio, or compileAttributes() if you are not in RStudio.

    C. You should now see in your \R directory a file called RcppExports.R. Open it and check it out. In RcppExports.R you should see the R wrapper functions for all the .cpp files you have in your \src directory. Pretty sweet, eh?.

    D) Try out the R function that corresponds to the function you wrote in theCppFile.cpp. Does it work? If so move on.

    E) You can now just add new .cpp files like otherCpp.cpp to the \src directory as you create them. Then you just have to rebuild the package, and the R wrappers will be generated and added to RcppExports.R for you. In RStudio this is just "Build & Reload" in the Build menu. If you are not using RStudio you should run compileAttributes()

    0 讨论(0)
  • 2020-12-28 10:05

    You are missing the forest for the trees.

    sourceCpp() is a recent function; it is part of what we call "Rcpp attributes" which has its own vignette (with the same title, in the package, on my website, and on CRAN) which you may want to read. It among other things details how to turn something you compiled and run using sourceCpp() into a package. Which is what you want.

    Randomly jumping between documentation won't help you, and at the end of the genuine source documentation by package authors may be preferable. Or to put a different spin on it: you are using a new feature but old documentation that doesn't reflect it. Try to write a basic package with Rcpp, ie come to it from the other end as well.

    Lastly, there is a mailing list...

    0 讨论(0)
  • 2020-12-28 10:08

    In short, the trick is to call compileAttributes() from within the root of the package. So for instance for package foo

    $ cd /path/to/foo
    $ ls
    DESCRIPTION  man  NAMESPACE  R  src
    $ R
    R> compileAttributes()
    

    This command will generate the RcppExports.cpp and RcppExports.R that were missing.

    0 讨论(0)
提交回复
热议问题