Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a r package with a C++ dll in windows and ensuring portability to linux

Tags:

r

Working in Windows, I've created an r package that links to a c++ dll as a shared library. This works fine and installs without problems on Windows. When I switch to linux, however, the so is not found.

Am I right in thinking that the only file in the src directory should be the .cpp file?

Do I actually need to run the SHLIB command in that directory before I create the package?

In the NAMESPACE I use:

useDynLib(myc.cpp,my.c.function)

and in the function call:

my.r.f <- .Call(my.c.function, a, b)

On windows running R CMD check works fine. Could it be my linux R configuration that is to blame? It seems to install 3rd party packages fine.

I'm stumped!

like image 716
Chris Wheadon Avatar asked Sep 09 '10 11:09

Chris Wheadon


People also ask

How do I create an R package in rtools?

Once RTools is set up, you can go about creating the package. In RStudio, select File > New Project > New Directory > R Package. In the dialog box that pops up, give the package a name and enter the directory in which you want the package to reside.

How do I start an R project in RStudio?

Before getting started, you will need to load two packages: You now want to open File in RStudio and select New Project, which will put you at this point: Select a new directory as desired, and specify R Package, as shown in the following screenshot:

How to use dll (class library) in C #?

C# Corner. Creating and Using DLL (Class Library) in C#. Introduction. A Dynamic Link library (DLL) is a library that contains functions and codes that can be used by more than one program at a time. Once we have created a DLL file, we can use it in many applications. The only thing we need to do is to add the reference/import the DLL File.

Where do I find the package description in RStudio?

Note that the description of the package still says “What the Package Does (Title Case)”. We’ll fix that in a bit. Next, look at the “Files” tab. You’ll see a bunch of files and directories that RStudio created for your project. The script files will be in the “R” directory and the help files will be in the “man” directory.


2 Answers

There are several hundred packages on CRAN which do successfully what you attempt to do -- build a package with to-be-compiled sources on any of the supported platforms.

A strategy I quite like is to take one or more existing package and look exactly how they are set up. You can then copy the working recipe depending on how it corresponds to your setup (with or without NAMESPACE, with or without external library like libxml etc pp_)

like image 152
Dirk Eddelbuettel Avatar answered Sep 30 '22 12:09

Dirk Eddelbuettel


I think you should just use useDynLib(myc)... The symbol lookup is done internally.
EDIT: The other thing is the name of this object file -- I think the standard makefile just names it with package name, so it should be rather useDynLib(<package name>). At least it always works for me.

like image 35
mbq Avatar answered Sep 30 '22 13:09

mbq