How to use package `RcppExamples`? - r

I'm new to Rcpp and want to export some C++ class into R. I've install RcppExmples intending to learn some code snippet. But I didn't even know how to use it? help(RcppExamples) only to get:
Description:
This package shows some simple examples for the use of ‘Rcpp’.
Both the older ('classic') and new API are documented.
It can also serve as a working template to create packages that
use ‘Rcpp’ to interface C++ code or libraries.
Where can I get the examples?

The sources for the examples in the package are, well, in the package itself.
This is Rcpp. It works with C++ source code, and R code. You don't use this like a normal package, you create packages with it. From source.

Did you had a look at the manual? I think it's clear from this document. But the DESCRIPTION file says:
Note that the documentation in this package currently does not cover all the features in the package. It is not even close.
So I fear that's all you can get - besides looking at the source code.

Related

add and Rcpp file to an existing r Package?

I have already made a simple R package (pure R) to solve a problem with brute force then I tried to faster the code by writing the Rcpp script. I wrote a script to compare the running time with the "bench" library. now, how can I add this script to my package? I tried to add
#'#importFrom Rcpp cppFunction
on top of my R script and inserting the Rcpp file in the scr folder but didn't work. Is there a way to add it to my r package without creating the package from scratch? sorry if it has already been asked but I am new to all this and completely lost.
That conversion is actually (still) surprisingly difficult (in the sense of requiring more than just one file). It is easy to overlook details. Let me walk you through why.
Let us assume for a second that you started a working package using the R package package.skeleton(). That is the simplest and most general case. The package will work (yet have warning, see my pkgKitten package for a wrapper than cleans up, and a dozen other package helping functions and packages on CRAN). Note in particular that I have said nothing about roxygen2 which at this point is a just an added complication so let's focus on just .Rd files.
You can now contrast your simplest package with one built by and for Rcpp, namely by using Rcpp.package.skeleton(). You will see at least these differences in
DESCRIPTION for LinkingTo: and Imports
NAMESPACE for importFrom as well as the useDynLib line
a new src directory and a possible need for src/Makevars
All of which make it easier to (basically) start a new package via Rcpp.package.skeleton() and copy your existing package code into that package. We simply do not have a conversion helper. I still do the "manual conversion" you tried every now and then, and even I need a try or two and I have seen all the error messages a few times over...
So even if you don't want to "copy everything over" I think the simplest way is to
create two packages with and without Rcpp
do a recursive diff
ensure the difference is applied in your original package.
PS And remember that when you use roxygen2 and have documentation in the src/ directory to always first run Rcpp::compileAttributes() before running roxygen2::roxygenize(). RStudio and other helpers do that for you but it is still easy to forget...

CRAN submission - How should I document hidden functions in R?

Long story short:
My aim is to submit a R package developed with roxygen2 to CRAN, and I need to find some guidelines on writing and documenting hidden functions.
More details:
I am writing my first R package using roxygen2 in Rstudio. I have documented all the functions I wrote so far, so that my collaborators can easily understand their purpose before going into the details of the script. All the functions that are of no use to the user, but necessary to the package, are not exported in the namespace and eliminated from the package manual/index (#keywords internal). At the same time, my collaborators can still read their documentation using the help of Rstudio.
Eventually, I would like to remove the documentation of these "hidden functions" from the help because there is no reason to keep it. I was considering to follow a suggestion I found in other posts, that is changing #' with ## in the documentation created with roxygen2. However, I am not sure this is the correct procedure to implement, and if removing the documentation of a function in the help and the manual is compatible with CRAN requirements.
Can anyone point me to some guidelines related to this issue, or has any experience to share?

Using another R package function without using the whole package as dependency

I'm working on an R package here and got this doubt: I need an auxiliar function from another package, but I don't want to include the entire package as a dependency because I only need this one function. What is the correct procedure here? Is it OK if both codes are GPL-2 and I just copy/paste the function to my package? Should I contact the author? Or is it best to include the whole package as a dependency?
If it's just a small function, I don't see a problem with copying the code into your own package (since everything is GPLed). You should acknowledge the source in your package though.
This has the benefit of insulating your code from any changes in the other package; it's not unusual for updates to packages to break other packages downstream. It has the downside that if those updates were useful (bug fixes or added functionality) then you don't benefit from them either.

I'm writing a package. How can make it such that when library(my_package) is called, other packages are loaded as well?

Title should be pretty clear I hope. I'm writing a package called forecasting, with imports for dplyr among other packages. With the imports written in to the DESCRIPTION file, I am able to force these other packages to be installed along with forecasting - is there an equivalent way to do this for the loading of the package? In other words, is there a way that when I load my package with library(forecasting), it automatically also loads dplyr and the other packages?
Thanks
Yes.
Re-read "Writing R Extensions". The Depends: forces both the initial installation as well as the loading of the depended-upon packages.
But these days you want Imports: along with importFrom() in the NAMESPACE file which is more fine-grained.
But first things first: get it working with Depends.
Edit:
Opps you're correct, the documentation I referenced is not a primary source. Perhaps this is better:
From the R documentation:
The ‘Depends’ field gives a comma-separated list of package names which this package depends on. Those packages will be attached before the current package when library or require is called.
and
The ‘Imports’ field lists packages whose namespaces are imported from (as specified in the NAMESPACE file) but which do not need to be attached. Namespaces accessed by the ‘::’ and ‘:::’ operators must be listed here, or in ‘Suggests’ or ‘Enhances’
Original:
From the R packages documentation:
Adding a package dependency here [the DESCRIPTION file] ensures that it’ll be installed. However, it does not mean that it will be attached along with your package (i.e., library(x)). The best practice is to explicitly refer to external functions using the syntax package::function(). This makes it very easy to identify which functions live outside of your package. This is especially useful when you read your code in the future.

Is it possible to create a sharable package from a Rcpp function without other users needing Rcpp?

I made a function with cppFunction which works as expected and now I'd like for my co-workers to be able to use it. Is it possible to make a package that compiles my cpp code so that other users of the package don't need Rcpp? It seems like the guide here http://adv-r.had.co.nz/Rcpp.html#using-rcpp-in-a-package and here http://cran.r-project.org/web/packages/Rcpp/vignettes/Rcpp-package.pdf create packages that have Rcpp as a dependency. Am I reading those guides correctly that what I want to do can't easily be done?
No, you are overlooking the fact that code from Rcpp, just like code from R itself, is loaded via a so-called shared library.
So in order to run code using Rcpp, you need Rcpp installed.
Which is why packages win (again). Wrap your code in a package, have your package depend on Rcpp, and you're (essentially) done.

Resources