Skip to content

[Rcpp-devel] Hello world for Rcpp modules

6 messages · Chris DuBois, Dirk Eddelbuettel

#
Hi all,

I've created a package using Rcpp.package.skeleton("pkg",module=TRUE).

If I create a file R/hello_world2.R with the following

show(World)
b <- new(World)
b$greet()
b$set("hello everybody")
b$greet()
rm(b)

then I receive an "execution halted" error when I try to compile the package
using R CMD INSTALL pkg.  Why is this?

I've included my steps below.
Creating directories ...
Creating DESCRIPTION ...
Creating NAMESPACE ...
Creating Read-and-delete-me ...
Saving functions and data ...
Making help files ...
Done.
Further steps are described in './pkg/Read-and-delete-me'.

Adding Rcpp settings
 >> added RcppModules: yada
 >> added Depends: Rcpp
 >> added LinkingTo: Rcpp
 >> added useDynLib directive to NAMESPACE
 >> added Makevars file with Rcpp settings
 >> added Makevars.win file with Rcpp settings
 >> added example header file using Rcpp classes
 >> added example src file using Rcpp classes
 >> added example R file calling the C++ example
 >> added Rd file for rcpp_hello_world
 >> copied the example module
Save workspace image? [y/n/c]: n
chris at seldon:~/Documents/networkstats$ R CMD INSTALL pkg
* installing to library ?/home/chris/R/i486-pc-linux-gnu-library/2.10?
* installing *source* package ?pkg? ...
** libs
g++ -I/usr/share/R/include
-I"/home/chris/R/i486-pc-linux-gnu-library/2.10/Rcpp/include"   -fpic  -O3
-pipe  -g -c rcpp_hello_world.cpp -o rcpp_hello_world.o
g++ -I/usr/share/R/include
-I"/home/chris/R/i486-pc-linux-gnu-library/2.10/Rcpp/include"   -fpic  -O3
-pipe  -g -c rcpp_module.cpp -o rcpp_module.o
g++ -shared -o pkg.so rcpp_hello_world.o rcpp_module.o
-L/home/chris/R/i486-pc-linux-gnu-library/2.10/Rcpp/lib -lRcpp
-Wl,-rpath,/home/chris/R/i486-pc-linux-gnu-library/2.10/Rcpp/lib
-L/usr/lib/R/lib -lR
installing to /home/chris/R/i486-pc-linux-gnu-library/2.10/pkg/libs
** R
** preparing package for lazy loading
** help
Warning: /home/chris/Documents/networkstats/pkg/man/pkg-package.Rd:32: All
text must be in a section
Warning: /home/chris/Documents/networkstats/pkg/man/pkg-package.Rd:33: All
text must be in a section
*** installing help indices
** building package indices ...
** testing if installed package can be loaded

* DONE (pkg)
chris at seldon:~/Documents/networkstats$ R
R version 2.13.1 (2011-07-08)
Loading required package: Rcpp
[[1]]
[1] "foo" "bar"

[[2]]
[1] 0 1
chris at seldon:~/Documents/networkstats$ cp rcpp_hello_world2.R pkg/R/
chris at seldon:~/Documents/networkstats$ R CMD INSTALL pkg
* installing to library ?/home/chris/R/i486-pc-linux-gnu-library/2.10?
* installing *source* package ?pkg? ...
** libs
make: Nothing to be done for `all'.
installing to /home/chris/R/i486-pc-linux-gnu-library/2.10/pkg/libs
** R
** preparing package for lazy loading
Execution halted
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20110811/e897949a/attachment.htm>
#
Hi Chris,
On 11 August 2011 at 22:23, Chris DuBois wrote:
| Hi all,
| 
| I've created a package using Rcpp.package.skeleton("pkg",module=TRUE). ?
| 
| If I create a file R/hello_world2.R with the following
| 
| show(World)
| b <- new(World)
| b$greet()
| b$set("hello everybody")
| b$greet()
| rm(b)
| 
| then I receive an "execution halted" error when I try to compile the package
| using R CMD INSTALL pkg. ?Why is this? ?
| 
| I've included my steps below.
| 
| > library(Rcpp) ?# latest version from CRAN
| > Rcpp.package.skeleton("pkg",module=TRUE)
| Creating directories ...
| Creating DESCRIPTION ...
| Creating NAMESPACE ...
| Creating Read-and-delete-me ...
| Saving functions and data ...
| Making help files ...
| Done.
| Further steps are described in './pkg/Read-and-delete-me'.
| 
| Adding Rcpp settings
| ?>> added RcppModules: yada
| ?>> added Depends: Rcpp
| ?>> added LinkingTo: Rcpp
| ?>> added useDynLib directive to NAMESPACE
| ?>> added Makevars file with Rcpp settings
| ?>> added Makevars.win file with Rcpp settings
| ?>> added example header file using Rcpp classes
| ?>> added example src file using Rcpp classes
| ?>> added example R file calling the C++ example
| ?>> added Rd file for rcpp_hello_world
| ?>> copied the example module?
| > q()
| Save workspace image? [y/n/c]: n
| chris at seldon:~/Documents/networkstats$ R CMD INSTALL pkg
| * installing to library ?/home/chris/R/i486-pc-linux-gnu-library/2.10?

Rcpp has dependended on R (>= 2.12) for quite some time, and Module
definitely requires it.

How did you get Rcpp installed on that version of R, and what version of Rcpp
is it?

Something like the example you show here is part of the unit tests and
clearly works with current R and Rcpp versions.  Can you get hold of a newer
R somewhere to try it?

Dirk

| * installing *source* package ?pkg? ...
| ** libs
| g++ -I/usr/share/R/include ? -I"/home/chris/R/i486-pc-linux-gnu-library/2.10/
| Rcpp/include" ? -fpic ?-O3 -pipe ?-g -c rcpp_hello_world.cpp -o
| rcpp_hello_world.o
| g++ -I/usr/share/R/include ? -I"/home/chris/R/i486-pc-linux-gnu-library/2.10/
| Rcpp/include" ? -fpic ?-O3 -pipe ?-g -c rcpp_module.cpp -o rcpp_module.o
| g++ -shared -o pkg.so rcpp_hello_world.o rcpp_module.o -L/home/chris/R/
| i486-pc-linux-gnu-library/2.10/Rcpp/lib -lRcpp -Wl,-rpath,/home/chris/R/
| i486-pc-linux-gnu-library/2.10/Rcpp/lib -L/usr/lib/R/lib -lR
| installing to /home/chris/R/i486-pc-linux-gnu-library/2.10/pkg/libs
| ** R
| ** preparing package for lazy loading
| ** help
| Warning: /home/chris/Documents/networkstats/pkg/man/pkg-package.Rd:32: All text
| must be in a section
| Warning: /home/chris/Documents/networkstats/pkg/man/pkg-package.Rd:33: All text
| must be in a section
| *** installing help indices
| ** building package indices ...
| ** testing if installed package can be loaded
| 
| * DONE (pkg)
| chris at seldon:~/Documents/networkstats$ R
| R version 2.13.1 (2011-07-08)
| > library(pkg)
| Loading required package: Rcpp
| > source("pkg/R/rcpp_hello_world.R")
| > rcpp_hello_world() ?# works as expected
| [[1]]
| [1] "foo" "bar"
| 
| [[2]]
| [1] 0 1
| > q()
| chris at seldon:~/Documents/networkstats$ cp rcpp_hello_world2.R pkg/R/
| chris at seldon:~/Documents/networkstats$ R CMD INSTALL pkg
| * installing to library ?/home/chris/R/i486-pc-linux-gnu-library/2.10?
| * installing *source* package ?pkg? ...
| ** libs
| make: Nothing to be done for `all'.
| installing to /home/chris/R/i486-pc-linux-gnu-library/2.10/pkg/libs
| ** R
| ** preparing package for lazy loading
| Execution halted
| 
| 
| ----------------------------------------------------------------------
| _______________________________________________
| 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
#
Sorry, but that's just me being lazy with my lib directory.  When I start R
back up in my example, you'll see it's 2.13.1.
On Fri, Aug 12, 2011 at 6:07 AM, Dirk Eddelbuettel <edd at debian.org> wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20110812/be3fa147/attachment.htm>
#
On 12 August 2011 at 07:34, Chris DuBois wrote:
| Sorry, but that's just me being lazy with my lib directory. ?When I start R
| back up in my example, you'll see it's 2.13.1. ?

Well then maybe do some cleanup at your end. As I said, it works in all
regression tests and explicitly too, so I still think this is local to your
end.  See here:

edd at max:~$ cd /tmp/
edd at max:/tmp$ r -lRcpp -e'Rcpp.package.skeleton("chrisExample",module=TRUE)'

Attaching package: 'utils'

The following object(s) are masked from 'package:Rcpp':

    .DollarNames, prompt

Creating directories ...
Creating DESCRIPTION ...
Creating NAMESPACE ...
Creating Read-and-delete-me ...
Saving functions and data ...
Making help files ...
Done.
Further steps are described in './chrisExample/Read-and-delete-me'.

Adding Rcpp settings
 >> added RcppModules: yada
 >> added Depends: Rcpp
 >> added LinkingTo: Rcpp
 >> added useDynLib directive to NAMESPACE
 >> added Makevars file with Rcpp settings
 >> added Makevars.win file with Rcpp settings
 >> added example header file using Rcpp classes
 >> added example src file using Rcpp classes
 >> added example R file calling the C++ example
 >> added Rd file for rcpp_hello_world
 >> copied the example module 
edd at max:/tmp$ R CMD INSTALL chrisExample/
* installing to library ?/usr/local/lib/R/site-library?
* installing *source* package ?chrisExample? ...
** libs
ccache g++-4.5 -I/usr/share/R/include   -I"/usr/local/lib/R/site-library/Rcpp/include"   -fpic  -g0 -O3 -Wall -pipe -pedantic -Wno-variadic-macros  -c rcpp_hello_world.cpp -o rcpp_hello_world.o
ccache g++-4.5 -I/usr/share/R/include   -I"/usr/local/lib/R/site-library/Rcpp/include"   -fpic  -g0 -O3 -Wall -pipe -pedantic -Wno-variadic-macros  -c rcpp_module.cpp -o rcpp_module.o
g++ -shared -o chrisExample.so rcpp_hello_world.o rcpp_module.o -L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib -L/usr/lib64/R/lib -lR
installing to /usr/local/lib/R/site-library/chrisExample/libs
** R
** preparing package for lazy loading
** help
Warning: /tmp/chrisExample/man/chrisExample-package.Rd:32: All text must be in a section
Warning: /tmp/chrisExample/man/chrisExample-package.Rd:33: All text must be in a section
*** installing help indices
** building package indices ...
** testing if installed package can be loaded

* DONE (chrisExample)
edd at max:/tmp$ r -lchrisExample -p -e'rcpp_hello_world()'
[1] "chrisExample" "Rcpp"         "methods"      "base"        
[[1]]
[1] "foo" "bar"

[[2]]
[1] 0 1

edd at max:/tmp$ 


r is from littler, -lfoo loads package foo, -p enables print and -e evaluates
the expression.

The 'ccache g++-4.5' comes from my ~/.R/Makevars.

Dirk
#
Got it now:  you cannot just drop code in an R file, that has nothing to do
with Rcpp methinks.   Wrap it in a function like so:

edd at max:/tmp$ R CMD INSTALL chrisExample/
* installing to library ?/usr/local/lib/R/site-library?
* installing *source* package ?chrisExample? ...
** libs
make: Nothing to be done for `all'.
installing to /usr/local/lib/R/site-library/chrisExample/libs
** R
** preparing package for lazy loading
** help
Warning: /tmp/chrisExample/man/chrisExample-package.Rd:32: All text must be in a section
Warning: /tmp/chrisExample/man/chrisExample-package.Rd:33: All text must be in a section
*** installing help indices
** building package indices ...
** testing if installed package can be loaded

* DONE (chrisExample)
edd at max:/tmp$ r -lchrisExample  -e'chris()'
C++ class 'World' <0x2a55d60>
Constructors:
    World()

Fields: No public fields exposed by this class

Methods: 
     std::string greet()  
           docstring : get the message
     void set(std::string)  
           docstring : set the message
edd at max:/tmp$ cat chrisExample/R/rcpp_hello_world2.R 

chris <- function() {
    show(World)
    b <- new(World)
    b$greet()
    b$set("hello everybody")
    b$greet()
    rm(b)

    invisible(NULL)
}
edd at max:/tmp$ 

Ok?

Dirk
#
Yes, that fixes the problem.

Apologies and thanks,
Chris
On Fri, Aug 12, 2011 at 7:59 AM, Dirk Eddelbuettel <edd at debian.org> wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20110812/b9700bd1/attachment.htm>