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

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.

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...

Imported packages do not work with my package in R for some functions?

I built my own package. I imported the most important package that I need them in my package. In these packages there are some functions are not exported by the package (I did not find them in the namespace of the package). I need these functions. When I call them, I get an error that those funciton are not found. So, How I can solve this problem. Also, how does these packages uses this functions inside their packages without using #export!! any help please?
based on the answer:
I understand I do it like this inside my R code: I need the following function:
args <- preproc(c(as.list(environment()), call = match.call()),
check_matrix,
check_fammat,
check_parmat,
check_par2mat)
list2env(args, environment())
Then I must do like this:
VineCopula:::preproc()
Then how to call args?
You can call non exported functions with
packagename:::functionname()
It is however not recommended to do that since those functions might not be supported in future versions of packages.
If you want to use a non exported function from your own library inside your own library, you can just use functionname() altough some package developers still prefer packagename:::functionname().

Rcpp integrated with R package: Documentation of CPP code objects

I have been developing a package with Rcpp for C++ integration. I used RcppExport to make the functions return SEXP objects.
The issue is travis-ci seems to give warnings telling that there are undocumented code objects. (These are cpp functions). However I do not want users to directly access those functions as well.
How can I resolve this issue? How can I document these functions as well?
You seem to have an elementary misunderstanding here.
If your NAMESPACE contains a wildcard 'export all' a la exportPattern("^[[:alpha:]]+") then each global symbol is exported and per R standards that are clearly documented needs a help entry.
One easy fix is NOT to export everything and just write documentation for what you want exported. We sometimes do that and call the Rcpp function something like foo_impl and then have R functions foo (with documentation) call foo_impl. In that case you would just export foo and all is good.
In short, you are confused about R packages and not so much Rcpp. I would recommend downloading the sources of a few (small) Rcpp packages to get a feel for what they do.

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.

How to use package `RcppExamples`?

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.

Resources