Skip to content

[Rcpp-devel] Question on 5.6 Interfacing C++ code

8 messages · Sean Robert McGuffee, Dirk Eddelbuettel, Steve Lianoglou

#
Hi, I have a quick couple of questions about some of the documentation on
the web page:
http://cran.r-project.org/doc/manuals/R-exts.html#Linking-GUIs-and-other-fro
nt_002dends-to-R
under the heading:
5.6 Interfacing C++ code

Question 1:
If I?m at a terminal, I can type the instructions they suggest:
R CMD SHLIB X.cc X_main.cc
If I wanted a package to do this, how would I tell the package to do that
same thing?
Would I use the same command and just include it in a file somewhere in the
package?
If so, which file?

Question 2:
dyn.load(paste("X", .Platform$dynlib.ext, sep = ""))

Where does .Platform$dynlib.ext come from?
What does it mean?
What do it?s components .Platform and $dynlib and .ext mean?

Thanks,

Sean




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20110420/859ac7d0/attachment.htm>
#
Hi,

On Wed, Apr 20, 2011 at 9:49 AM, Sean Robert McGuffee
<sean.mcguffee at gmail.com> wrote:
Just to make sure we're all on the same page, you want an R package to
compile some source code into a shared library/dll from inside R?

Not sure if there's a "baked in" way for that to happen, but maybe you
can invoke `R CMD WHATEVER` from inside R using the `system` function:

R> ?system
Hmm ... I'm curious what you're trying to do, exactly?
.Platform is lust a normal list -- it is defined internally (I guess).
You can access "named" elements of a list with `$`.

.Platform$dynlyb (or .Platform[['dynlib']]) tells you the extension
your particular system uses for shared libraries:

R> .Platform
$OS.type
[1] "unix"

$file.sep
[1] "/"

$dynlib.ext
[1] ".so"

$GUI
[1] "X11"

$endian
[1] "little"

$pkgType
[1] "mac.binary.leopard"

$path.sep
[1] ":"

$r_arch
[1] "x86_64"

See ?.Platform for more help.
#
Hi Sean,

Steve had the right hunch:  Is this a question for 

    r-devel,  	    	ie R in general, or for 

    rcpp-devel, 	ie R with Rcpp  ?
On 20 April 2011 at 09:49, Sean Robert McGuffee wrote:
| Hi, I have a quick couple of questions about some of the documentation on the
| web page:
| http://cran.r-project.org/doc/manuals/R-exts.html#
| Linking-GUIs-and-other-front_002dends-to-R
| under the heading:
| 5.6 Interfacing C++ code
| 
| Question 1:
| If I?m at a terminal, I can type the instructions they suggest:
| R CMD SHLIB X.cc X_main.cc
| If I wanted a package to do this, how would I tell the package to do that same
| thing?

Place the source files into the top-level directory src/ of the package. 

| Would I use the same command and just include it in a file somewhere in the
| package?
| If so, which file?

'R CMD build' and 'R CMD INSTALL' and ... all take care of it.
 
| Question 2:
| dyn.load(paste("X", .Platform$dynlib.ext, sep = ""))
| 
| Where does .Platform$dynlib.ext come from?
| What does it mean?

R knows that that Windoze uses .dll and .a, Linux uses .so and .a, OS X uses
.dynlib.  It swaps the correct extension based on the OS it is being run on.

| What do it?s components .Platform and $dynlib and .ext mean?

Maybe the help file for .Platform is of interest?

I think you are being somewhat off-topic here and abusing the high
signal-to-noise ration of this list.  The r-devel is list of very high
quality too and has a wider readership, so maybe you should post non-Rcpp
questions there?

I also find learning by example instructive. There are 2946 packages on
CRAN, of which 980 contain a src/ directory.  That makes for 980 examples you
could look at---some are small, some are big, some use C, some use C++, ...  
You should be able to find a few close to what you currently attempt to do.

And we would be happy to take actual Rcpp questions here.

Cheers, Dirk
#
Hi, thanks!
ok, so where in the package would I put the system call in the package to
have it run when installing the package?
I'm trying to figure out how take commands such as " R CMD SHLIB X.cc
X_main.cc" followed by "dyn.load(paste("X", .Platform$dynlib.ext, sep =
""))," which are commands I can get to work for myself as a human
interactively, and put the commands into a package to be automatically run
when installing the package. I mean, it's great if I can compile a c++ file
and then use it inside R, but I'm only doing that so I can let other people
do that via a package. As much as I read this documentation, I keep missing
the connections between the different sections. This is a section I am
loving because it works very well. Thus, I want to figure out how to take
the baby steps I'm doing and combine them into a package. Specifically, I
want to take these two commands and insert them into a package so that these
commands will compile my code and make a dynamic ".so" file where R can
access its functions when others install my package.
Ah, thanks, that clarifies exactly what .Platform$dynlib.ext is, it's ".so"
on my system. 

This, the dyn.load(paste("X", .Platform$dynlib.ext, sep = "")) is equivalent
to the command dyn.load("X.so) which now makes sense in that context!
#
On 20 April 2011 at 10:20, Sean Robert McGuffee wrote:
| 
| 
| Hi, thanks!
|
| >On 4/20/11 10:03 AM, "Steve Lianoglou" <mailinglist.honeypot at gmail.com> wrote:
| > Hi,
| > 
| > On Wed, Apr 20, 2011 at 9:49 AM, Sean Robert McGuffee
| > <sean.mcguffee at gmail.com> wrote:
| >> Hi, I have a quick couple of questions about some of the documentation on
| >> the web page:
| >> http://cran.r-project.org/doc/manuals/R-exts.html#Linking-GUIs-and-other-fron
| >> t_002dends-to-R
| >> under the heading:
| >> 5.6 Interfacing C++ code
| >> 
| >> Question 1:
| >> If I?m at a terminal, I can type the instructions they suggest:
| >> R CMD SHLIB X.cc X_main.cc
| >> If I wanted a package to do this, how would I tell the package to do that
| >> same thing?
| > 
| > Just to make sure we're all on the same page, you want an R package to
| > compile some source code into a shared library/dll from inside R?
| > 
| > Not sure if there's a "baked in" way for that to happen, but maybe you
| > can invoke `R CMD WHATEVER` from inside R using the `system` function:
| > 
| > R> ?system
| > 
| 
| ok, so where in the package would I put the system call in the package to
| have it run when installing the package?

You don't. As I said, 'R CMD INSTALL' et all do that.

Download an existing package with source, install it.  Study its sources,
study the 'Writing R Extensions' manual.  Ask on r-devel.

Basic R questions are off-topic here.
 
| >> Would I use the same command and just include it in a file somewhere in the
| >> package?
| >> If so, which file?
| > 
| > Hmm ... I'm curious what you're trying to do, exactly?
| 
| I'm trying to figure out how take commands such as " R CMD SHLIB X.cc
| X_main.cc" followed by "dyn.load(paste("X", .Platform$dynlib.ext, sep =
| ""))," which are commands I can get to work for myself as a human
| interactively, and put the commands into a package to be automatically run
| when installing the package. I mean, it's great if I can compile a c++ file
| and then use it inside R, but I'm only doing that so I can let other people
| do that via a package. As much as I read this documentation, I keep missing

Again, I like working from an existing, working package. As I said, there are
almost 1000 to pick from.

Please direct follow-ups that have no bearing on Rcpp to r-devel.

Dirk

| the connections between the different sections. This is a section I am
| loving because it works very well. Thus, I want to figure out how to take
| the baby steps I'm doing and combine them into a package. Specifically, I
| want to take these two commands and insert them into a package so that these
| commands will compile my code and make a dynamic ".so" file where R can
| access its functions when others install my package.
| 
| > 
| >> Question 2:
| >> dyn.load(paste("X", .Platform$dynlib.ext, sep = ""))
| >> 
| >> Where does .Platform$dynlib.ext come from?
| >> What does it mean?
| >> What do it?s components .Platform and $dynlib and .ext mean?
| > 
| > .Platform is lust a normal list -- it is defined internally (I guess).
| > You can access "named" elements of a list with `$`.
| > 
| > .Platform$dynlyb (or .Platform[['dynlib']]) tells you the extension
| > your particular system uses for shared libraries:
| > 
| > R> .Platform
| > $OS.type
| > [1] "unix"
| > 
| > $file.sep
| > [1] "/"
| > 
| > $dynlib.ext
| > [1] ".so"
| > 
| > $GUI
| > [1] "X11"
| > 
| > $endian
| > [1] "little"
| > 
| > $pkgType
| > [1] "mac.binary.leopard"
| > 
| > $path.sep
| > [1] ":"
| > 
| > $r_arch
| > [1] "x86_64"
| > 
| > See ?.Platform for more help.
| 
| Ah, thanks, that clarifies exactly what .Platform$dynlib.ext is, it's ".so"
| on my system. 
| 
| This, the dyn.load(paste("X", .Platform$dynlib.ext, sep = "")) is equivalent
| to the command dyn.load("X.so) which now makes sense in that context! 
| 
| 
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
#
I don't know!
I'm trying to learn how to set something like a package up to allow others
to access my C++ code inside of R. I'm dense about how to do this and about
what emails list names are.
Sean
On 4/20/11 10:18 AM, "Dirk Eddelbuettel" <edd at debian.org> wrote:

            
#
Does anyone know how to get this email sent to whereever it's supposed to be
sent?
On 4/20/11 10:33 AM, "Dirk Eddelbuettel" <edd at debian.org> wrote:

            
#
Hi,

On Wed, Apr 20, 2011 at 10:49 AM, Sean Robert McGuffee
<sean.mcguffee at gmail.com> wrote:
With email in general, one typically does this by changing the value
in the "To:" field before you hit "Send" ... ;-)

Here's a list of (some) R mailing lists:

http://www.r-project.org/mail.html

Since your questions are more about code/package development in R in
general, and not really specific to Rcpp, Dirk is suggesting that you
post to r-devel. So you'll have to subscribe there first, then fire
away.

-steve