[Rd] Define replacement functions

2015-05-04 Thread soeren . vogel
Hello

I tried to define replacement functions for the class mylist. When I test 
them in an active R session, they work -- however, when I put them into a 
package, they don't. Why and how to fix?


make_my_list - function( x, y ) {
return(structure(list(x, y, class=mylist)))
}
mylist - make_my_list(1:4, letters[3:7])
mylist
mylist[['x']] - 4:6
mylist
[[- - function(x, field, value) {
UseMethod('[[-', x)
}
[[-.mylist - function(x, field, value) {
stop( Do not assign. )
}
mylist[['x']] - 1:10
mylist
mylist$y - LETTERS[1:3]
mylist
$- - function(x, field, value) {
UseMethod('$-', x)
}
$-.mylist - function(x, field, value) {
stop( Do not assign. )
}
mylist$y - LETTERS[10:15]


Thanks for help
Sören
__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Define replacement functions

2015-05-04 Thread soeren . vogel
No. I fixed that, the NAMESPACE file now contains:

S3method([[-, mylist)
S3method($-, mylist)

It still does not work. I also created a print method (print.mylist) which did 
work out of the box, regardless of being in the NAMESPACE file or not. Could it 
be somehow in here (also in my NAMESPACE file):

exportPattern(^[[:alpha:]]+)

Or could it be that the type of quoting causes the problems? Like in the 
NAMESPACE I used double quotes for function definition. Do I need single quotes 
or back-ticks?

Or is there a package where I can look how others implemented this kind of 
functionality?

Sören

 On 04.05.2015, at 16:17, Hadley Wickham h.wick...@gmail.com wrote:
 
 Did you export the S3 methods in the NAMESPACE?
 
 Hadley
 
 On Mon, May 4, 2015 at 7:52 AM,  soeren.vo...@posteo.ch wrote:
 Hello
 
 I tried to define replacement functions for the class mylist. When I test 
 them in an active R session, they work -- however, when I put them into a 
 package, they don't. Why and how to fix?
 
 
 make_my_list - function( x, y ) {
return(structure(list(x, y, class=mylist)))
 }
 mylist - make_my_list(1:4, letters[3:7])
 mylist
 mylist[['x']] - 4:6
 mylist
 [[- - function(x, field, value) {
UseMethod('[[-', x)
 }
 [[-.mylist - function(x, field, value) {
stop( Do not assign. )
 }
 mylist[['x']] - 1:10
 mylist
 mylist$y - LETTERS[1:3]
 mylist
 $- - function(x, field, value) {
UseMethod('$-', x)
 }
 $-.mylist - function(x, field, value) {
stop( Do not assign. )
 }
 mylist$y - LETTERS[10:15]
 
 
 Thanks for help
 Sören
 __
 R-devel@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel
 
 
 
 -- 
 http://had.co.nz/

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] Compilation error Mac binary

2011-06-29 Thread soeren . vogel
Hello, the compilation of the GUTS package on CRAN for Mac produces the 
following error messages:

http://www.r-project.org/nosvn/R.check/r-release-macosx-ix86/GUTS-00install.html

I guess it is some namespace collision, but I can not reproduce it on my 
machine, it compiles fine here. What is the cause and which setup do I need to 
reproduce the errors?

R: version
platform   i386-apple-darwin9.8.0
arch   i386
os darwin9.8.0
system i386, darwin9.8.0
status
major  2
minor  13.0
year   2011
month  04
day13
svn rev55427
language   R
version.string R version 2.13.0 (2011-04-13)

I ~ $: gcc --version
i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

I ~ $: less /opt/local/include/boost/version.hpp
//  Boost version.hpp configuration header file  
--//
[...]
#define BOOST_VERSION 104601
#define BOOST_LIB_VERSION 1_46_1
[...]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Compilation error Mac binary

2011-06-29 Thread soeren . vogel
Hello David

On 29.06.2011, at 15:56, David Winsemius wrote:

 On Jun 29, 2011, at 5:33 AM, soeren.vo...@uzh.ch wrote:
 
 Hello, the compilation of the GUTS package on CRAN for Mac produces the 
 following error messages:
 
 http://www.r-project.org/nosvn/R.check/r-release-macosx-ix86/GUTS-00install.html
 
 I guess it is some namespace collision, but I can not reproduce it on my 
 machine, it compiles fine here. What is the cause and which setup do I need 
 to reproduce the errors?
 
 I was going to suggest you take this up with the maintainers... but you _are_ 
 one of the maintainers.

:-)

 Here is the output from an attempt to compile from source, version 0.1.45, 
 with a relatively recent version of R 2.13.0 (but not the most recent and I 
 still use Mac OSX 10.5.8). Perhaps its brevity will be helpful, and the fact 
 that it seems to identify different error than those flagged in the log at 
 CRAN.

Thanks for your answer. The failure on your side is clear to me: there is no 
installation of the boost C++ library in any of the standard include locations. 
I have added /opt/local/include in Makevars (Mac Ports installation of C source 
packages), however, I know I should but still I am reluctant to write a 
configure script that could circumvent non-standard installations of boost on 
UNIX machines.

So thanks for the output, but the CRAN messages must point to something 
different...

Regards
Sören

 trying URL 'http://lib.stat.cmu.edu/R/CRAN/src/contrib/GUTS_0.1.45.tar.gz'
 Content type 'application/x-gzip' length 9842 bytes
 opened URL
 ==
 downloaded 9842 bytes
 
 Loading required package: lattice
 Loading required package: sos
 Loading required package: brew
 [1] 2011-06-29 09:40:18 EDT
 * installing *source* package ‘GUTS’ ...
 ** libs
 *** arch - i386
 g++ -arch i386 -I/Library/Frameworks/R.framework/Resources/include 
 -I/Library/Frameworks/R.framework/Resources/include/i386 -I/opt/local/include 
 -I/usr/local/include 
 -I/Library/Frameworks/R.framework/Versions/2.13/Resources/library/Rcpp/include
-fPIC  -g -O2 -c GUTS.cpp -o GUTS.o
 In file included from GUTS.cpp:12:
 GUTS.h:21:28: error: boost/random.hpp: No such file or directory
 GUTS.h:22:40: error: boost/math/distributions.hpp: No such file or directory
 GUTS.cpp: In function ‘void to_print(double)’:
 GUTS.cpp:277: error: ‘cout’ was not declared in this scope
 GUTS.cpp: In member function ‘void GUTS::showObject()’:
 GUTS.cpp:281: error: ‘cout’ is not a member of ‘std’
 GUTS.cpp:284: error: ‘cout’ was not declared in this scope
 make: *** [GUTS.o] Error 1
 ERROR: compilation failed for package ‘GUTS’
 * removing 
 ‘/Library/Frameworks/R.framework/Versions/2.13/Resources/library/GUTS’
 
  sessionInfo()
 R version 2.13.0 beta (2011-04-04 r55296)
 Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
 
 locale:
 [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8
 
 attached base packages:
 [1] stats graphics  grDevices utils datasets  methods   base
 
 other attached packages:
 [1] mgcv_1.7-6  sos_1.3-0   brew_1.0-6  lattice_0.19-26
 
 loaded via a namespace (and not attached):
 [1] grid_2.13.0Matrix_0.999375-50 nlme_3.1-101   tools_2.13.0
 
 -- 
 David.
 
 
 
 R: version
 platform   i386-apple-darwin9.8.0
 arch   i386
 os darwin9.8.0
 system i386, darwin9.8.0
 status
 major  2
 minor  13.0
 year   2011
 month  04
 day13
 svn rev55427
 language   R
 version.string R version 2.13.0 (2011-04-13)
 
 I ~ $: gcc --version
 i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3)
 Copyright (C) 2007 Free Software Foundation, Inc.
 This is free software; see the source for copying conditions.  There is NO
 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
 I ~ $: less /opt/local/include/boost/version.hpp
 //  Boost version.hpp configuration header file  
 --//
 [...]
 #define BOOST_VERSION 104601
 #define BOOST_LIB_VERSION 1_46_1
 [...]
 
 __
 R-devel@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel
 
 David Winsemius, MD
 West Hartford, CT
 
 

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Compilation error Mac binary

2011-06-29 Thread soeren . vogel
Thanks Simon, Uwe and David for your helpful answers. English is not my primary 
language, so I was hypothesising a bit about the correct interpretation of 
inlining and supply inside. I thought it meant copying the headers inside 
the package. I was already inspecting RQuantlib but I did, so far, not get 
smarter. I think I will setup a VMware OS X 10.5 with an older boost. This 
approach seems more reliable to me than just trying around with a configure 
script which, eventually, I don't understand fully. Yet, interesting that GUTS 
builds on Win and Linux, but not on the CRAN OS X. Anyway, thanks for your help.

Best
Sören


On 29.06.2011, at 20:05, Simon Urbanek wrote:


 On Jun 29, 2011, at 1:12 PM, Uwe Ligges wrote:
 
 Looks like a different boost version than the one you require.
 I'd suggest to talk to the Mac maintainer, Simon Urbanek (CCing).
 
 
 GUTS doesn't specify any system requirements, so obviously it has no business 
 trying to use boost from the system. Also note boost is inlined in the vast 
 majority of cases, so most packages simply supply the headers inside as to 
 guarantee the required version is used (which I would certainly expect GUTS 
 to do since it doesn't depend on boost according to the DESCRIPTION).
 
 Cheers,
 Simon
 
 
 
 On 29.06.2011 17:14, soeren.vo...@uzh.ch wrote:
 Hello David
 
 On 29.06.2011, at 15:56, David Winsemius wrote:
 
 On Jun 29, 2011, at 5:33 AM, soeren.vo...@uzh.ch wrote:
 
 Hello, the compilation of the GUTS package on CRAN for Mac produces the 
 following error messages:
 
 http://www.r-project.org/nosvn/R.check/r-release-macosx-ix86/GUTS-00install.html
 
 I guess it is some namespace collision, but I can not reproduce it on my 
 machine, it compiles fine here. What is the cause and which setup do I 
 need to reproduce the errors?
 
 I was going to suggest you take this up with the maintainers... but you 
 _are_ one of the maintainers.
 
 :-)
 
 Here is the output from an attempt to compile from source, version 0.1.45, 
 with a relatively recent version of R 2.13.0 (but not the most recent and 
 I still use Mac OSX 10.5.8). Perhaps its brevity will be helpful, and the 
 fact that it seems to identify different error than those flagged in the 
 log at CRAN.
 
 Thanks for your answer. The failure on your side is clear to me: there is 
 no installation of the boost C++ library in any of the standard include 
 locations. I have added /opt/local/include in Makevars (Mac Ports 
 installation of C source packages), however, I know I should but still I am 
 reluctant to write a configure script that could circumvent non-standard 
 installations of boost on UNIX machines.
 
 So thanks for the output, but the CRAN messages must point to something 
 different...
 
 Regards
 Sören
 
 trying URL 'http://lib.stat.cmu.edu/R/CRAN/src/contrib/GUTS_0.1.45.tar.gz'
 Content type 'application/x-gzip' length 9842 bytes
 opened URL
 ==
 downloaded 9842 bytes
 
 Loading required package: lattice
 Loading required package: sos
 Loading required package: brew
 [1] 2011-06-29 09:40:18 EDT
 * installing *source* package ‘GUTS’ ...
 ** libs
 *** arch - i386
 g++ -arch i386 -I/Library/Frameworks/R.framework/Resources/include 
 -I/Library/Frameworks/R.framework/Resources/include/i386 
 -I/opt/local/include -I/usr/local/include 
 -I/Library/Frameworks/R.framework/Versions/2.13/Resources/library/Rcpp/include
-fPIC  -g -O2 -c GUTS.cpp -o GUTS.o
 In file included from GUTS.cpp:12:
 GUTS.h:21:28: error: boost/random.hpp: No such file or directory
 GUTS.h:22:40: error: boost/math/distributions.hpp: No such file or 
 directory
 GUTS.cpp: In function ‘void to_print(double)’:
 GUTS.cpp:277: error: ‘cout’ was not declared in this scope
 GUTS.cpp: In member function ‘void GUTS::showObject()’:
 GUTS.cpp:281: error: ‘cout’ is not a member of ‘std’
 GUTS.cpp:284: error: ‘cout’ was not declared in this scope
 make: *** [GUTS.o] Error 1
 ERROR: compilation failed for package ‘GUTS’
 * removing 
 ‘/Library/Frameworks/R.framework/Versions/2.13/Resources/library/GUTS’
 
 sessionInfo()
 R version 2.13.0 beta (2011-04-04 r55296)
 Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
 
 locale:
 [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8
 
 attached base packages:
 [1] stats graphics  grDevices utils datasets  methods   base
 
 other attached packages:
 [1] mgcv_1.7-6  sos_1.3-0   brew_1.0-6  lattice_0.19-26
 
 loaded via a namespace (and not attached):
 [1] grid_2.13.0Matrix_0.999375-50 nlme_3.1-101   tools_2.13.0
 
 --
 David.
 
 
 
 R: version
 platform   i386-apple-darwin9.8.0
 arch   i386
 os darwin9.8.0
 system i386, darwin9.8.0
 status
 major  2
 minor  13.0
 year   2011
 month  04
 day13
 svn rev55427
 language   R
 version.string R version 2.13.0 (2011-04-13)
 
 I ~ $: gcc --version
 i686-apple-darwin10-gcc-4.2.1 

Re: [Rd] Compilation error Mac binary

2011-06-29 Thread soeren . vogel
On 29.06.2011, at 21:19, Dirk Eddelbuettel wrote:

 Soeren just shown resilience and persistence in getting his package
 written, built and now onto CRAN.  The remaining step is to learn more about
 cross-platform build issues.  Because that is the (high) standard of CRAN,
 and if you want to be part of it, you are expected to conform.  No shortcuts.

Hehehe :-D Okay, Dirk, I'll try my best :-)!

Cheers
Sören

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] R CMD check on CRAN

2011-06-19 Thread soeren . vogel
Hello

I have a package with a configure script (--include-something). How does the 
package pass the check on CRAN, that is, how should I signalise that check and 
install may need options?

Thanks
Sören

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] Reference Classes, Generator Object

2011-06-10 Thread soeren . vogel
Hello, I am working with a reference class:

Foo - setRefClass(Bar,
  fields = list( a = numeric),
  methods = list( baz = function( xa ) a - xa )
)
x - Foo$new()
x$baz(2)
x$a
y - new(Bar)
y$baz(4)
y$a

But that might be confusing, since new(Foo) does not work and neither does 
Bar$new() (which is both clear to me but -- maybe -- not to the user). So my 
idea was to:

Bar - setRefClass(Bar,
  fields = list( a = numeric),
  methods = list( baz = function( xa ) a - xa )
)
x - Bar$new()
x$baz(2)
x$a
y - new(Bar)
y$baz(4)
y$a

However, this is not the way it is usually done (e.g., with Rcpp modules), why 
not? Thanks, Sören

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] S4 class, passing argument names to function, modify original

2011-06-04 Thread soeren . vogel
Hello, an S4 class Foo is defined with a setter, $. For several reasons, the 
setter calls a function, .foo.update(). However, bypassing the argument names 
of the setter does not work. Question 1: Why not and how can I fix this? 
Question 2: What is the way to define either the function or the setter to 
modify the original object (not returning the modified copy of it an overwrite 
the original by assignment)? Thanks, Sören

setClass(Foo,
  representation(
N = numeric
  ),
  prototype(
N = 1
  )
)

.foo.update - function(object, ...) {
  args - list(...)
  for (i in slotNames(Foo)[pmatch(names(args), slotNames(Foo), nomatch=0)]) 
{
slot(object, i) - args[[i]]
# indeed more to do here
return(object)
  }
}

setReplaceMethod($, Foo,
  function(x, name, value) {
x - .foo.update(x, name=value)
x
  }
)

x - new(Foo)
x
x$N - 99
x # NULL

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] Interfacing a C++ class

2011-06-04 Thread soeren . vogel
Hello

Apologies for cross-posting, the discussion should (if) go to R-devel, but I 
also want to reach the rcpp-devel people.

My C++ class FOO is a module available through Rcpp, and it works fine and is 
-- so far -- bug free. With trying to further develop my R package, I thought 
it was a good idea to interface my C++ workhorse FOO with an S4 class Foo. 
After some long and not always insightful trip through S4 classes and methods, 
I am not sure if I am on the right way of thinking and designing. Since there 
is no local assistance available, could you help to make things clearer?

Just a brief outline:

FOO is the C++ object, and Foo should be the S4 class. If the user creates an 
object, say bar, from class Foo, the creation process automatically makes a new 
FOO object relating to bar in that a unique name of the FOO instance is stored 
in a slot of bar. All the user then has to do is modify bar by simple 
assignments. The getters and setters ($, [) are set up and work. Each 
modification goes in hand with assigning new values to bar as well as updating 
the FOO object through available setters from FOO.

So far, this way has brought me to about 100 lines, but now I read about 
ReferenceClasses and was wondering, if there is a much easier way of achieving 
my goals. Moreover, I was not sure any longer if my goals make sense or if a 
more advanced programmer would do it totally different (and could share some 
structural thinking on their approach).

The idea behind my way of doing was based upon several considerations. First, a 
classical R object would not confuse users, as I assume users of my package 
to be rather non-skilled (and not willing to change that). Second, I want to 
create a properly programmed package for distribution on CRAN and publication, 
eventually. Third, I want to save objects across sessions. So if a user 
restores a session, a simple command, say, restore(), would do the trick to 
build all the related C++ objects again. However, I admit that I still have not 
figured out how to automatically clean up the workspace correctly before 
leaving a session, wanted or unwanted, that is, clean up memory before leaving 
home. Fourth, pure arithmetic check is done in C++, however, semantic check 
*should* be left to R, and the validity and class routines seem to be perfect 
for this. Fifth, some work should be done in R, such as the passing of data 
frames or samples from distributions.

I hope to get some structured ideas and hints how to start and/or proceed.

Thank you in advance,
Sören

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] S4 class, passing argument names to function, modify original

2011-06-04 Thread soeren . vogel
On 04.06.2011, at 15:41, Martin Morgan wrote:

 On 06/04/2011 03:07 AM, soeren.vo...@uzh.ch wrote:
 
 Hello, an S4 class Foo is defined with a setter, $. For several reasons, 
 the setter calls a function, .foo.update(). However, bypassing the argument 
 names of the setter does not work. Question 1: Why not and how can I fix 
 this? Question 2: What is the way to define either the function or the 
 setter to modify the original object (not returning the modified copy of it 
 an overwrite the original by assignment)? Thanks, Sören
 
 setClass(Foo,
   representation(
 N = numeric
   ),
   prototype(
 N = 1
   )
 )
 
 .foo.update- function(object, ...) {
   args- list(...)
   for (i in slotNames(Foo)[pmatch(names(args), slotNames(Foo), 
 nomatch=0)]) {
 slot(object, i)- args[[i]]
 # indeed more to do here
 return(object)
   }
 }
 
 Since names(args) is 'name', and 'name' is not a slot of 'Foo', the return of 
 pmatch is 0 and .foo.update returns NULL. Put return(object) outside the for 
 loop.
 
 setReplaceMethod($, Foo,
   function(x, name, value) {
 x- .foo.update(x, name=value)
 x
   }
 )
 
 x- new(Foo)
 x
 x$N- 99
 x # NULL
 
 here your intention is that name=value to be substituted with N=99, but you 
 end up with name=99. You could arrange to parse this correctly, but this 
 isn't usually what you _want_ to do and I don't really understand what you're 
 trying to accomplish.

Exactly, what I want to parse is N=99. To clarify, .foo.update should be the 
one workhorse to do a couple of things depending on arguments on more than 
one object (see my post on interfacing a C++ class, just some minutes ago). 
Thus, assigning x@N = 10 (x$N, x[N]) should not only end up in x (object of 
class Foo) with value 10, but also should invoke FOO$setN(10) (a setter for the 
C++ object, not shown here). So far, .foo.update works perfectly and indeed 
alters the object x, but the setter does not parse the argument name. I tried 
with quote, substitute, deparse ..., but got none working ...

 Maybe
 
 .foo.update - function(object, name, value, ...)
 {
slot(object, name) - value
## some other stuff
object
 }
 
 Hope that helps a bit.
 
 Martin

Thanks, Sören

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Create and access several instances of a C++ class from R

2011-05-13 Thread soeren . vogel
On 07.05.2011, at 11:15, Romain Francois wrote:

 Try having do_bar with this signature:
 
 void do_bar(NumericVector data);
 
 And my advice would probably also to have your C field either as a 
 NumericVector. NumericVector is just a thin wrapper around the internal R 
 data structure.
 
 Having data members as direct pointers is dangerous and not a very C++ way of 
 doing things.
 
 If you want to learn about the STL, there are many books that cover it and 
 plenty of online resources. I use this : http://cplusplus.com/reference/stl/. 
 Using the STL is easier than dealing with pointers and stuff.
 
 If your game is to pass down a double* to some api you use, you can use the 
 begin method of NumericVector, this will give you the array pointer (as would 
 REAL do on the underlying SEXP):

[...]

For the files: the discussion continues here:

http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2011-May/002261.html

Regards
Sören

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Create and access several instances of a C++ class from R

2011-05-07 Thread soeren . vogel
Hello

Thanks again for help!

We have attached version info, code, and contents of 00install.out at the end 
of the message. The package can be found here:

http://sovo.md-hh.com/files/Foo.tar.gz

We had followed Dirks explanation in the Rcpp modules vignette and the 
presentation of Rcpp modules. The steps there and the ones Dirk listed here are 
basically the same, and all worked fine for us, so far. Even more, we have 
adapted some of the example code to a class Foo with two methods of interest, 
do_foo() and do_bar(). We have played around with various combinations of which 
method to expose in the module. In the end, exposing do_foo() works, R CMD 
CHECK does not complain. (Except for some namespacing error, but the same 
happens when I compile the example simple, see below.)

Anyway, if we want the method do_bar() to expose, R CMD CHECK Foo stops with an 
error that says: cannot convert ‘SEXPREC*’ to ‘double*’ in initialization. We 
*guess* that it is pointers as arguments in methods exposed that cause errors, 
because there is no wrappers as() and wrap() for pointers in Rcpp. Indee, 
we know that our crystal ball may fool us.

Summarising so far, principally, Rcpp modules works for us and coding with this 
module is very easy, especially with the help files provided in the package. 
However, Rcpp modules does not work if you want to expose methods with pointers 
as exposed methods' arguments (we assume). Yet, we have to use pointers, and 
rewriting the complete class is no option.

Recently, Dirk gave us the advice to have a look at STL containers. We have 
googled and found some information, and looking at it works fine, however, 
working with it is somewhat beyond our C++ skills. If the STL things *are* 
our *only* solution, could some reader here provide us with a specific solution 
to exposing do_bar()? Or what else can we do to (1) use the class as is 
(because it works with dyn.load as well as in other software outside R) and (2) 
incorporate things into Rcpp modules (or other code) anyway?

Thanks for all help!

Sören and Carlo


On 07.05.2011, at 02:39, Dirk Eddelbuettel wrote:

 Sören and Carlo,
 
 On 6 May 2011 at 19:24, soeren.vo...@uzh.ch wrote:
 | Hello
 | 
 | We have a C++ class with several methods that manipulate an object. How is
 | it possible to create several instances of that class *from R* in the C++
 | realm, which can then be accessed via a given name character? 
 
 Yes it is, and even somewhat easily given Rcpp modules as we tried to explain
 over at the Rcpp-devel list when you asked there.  As a real quick
 proof-of-concept, I did the following:
 
 
 1) Make sure you have a recent Rcpp such as 0.9.3 or 0.9.4
 
 
 2) Let Rcpp create a complete 'stub' of a working package with Rcpp modules
   support for you via the Rcpp.package.skeleton.function() with the
   module=TRUE argument:
 
 
   R library(Rcpp)
   R Rcpp.package.skeleton(simple, 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 './simple/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 
   R 
 
 
 3) As you are keen to see that we get actual new objects, I am just doing
   the minimal code for by adding one for a new class member function:
 
void showmyaddress() const { std::cout  Address is   this  
 std::endl; }
 
   which I add to the class 'World' in file simple/src/rcpp_module.rcpp -- on
   line 32 if it matters.   I also add this line to the module definition in
   the same file on line 62:
 
 .const_method( showmyaddress, World::showmyaddress, get *this ptr 
 address)
 
   It doesn't matter that the method is const, you can do without const in
   both eg
 
void showmyaddress() { std::cout  Address is   this  std::endl; }
 
.method( showmyaddress, World::showmyaddress, get *this ptr address)
 
   All that the code does is reveal its pointer to stdout.
 
 
 4) Install it via
 
   $ R CMD INSTALL simple
 
 
 5) Try it in R (and I first
 
   R library(simple)
   Loading required package: Rcpp
   R World
   C++ class 'World' 0x2b84940
   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
void showmyaddress()  const 
  docstring : get *this ptr address
   R
   R w1 - new( World )
   R w1$showmyaddress()
   Address is 0x2748370
   

[Rd] Create and access several instances of a C++ class from R

2011-05-06 Thread soeren . vogel
Hello

We have a C++ class with several methods that manipulate an object. How is it 
possible to create several instances of that class *from R* in the C++ realm, 
which can then be accessed via a given name character? Symbolic example (we 
hope this illustrates our problem):

// C++ side:
class Foo{
...
}
// perhaps:
void my_new_instance_wrapper(the_character) // plain to see that I am no C++ 
programmer ;-)
{
  static Foo the_character; // no return needed since we know the name of the 
instance = the_character
}

# R side:
create_new_instance - function(name){
  dono_what_to_use_here(???, class)
}
# perhaps:
create_new_instance - function(name){
  .C(my_new_instance_wrapper, as.character(name))
}

dyn.load(Foo)
obj1 - create_new_instance(bar, class=Foo)
obj2 - create_new_instance(baz, class=Foo)
str(obj1)
: character which can be manipulated using class methods in the C++ realm

What we do not want: make simple copies of the object in R; use Rcpp modules 
(we tried that without success, pointers in constructors cause trouble); 
re-write our code such that C++ only works off heavy code, the rest is 
R-side. What we want: interfacing (from the R-side) instances of our class 
where the instances exist in the C++ realm.

Either there is a function (or code) in R that solve this task, perhaps by 
returning pointers to instances of C++ classes. (Or there is a possibility to 
create a wrapper in C++ creating a new instance, the wrapper we do not know of.)

Thanks for any notes, tips, experiences.

Sören and Carlo

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel