[Rd] Package with multiple shared libraries

2010-11-15 Thread Andreas Borg

Dear R-devel members,

I would like to compile a package with two seperate shared libraries. 
For example, in a package 'foo', a file 'bar.so' built from a distinct 
set of source files should be installed in addition to the default 
'foo.so' (or .dll on windows). Does anyone know about a way to achieve 
this without having a custom Makefile, e.g. via 'Makevars'?


Also, I'd be thankful for information on existing packages which make 
use of this or a similar customization.


Just in case anyone is interested in details: I have some C-code which 
is not intended to be loaded into R but into a SQLite database to which 
I connect via RSQLite. I would like to have these object in a seperate 
library.


Thanks and kind regards,

Andreas

--
Andreas Borg
Medizinische Informatik

UNIVERSITÄTSMEDIZIN
der Johannes Gutenberg-Universität
Institut für Medizinische Biometrie, Epidemiologie und Informatik
Obere Zahlbacher Straße 69, 55131 Mainz
www.imbei.uni-mainz.de

Telefon +49 (0) 6131 175062
E-Mail: b...@imbei.uni-mainz.de

Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. 
Wenn Sie nicht der
richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren 
Sie bitte sofort den
Absender und löschen Sie diese Mail. Das unerlaubte Kopieren sowie die 
unbefugte Weitergabe
dieser Mail und der darin enthaltenen Informationen ist nicht gestattet.

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


Re: [Rd] Package with multiple shared libraries

2010-11-15 Thread Prof Brian Ripley
R CMD INSTALL is R code, and you can read it for yourself rather than 
asking other people to do so for you.  If you look in

tools:::.install.packages you will see

shlib_install <- function(instdir, arch)
{
files <- Sys.glob(paste0("*", SHLIB_EXT))
if (length(files)) {
...

so it does in general install all the *.so or *.dll's generated.  I am 
not aware of a package that does so for more than one, though.


On Mon, 15 Nov 2010, Andreas Borg wrote:


Dear R-devel members,

I would like to compile a package with two seperate shared libraries. For 
example, in a package 'foo', a file 'bar.so' built from a distinct set of 
source files should be installed in addition to the default 'foo.so' (or .dll 
on windows). Does anyone know about a way to achieve this without having a 
custom Makefile, e.g. via 'Makevars'?


Also, I'd be thankful for information on existing packages which make use of 
this or a similar customization.


Just in case anyone is interested in details: I have some C-code which is not 
intended to be loaded into R but into a SQLite database to which I connect 
via RSQLite. I would like to have these object in a seperate library.


In which case it seems that the library should not be in the package's 
libs directory, which is intended for DSO/DLLs to be loaded into R.


If you want to do this in a public package you need to remember that 
multiple architectures are used on Mac OS and Windows.



Thanks and kind regards,

Andreas

--
Andreas Borg
Medizinische Informatik

UNIVERSITÄTSMEDIZIN
der Johannes Gutenberg-Universität
Institut für Medizinische Biometrie, Epidemiologie und Informatik
Obere Zahlbacher Straße 69, 55131 Mainz
www.imbei.uni-mainz.de

Telefon +49 (0) 6131 175062
E-Mail: b...@imbei.uni-mainz.de


--
Brian D. Ripley,  rip...@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] SEXPs and slots

2010-11-15 Thread Patrick Leyshock
Very helpful, thank you.

A couple other questions, please:

1.  I've got a function written in C, named "my_c_function".  In my R code I
call this function, passing to it an INTSXP and a STRSXP, respectively:

   result <- .Call("my_c_function", int_vector, str_vector)

The prototype of "my_c_function" is:

   SEXP my_c_function(SEXP int_vec, SEXP str_vec);

Within my_c_function I am able to extract the values within the integer
vector, e.g. I can grab the first value with these lines of code:

   int extracted_value;
   extracted_value = *INTEGER(int_vec);

What I cannot figure out how to do is extract the value from the STRSXP.
I'm assuming that I can create a pointer to a character array, then malloc
enough memory to hold the value.  Is there an analogous operation on
"INTEGER" for STRSXPs?

2.  Any good references/resources for developing R?  Nearly all the
documents I've found are for programming R as a user, not as a developer.  I
have copies of the documentation, which are very helpful, but it'd be
helpful to have additional resources to fill in their gaps.

Thank you,

Patrick


On Fri, Nov 12, 2010 at 4:36 PM, Martin Morgan  wrote:

> On 11/12/2010 02:31 PM, Patrick Leyshock wrote:
> > Hello,
> >
> > I've created this class:
> >
> > setClass("example",
> >representation  (
> >   size = "numeric",
> >   id= "character"
> >)
> > )
> >
> > Suppose I create a new instance of this class:
> >
> >> x <- new("example", 4, "id_value")
> >
> > This creates an S4 object with two slots.  Am I correct in thinking that
> > slots are "filled" by SEXPs?
>
> Hi Patrick --
>
> If I
>
> > eg = new("example", size=4, id="id_value")
>
> (note the named arguments) and take a peak at the str'ucture of eg, I see
>
> > str(eg)
> Formal class 'example' [package ".GlobalEnv"] with 2 slots
>  ..@ size: num 4
>  ..@ id  : chr "id_value"
>
> so the @size slot is a numeric vector of length 1 containing the value
> 4. One doesn't really have to know the detailed representation, but one
> can find out from
>
> > .Internal(inspect(eg))
> @df70e48 25 S4SXP g0c0 [OBJ,NAM(2),gp=0x10,ATT]
> ATTRIB:
>  @df70ef0 02 LISTSXP g0c0 []
>TAG: @769258 01 SYMSXP g1c0 [MARK] "size"
>@c0f6db8 14 REALSXP g0c1 [NAM(2)] (len=1, tl=0) 4
>TAG: @15b0228 01 SYMSXP g1c0 [MARK,NAM(2)] "id"
>@c0f6178 16 STRSXP g0c1 [NAM(2)] (len=1, tl=0)
>  @12341c80 09 CHARSXP g0c2 [gp=0x20] "id_value"
>TAG: @607ce8 01 SYMSXP g1c0 [MARK,NAM(2),gp=0x4000] "class"
>@c0f6d58 16 STRSXP g0c1 [NAM(2),ATT] (len=1, tl=0)
>  @96ed08 09 CHARSXP g1c1 [MARK,gp=0x21] "example"
>ATTRIB:
>  @df70fd0 02 LISTSXP g0c0 []
>TAG: @624f70 01 SYMSXP g1c0 [MARK,NAM(2)] "package"
>@c0f6d88 16 STRSXP g0c1 [NAM(2)] (len=1, tl=0)
>  @67f5e0 09 CHARSXP g1c2 [MARK,gp=0x21,ATT] ".GlobalEnv"
>
> that the 'eg' object is an S4SXP with an attribute that is a LISTSXP.
> The LISTSXP has elements that are tagged with SYMSXP representing the
> slot name, and values that are REALSXP (for 'size') or STRSXP (for
> 'id'). The LISTSXP attribute itself has an attribute, which contains
> information about the package where the class is defined. With these
> hints one can see through the S4 interface to the underlying implementation
>
> > attributes(eg)
> $size
> [1] 4
>
> $id
> [1] "id_value"
>
> $class
> [1] "example"
> attr(,"package")
> [1] ".GlobalEnv"
>
> But probably you have a specific goal in mind, and this is too much
> information...
>
> Martin
>
> >
> > Thanks, Patrick
> >
> >   [[alternative HTML version deleted]]
> >
> > __
> > R-devel@r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
>
>
> --
> Computational Biology
> Fred Hutchinson Cancer Research Center
> 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109
>
> Location: M1-B861
> Telephone: 206 667-2793
>

[[alternative HTML version deleted]]

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


Re: [Rd] SEXPs and slots

2010-11-15 Thread Hadley Wickham
> 2.  Any good references/resources for developing R?  Nearly all the
> documents I've found are for programming R as a user, not as a developer.  I
> have copies of the documentation, which are very helpful, but it'd be
> helpful to have additional resources to fill in their gaps.

The best advice I've received is to use Rcpp:
http://stackoverflow.com/questions/4106174/where-can-i-learn-to-how-to-write-c-code-to-speed-up-slow-r-functions.
 It provides a consistent api for C <-> R conversions and so is much
easier to learn.  Dirk and Romain are also churning out the
documentation, so there are lots of examples to learn from.

Hadley

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

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


Re: [Rd] SEXPs and slots

2010-11-15 Thread Dirk Eddelbuettel

On 15 November 2010 at 10:46, Hadley Wickham wrote:
| > 2.  Any good references/resources for developing R?  Nearly all the
| > documents I've found are for programming R as a user, not as a developer.  I
| > have copies of the documentation, which are very helpful, but it'd be
| > helpful to have additional resources to fill in their gaps.
| 
| The best advice I've received is to use Rcpp:
| 
http://stackoverflow.com/questions/4106174/where-can-i-learn-to-how-to-write-c-code-to-speed-up-slow-r-functions.
|  It provides a consistent api for C <-> R conversions and so is much
| easier to learn.  Dirk and Romain are also churning out the
| documentation, so there are lots of examples to learn from.

Thanks for the plug and endorsement -- we appreciate it. One correction
though:  We strive to provide seamless C++ and R integration, not C and R.

Dirk

-- 
Dirk Eddelbuettel | e...@debian.org | http://dirk.eddelbuettel.com

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


Re: [Rd] SEXPs and slots

2010-11-15 Thread Martin Morgan
On 11/15/2010 07:45 AM, Patrick Leyshock wrote:
> Very helpful, thank you.
> 
> A couple other questions, please:
> 
> 1.  I've got a function written in C, named "my_c_function".  In my R
> code I call this function, passing to it an INTSXP and a STRSXP,
> respectively:
> 
>result <- .Call("my_c_function", int_vector, str_vector)
> 
> The prototype of "my_c_function" is: 
> 
>SEXP my_c_function(SEXP int_vec, SEXP str_vec);
> 
> Within my_c_function I am able to extract the values within the integer
> vector, e.g. I can grab the first value with these lines of code:
> 
>int extracted_value;
>extracted_value = *INTEGER(int_vec);
> 
> What I cannot figure out how to do is extract the value from the
> STRSXP.  I'm assuming that I can create a pointer to a character array,
> then malloc enough memory to hold the value.  Is there an analogous
> operation on "INTEGER" for STRSXPs? 

STRING_ELT(str_vec, 0)

gets the 0th component of str_vec, which is a CHARSXP, i.e., an SEXP for
a character string. The char* can be retrieved with CHAR, so the usual
paradigm is

  const char *x = CHAR(STRING_ELT(str_vec, 0));

note the const-ness of the char* -- it's not mutable, because R is
managing char * memory.

The converse action, of assigning to an element, is

  SET_STRING_ELT(str_vec, 0, mkChar("foo"));

mkChar() is creating a copy (if necessary) of "foo", managing it, and
returning a CHARSXP. Working through protection (which will likely be
your next obstacle ;) in this last example is a good exercise.

There is a parallel operation VECTOR_ELT / SET_VECTOR_ELT for lists.

> 2.  Any good references/resources for developing R?  Nearly all the
> documents I've found are for programming R as a user, not as a
> developer.  I have copies of the documentation, which are very helpful,
> but it'd be helpful to have additional resources to fill in their gaps.

Chambers, 2008, Software for Data Analysis: Programming with R chapters
11 & 12,

Gentleman, 2008, R Programming for Bioinformatics chapter 6

might be helpful, but by the time they arrive you might find that you're
most of the way through the material covered...

I guess my opinion is that Rcpp would not be useful for understanding
R's C layer, whatever its merits for 'getting the job done'.

Martin

> 
> Thank you,
> 
> Patrick
> 
> 
> On Fri, Nov 12, 2010 at 4:36 PM, Martin Morgan  > wrote:
> 
> On 11/12/2010 02:31 PM, Patrick Leyshock wrote:
> > Hello,
> >
> > I've created this class:
> >
> > setClass("example",
> >representation  (
> >   size = "numeric",
> >   id= "character"
> >)
> > )
> >
> > Suppose I create a new instance of this class:
> >
> >> x <- new("example", 4, "id_value")
> >
> > This creates an S4 object with two slots.  Am I correct in
> thinking that
> > slots are "filled" by SEXPs?
> 
> Hi Patrick --
> 
> If I
> 
> > eg = new("example", size=4, id="id_value")
> 
> (note the named arguments) and take a peak at the str'ucture of eg,
> I see
> 
> > str(eg)
> Formal class 'example' [package ".GlobalEnv"] with 2 slots
>  ..@ size: num 4
>  ..@ id  : chr "id_value"
> 
> so the @size slot is a numeric vector of length 1 containing the value
> 4. One doesn't really have to know the detailed representation, but one
> can find out from
> 
> > .Internal(inspect(eg))
> @df70e48 25 S4SXP g0c0 [OBJ,NAM(2),gp=0x10,ATT]
> ATTRIB:
>  @df70ef0 02 LISTSXP g0c0 []
>TAG: @769258 01 SYMSXP g1c0 [MARK] "size"
>@c0f6db8 14 REALSXP g0c1 [NAM(2)] (len=1, tl=0) 4
>TAG: @15b0228 01 SYMSXP g1c0 [MARK,NAM(2)] "id"
>@c0f6178 16 STRSXP g0c1 [NAM(2)] (len=1, tl=0)
>  @12341c80 09 CHARSXP g0c2 [gp=0x20] "id_value"
>TAG: @607ce8 01 SYMSXP g1c0 [MARK,NAM(2),gp=0x4000] "class"
>@c0f6d58 16 STRSXP g0c1 [NAM(2),ATT] (len=1, tl=0)
>  @96ed08 09 CHARSXP g1c1 [MARK,gp=0x21] "example"
>ATTRIB:
>  @df70fd0 02 LISTSXP g0c0 []
>TAG: @624f70 01 SYMSXP g1c0 [MARK,NAM(2)] "package"
>@c0f6d88 16 STRSXP g0c1 [NAM(2)] (len=1, tl=0)
>  @67f5e0 09 CHARSXP g1c2 [MARK,gp=0x21,ATT] ".GlobalEnv"
> 
> that the 'eg' object is an S4SXP with an attribute that is a LISTSXP.
> The LISTSXP has elements that are tagged with SYMSXP representing the
> slot name, and values that are REALSXP (for 'size') or STRSXP (for
> 'id'). The LISTSXP attribute itself has an attribute, which contains
> information about the package where the class is defined. With these
> hints one can see through the S4 interface to the underlying
> implementation
> 
> > attributes(eg)
> $size
> [1] 4
> 
> $id
> [1] "id_value"
> 
> $class
> [1] "example"
> attr(,"package")
> [1] ".GlobalEnv"
> 
> But probably you have a specific goal in mind, and this is too mu

Re: [Rd] SEXPs and slots

2010-11-15 Thread Patrick Leyshock
Thanks Martin and Hadley, this is very helpful.

I should also add that I'm constantly reading the source code, but sometimes
a response like this one can eliminate many, many hours of puzzlement.  Much
appreciated.

Patrick

On Mon, Nov 15, 2010 at 10:01 AM, Martin Morgan  wrote:

> On 11/15/2010 07:45 AM, Patrick Leyshock wrote:
> > Very helpful, thank you.
> >
> > A couple other questions, please:
> >
> > 1.  I've got a function written in C, named "my_c_function".  In my R
> > code I call this function, passing to it an INTSXP and a STRSXP,
> > respectively:
> >
> >result <- .Call("my_c_function", int_vector, str_vector)
> >
> > The prototype of "my_c_function" is:
> >
> >SEXP my_c_function(SEXP int_vec, SEXP str_vec);
> >
> > Within my_c_function I am able to extract the values within the integer
> > vector, e.g. I can grab the first value with these lines of code:
> >
> >int extracted_value;
> >extracted_value = *INTEGER(int_vec);
> >
> > What I cannot figure out how to do is extract the value from the
> > STRSXP.  I'm assuming that I can create a pointer to a character array,
> > then malloc enough memory to hold the value.  Is there an analogous
> > operation on "INTEGER" for STRSXPs?
>
> STRING_ELT(str_vec, 0)
>
> gets the 0th component of str_vec, which is a CHARSXP, i.e., an SEXP for
> a character string. The char* can be retrieved with CHAR, so the usual
> paradigm is
>
>  const char *x = CHAR(STRING_ELT(str_vec, 0));
>
> note the const-ness of the char* -- it's not mutable, because R is
> managing char * memory.
>
> The converse action, of assigning to an element, is
>
>  SET_STRING_ELT(str_vec, 0, mkChar("foo"));
>
> mkChar() is creating a copy (if necessary) of "foo", managing it, and
> returning a CHARSXP. Working through protection (which will likely be
> your next obstacle ;) in this last example is a good exercise.
>
> There is a parallel operation VECTOR_ELT / SET_VECTOR_ELT for lists.
>
> > 2.  Any good references/resources for developing R?  Nearly all the
> > documents I've found are for programming R as a user, not as a
> > developer.  I have copies of the documentation, which are very helpful,
> > but it'd be helpful to have additional resources to fill in their gaps.
>
> Chambers, 2008, Software for Data Analysis: Programming with R chapters
> 11 & 12,
>
> Gentleman, 2008, R Programming for Bioinformatics chapter 6
>
> might be helpful, but by the time they arrive you might find that you're
> most of the way through the material covered...
>
> I guess my opinion is that Rcpp would not be useful for understanding
> R's C layer, whatever its merits for 'getting the job done'.
>
> Martin
>
> >
> > Thank you,
> >
> > Patrick
> >
> >
> > On Fri, Nov 12, 2010 at 4:36 PM, Martin Morgan  > > wrote:
> >
> > On 11/12/2010 02:31 PM, Patrick Leyshock wrote:
> > > Hello,
> > >
> > > I've created this class:
> > >
> > > setClass("example",
> > >representation  (
> > >   size = "numeric",
> > >   id= "character"
> > >)
> > > )
> > >
> > > Suppose I create a new instance of this class:
> > >
> > >> x <- new("example", 4, "id_value")
> > >
> > > This creates an S4 object with two slots.  Am I correct in
> > thinking that
> > > slots are "filled" by SEXPs?
> >
> > Hi Patrick --
> >
> > If I
> >
> > > eg = new("example", size=4, id="id_value")
> >
> > (note the named arguments) and take a peak at the str'ucture of eg,
> > I see
> >
> > > str(eg)
> > Formal class 'example' [package ".GlobalEnv"] with 2 slots
> >  ..@ size: num 4
> >  ..@ id  : chr "id_value"
> >
> > so the @size slot is a numeric vector of length 1 containing the
> value
> > 4. One doesn't really have to know the detailed representation, but
> one
> > can find out from
> >
> > > .Internal(inspect(eg))
> > @df70e48 25 S4SXP g0c0 [OBJ,NAM(2),gp=0x10,ATT]
> > ATTRIB:
> >  @df70ef0 02 LISTSXP g0c0 []
> >TAG: @769258 01 SYMSXP g1c0 [MARK] "size"
> >@c0f6db8 14 REALSXP g0c1 [NAM(2)] (len=1, tl=0) 4
> >TAG: @15b0228 01 SYMSXP g1c0 [MARK,NAM(2)] "id"
> >@c0f6178 16 STRSXP g0c1 [NAM(2)] (len=1, tl=0)
> >  @12341c80 09 CHARSXP g0c2 [gp=0x20] "id_value"
> >TAG: @607ce8 01 SYMSXP g1c0 [MARK,NAM(2),gp=0x4000] "class"
> >@c0f6d58 16 STRSXP g0c1 [NAM(2),ATT] (len=1, tl=0)
> >  @96ed08 09 CHARSXP g1c1 [MARK,gp=0x21] "example"
> >ATTRIB:
> >  @df70fd0 02 LISTSXP g0c0 []
> >TAG: @624f70 01 SYMSXP g1c0 [MARK,NAM(2)] "package"
> >@c0f6d88 16 STRSXP g0c1 [NAM(2)] (len=1, tl=0)
> >  @67f5e0 09 CHARSXP g1c2 [MARK,gp=0x21,ATT] ".GlobalEnv"
> >
> > that the 'eg' object is an S4SXP with an attribute that is a LISTSXP.
> > The LISTSXP has elements that are tagged with SYMSXP representing the
> > slot name, and values that are R

Re: [Rd] SEXP and slots

2010-11-15 Thread Romain Francois

Hello,

Since people have whisperred about Rcpp, I'd like to play too.


On 11/15/2010 07:45 AM, Patrick Leyshock wrote:

Very helpful, thank you.

A couple other questions, please:

1.  I've got a function written in C, named "my_c_function".  In my R
code I call this function, passing to it an INTSXP and a STRSXP,
respectively:

   result <- .Call("my_c_function", int_vector, str_vector)

The prototype of "my_c_function" is:

   SEXP my_c_function(SEXP int_vec, SEXP str_vec);

Within my_c_function I am able to extract the values within the integer
vector, e.g. I can grab the first value with these lines of code:

   int extracted_value;
   extracted_value = *INTEGER(int_vec);

What I cannot figure out how to do is extract the value from the
STRSXP.  I'm assuming that I can create a pointer to a character array,
then malloc enough memory to hold the value.  Is there an analogous
operation on "INTEGER" for STRSXPs?


STRING_ELT(str_vec, 0)

gets the 0th component of str_vec, which is a CHARSXP, i.e., an SEXP for
a character string. The char* can be retrieved with CHAR, so the usual
paradigm is

  const char *x = CHAR(STRING_ELT(str_vec, 0));

note the const-ness of the char* -- it's not mutable, because R is
managing char * memory.

The converse action, of assigning to an element, is

  SET_STRING_ELT(str_vec, 0, mkChar("foo"));

mkChar() is creating a copy (if necessary) of "foo", managing it, and
returning a CHARSXP. Working through protection (which will likely be
your next obstacle ;) in this last example is a good exercise.


In Rcpp, you would wrap up the STRSXP into a CharacterVector and then 
pull things in and out using indexing:


Rcpp::CharacterVector aladdin(str_vec) ;
std::string first = aladdin[0] ;
aladdin[0] = "foobar" ;


There is a parallel operation VECTOR_ELT / SET_VECTOR_ELT for lists.


Same thing for lists:

Rcpp::List yasmine( some_vec_sxp ) ;
double x = yasmine[0] ;
yasmine[0] = "bla" ;


2.  Any good references/resources for developing R?  Nearly all the
documents I've found are for programming R as a user, not as a
developer.  I have copies of the documentation, which are very helpful,
but it'd be helpful to have additional resources to fill in their gaps.


Chambers, 2008, Software for Data Analysis: Programming with R chapters
11 & 12,

Gentleman, 2008, R Programming for Bioinformatics chapter 6

might be helpful, but by the time they arrive you might find that you're
most of the way through the material covered...

I guess my opinion is that Rcpp would not be useful for understanding
R's C layer, whatever its merits for 'getting the job done'.


Right, but we try to put a positive spin on this.

Rcpp hides the C API on purpose, so that users can concentrate on 
solving their problem rather than deal/fight with the C API.




Back to the original question, Rcpp also has (although it is less used) 
an Rcpp::S4 class that can be used to deal with slots. Here is a 
complete example using Rcpp and inline:


require( Rcpp)
require( inline )

setClass("example",
   representation  (
  size = "numeric",
  id= "character"
   )
)

fx <- cxxfunction( signature(x = "example"),
'
S4 obj(x) ;
obj.slot( "size" ) = 10 ;
obj.slot( "id"   ) = "foo" ;
return obj ;
', plugin = "Rcpp" )

str( fx( new("example", size=4, id="id_value") ) )

But as Martin says, it all depends on what your goal is: getting the job 
done or learn about the internal C API.


Romain



Martin



Thank you,

Patrick


On Fri, Nov 12, 2010 at 4:36 PM, Martin Morgan  fhcrc.org
> wrote:

On 11/12/2010 02:31 PM, Patrick Leyshock wrote:
> Hello,
>
> I've created this class:
>
> setClass("example",
>representation  (
>   size = "numeric",
>   id= "character"
>)
> )
>
> Suppose I create a new instance of this class:
>
>> x <- new("example", 4, "id_value")
>
> This creates an S4 object with two slots.  Am I correct in
thinking that
> slots are "filled" by SEXPs?

Hi Patrick --

If I

> eg = new("example", size=4, id="id_value")

(note the named arguments) and take a peak at the str'ucture of eg,
I see

> str(eg)
Formal class 'example' [package ".GlobalEnv"] with 2 slots
 ..@ size: num 4
 ..@ id  : chr "id_value"

so the @size slot is a numeric vector of length 1 containing the value
4. One doesn't really have to know the detailed representation, but one
can find out from

> .Internal(inspect(eg))
@df70e48 25 S4SXP g0c0 [OBJ,NAM(2),gp=0x10,ATT]
ATTRIB:
 @df70ef0 02 LISTSXP g0c0 []
   TAG: @769258 01 SYMSXP g1c0 [MARK] "size"
   @c0f6db8 14 REALSXP g0c1 [NAM(2)] (len=1, tl=0) 4
   TAG: @15b0228 01 SYMSXP g1c0 [MARK,NAM(2)] "id"
   @c0f6178 16 STRSXP g0c1 [NAM(2)] (len=1, tl=0)
 @12341c80 09 CHARSXP g0c2 [gp=0x20] "id_value"
   TAG: @607ce8 01 SYMSXP g1c0 [MARK,NAM(2),gp=0x400

Re: [Rd] SEXPs and slots

2010-11-15 Thread Patrick Leyshock
Thanks Martin and Hadley, this is very helpful.

I should also add that I'm constantly reading the source code, but sometimes
a response like this one can eliminate many, many hours of puzzlement.  Much
appreciated.

Patrick

On Mon, Nov 15, 2010 at 12:07 PM, Patrick Leyshock wrote:

> Thanks Martin and Hadley, this is very helpful.
>
> I should also add that I'm constantly reading the source code, but
> sometimes a response like this one can eliminate many, many hours of
> puzzlement.  Much appreciated.
>
> Patrick
>
>
> On Mon, Nov 15, 2010 at 10:01 AM, Martin Morgan wrote:
>
>> On 11/15/2010 07:45 AM, Patrick Leyshock wrote:
>> > Very helpful, thank you.
>> >
>> > A couple other questions, please:
>> >
>> > 1.  I've got a function written in C, named "my_c_function".  In my R
>> > code I call this function, passing to it an INTSXP and a STRSXP,
>> > respectively:
>> >
>> >result <- .Call("my_c_function", int_vector, str_vector)
>> >
>> > The prototype of "my_c_function" is:
>> >
>> >SEXP my_c_function(SEXP int_vec, SEXP str_vec);
>> >
>> > Within my_c_function I am able to extract the values within the integer
>> > vector, e.g. I can grab the first value with these lines of code:
>> >
>> >int extracted_value;
>> >extracted_value = *INTEGER(int_vec);
>> >
>> > What I cannot figure out how to do is extract the value from the
>> > STRSXP.  I'm assuming that I can create a pointer to a character array,
>> > then malloc enough memory to hold the value.  Is there an analogous
>> > operation on "INTEGER" for STRSXPs?
>>
>> STRING_ELT(str_vec, 0)
>>
>> gets the 0th component of str_vec, which is a CHARSXP, i.e., an SEXP for
>> a character string. The char* can be retrieved with CHAR, so the usual
>> paradigm is
>>
>>  const char *x = CHAR(STRING_ELT(str_vec, 0));
>>
>> note the const-ness of the char* -- it's not mutable, because R is
>> managing char * memory.
>>
>> The converse action, of assigning to an element, is
>>
>>  SET_STRING_ELT(str_vec, 0, mkChar("foo"));
>>
>> mkChar() is creating a copy (if necessary) of "foo", managing it, and
>> returning a CHARSXP. Working through protection (which will likely be
>> your next obstacle ;) in this last example is a good exercise.
>>
>> There is a parallel operation VECTOR_ELT / SET_VECTOR_ELT for lists.
>>
>> > 2.  Any good references/resources for developing R?  Nearly all the
>> > documents I've found are for programming R as a user, not as a
>> > developer.  I have copies of the documentation, which are very helpful,
>> > but it'd be helpful to have additional resources to fill in their gaps.
>>
>> Chambers, 2008, Software for Data Analysis: Programming with R chapters
>> 11 & 12,
>>
>> Gentleman, 2008, R Programming for Bioinformatics chapter 6
>>
>> might be helpful, but by the time they arrive you might find that you're
>> most of the way through the material covered...
>>
>> I guess my opinion is that Rcpp would not be useful for understanding
>> R's C layer, whatever its merits for 'getting the job done'.
>>
>> Martin
>>
>> >
>> > Thank you,
>> >
>> > Patrick
>> >
>> >
>> > On Fri, Nov 12, 2010 at 4:36 PM, Martin Morgan > > > wrote:
>> >
>> > On 11/12/2010 02:31 PM, Patrick Leyshock wrote:
>> > > Hello,
>> > >
>> > > I've created this class:
>> > >
>> > > setClass("example",
>> > >representation  (
>> > >   size = "numeric",
>> > >   id= "character"
>> > >)
>> > > )
>> > >
>> > > Suppose I create a new instance of this class:
>> > >
>> > >> x <- new("example", 4, "id_value")
>> > >
>> > > This creates an S4 object with two slots.  Am I correct in
>> > thinking that
>> > > slots are "filled" by SEXPs?
>> >
>> > Hi Patrick --
>> >
>> > If I
>> >
>> > > eg = new("example", size=4, id="id_value")
>> >
>> > (note the named arguments) and take a peak at the str'ucture of eg,
>> > I see
>> >
>> > > str(eg)
>> > Formal class 'example' [package ".GlobalEnv"] with 2 slots
>> >  ..@ size: num 4
>> >  ..@ id  : chr "id_value"
>> >
>> > so the @size slot is a numeric vector of length 1 containing the
>> value
>> > 4. One doesn't really have to know the detailed representation, but
>> one
>> > can find out from
>> >
>> > > .Internal(inspect(eg))
>> > @df70e48 25 S4SXP g0c0 [OBJ,NAM(2),gp=0x10,ATT]
>> > ATTRIB:
>> >  @df70ef0 02 LISTSXP g0c0 []
>> >TAG: @769258 01 SYMSXP g1c0 [MARK] "size"
>> >@c0f6db8 14 REALSXP g0c1 [NAM(2)] (len=1, tl=0) 4
>> >TAG: @15b0228 01 SYMSXP g1c0 [MARK,NAM(2)] "id"
>> >@c0f6178 16 STRSXP g0c1 [NAM(2)] (len=1, tl=0)
>> >  @12341c80 09 CHARSXP g0c2 [gp=0x20] "id_value"
>> >TAG: @607ce8 01 SYMSXP g1c0 [MARK,NAM(2),gp=0x4000] "class"
>> >@c0f6d58 16 STRSXP g0c1 [NAM(2),ATT] (len=1, tl=0)
>> >  @96ed08 09 CHARSXP g1c1 [MARK,gp=0x21] "example"
>> >ATTRIB:
>> >  

[Rd] Create NAMESPACE file as 'package.skeleton()' would do

2010-11-15 Thread Janko Thyson
Hi there,

 

is there a way to create a NAMESPACE file based on Rd-files (or whatever is
needed in order to apply the regular expression "^[[:alpha:]]+" without(!)
resorting to package.skeleton() (as this kind of interferes with
roxygenize() pretty often)?

 

Thanks a lot,

Janko


[[alternative HTML version deleted]]

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


Re: [Rd] Create NAMESPACE file as 'package.skeleton()' would do

2010-11-15 Thread Hadley Wickham
If you're using roxygenise, explicitly tag functions that you want to
export with @export.
Hadley

On Mon, Nov 15, 2010 at 5:11 PM, Janko Thyson
 wrote:
> Hi there,
>
>
>
> is there a way to create a NAMESPACE file based on Rd-files (or whatever is
> needed in order to apply the regular expression "^[[:alpha:]]+" without(!)
> resorting to package.skeleton() (as this kind of interferes with
> roxygenize() pretty often)?
>
>
>
> Thanks a lot,
>
> Janko
>
>
>        [[alternative HTML version deleted]]
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

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


[Rd] R5 reference classes: how to initialize exactly?

2010-11-15 Thread Janko Thyson
Dear List,

 

So far, I really like those new R5 classes. But what kind of puzzles me is
that it's not just enough to define the actual reference class, I also have
to assign it to an object (e.g. 'MyRefObj') in order to fire
'MyRefObj$new(.)'.

 

S4:

setClass("Blabla", .)

x <- new("Blabla")

 

R5:

MyRefObj <- setRefClass("Blabla", .)

x <- MyRefObj$new(.)

 

But then how do I define a reference class in a package that should be
available after the package is loaded via 'library(my_pkg)' as there is no
'MyRefObj' at startup yet? Do I have to call the script where the definition
lives?

 

Thanks for any comments,

Janko

 


[[alternative HTML version deleted]]

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


Re: [Rd] R5 reference classes: how to initialize exactly?

2010-11-15 Thread Janko Thyson
Sorry, I was stupid:

 

MyRefObj <- setRefClass("Blabla", .)

 

One can always get the generator object of an defined class with
'getRefClass()'. So:

 

g <- getRefClass("Blabla")

x <- g$new(.)

 

Regards,

Janko

 

 

Von: Janko Thyson [mailto:janko.thy...@ku-eichstaett.de] 
Gesendet: Dienstag, 16. November 2010 00:27
An: 'r-de...@r-project. org'
Betreff: R5 reference classes: how to initialize exactly?

 

Dear List,

 

So far, I really like those new R5 classes. But what kind of puzzles me is
that it's not just enough to define the actual reference class, I also have
to assign it to an object (e.g. 'MyRefObj') in order to fire
'MyRefObj$new(.)'.

 

S4:

setClass("Blabla", .)

x <- new("Blabla")

 

R5:

MyRefObj <- setRefClass("Blabla", .)

x <- MyRefObj$new(.)

 

But then how do I define a reference class in a package that should be
available after the package is loaded via 'library(my_pkg)' as there is no
'MyRefObj' at startup yet? Do I have to call the script where the definition
lives?

 

Thanks for any comments,

Janko

 


[[alternative HTML version deleted]]

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


[Rd] Trying to understand the search path and namespaces

2010-11-15 Thread Hadley Wickham
Hi all,

I'm trying to understand how the search path and namespaces interact.
For example, take the devtools package which suggests the testthat
package.  Here's what the search path looks like after I load each of
those packages:

> library(devtools)
> search()
 [1] ".GlobalEnv""package:devtools"  "package:stats"
 [4] "package:graphics"  "package:grDevices" "package:utils"
 [7] "package:datasets"  "package:methods"   "Autoloads"
[10] "package:base"
> library(testthat)
> search()
 [1] ".GlobalEnv""package:testthat"  "package:devtools"
 [4] "package:stats" "package:graphics"  "package:grDevices"
 [7] "package:utils" "package:datasets"  "package:methods"
[10] "Autoloads" "package:base"

My question is this: when I execute the test function in devtools
function it calls the the test_package function in the testthat
package - but that function is located higher up the search path - how
does R find it?

(I ask this question because I'm trying to simulate package loading
from within R to simplify the development cycle, but something is
missing in my knowledge of namespaces, and so I have the devel
versions of my packages can't access packages that are loaded after
they are)

Hadley

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

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


Re: [Rd] Trying to understand the search path and namespaces

2010-11-15 Thread Henrik Bengtsson
On Mon, Nov 15, 2010 at 3:26 PM, Hadley Wickham  wrote:
> Hi all,
>
> I'm trying to understand how the search path and namespaces interact.
> For example, take the devtools package which suggests the testthat
> package.  Here's what the search path looks like after I load each of
> those packages:
>
>> library(devtools)
>> search()
>  [1] ".GlobalEnv"        "package:devtools"  "package:stats"
>  [4] "package:graphics"  "package:grDevices" "package:utils"
>  [7] "package:datasets"  "package:methods"   "Autoloads"
> [10] "package:base"
>> library(testthat)
>> search()
>  [1] ".GlobalEnv"        "package:testthat"  "package:devtools"
>  [4] "package:stats"     "package:graphics"  "package:grDevices"
>  [7] "package:utils"     "package:datasets"  "package:methods"
> [10] "Autoloads"         "package:base"
>
> My question is this: when I execute the test function in devtools
> function it calls the the test_package function in the testthat
> package - but that function is located higher up the search path - how
> does R find it?

With a small risk of being incorrect (and the chance of learning
something new), I'll give it a try:

A search for a functions/objects/... that is not in the same package
environment is done in the order that the search() path gives.  The
exception to this iff your package has a namespace, then the 'base'
package environment is always searched first (before any other
environments).  The ordering of the search is important when there
exist multiple package environments with the same function.

Is that of any help?

/Henrik

>
> (I ask this question because I'm trying to simulate package loading
> from within R to simplify the development cycle, but something is
> missing in my knowledge of namespaces, and so I have the devel
> versions of my packages can't access packages that are loaded after
> they are)
>
> Hadley
>
> --
> Assistant Professor / Dobelman Family Junior Chair
> Department of Statistics / Rice University
> http://had.co.nz/
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

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


Re: [Rd] Trying to understand the search path and namespaces

2010-11-15 Thread Hadley Wickham
> With a small risk of being incorrect (and the chance of learning
> something new), I'll give it a try:
>
> A search for a functions/objects/... that is not in the same package
> environment is done in the order that the search() path gives.  The
> exception to this iff your package has a namespace, then the 'base'
> package environment is always searched first (before any other
> environments).  The ordering of the search is important when there
> exist multiple package environments with the same function.
>
> Is that of any help?

Well, that's what I thought too.  But:

parents <- function(x) {
  if (identical(x, emptyenv())) return()
  c(environmentName(x), parents(parent.env(x)))
}
> parents(as.environment("package:devtools"))
[1] "package:devtools" "package:methods"  "Autoloads""base"

And package:testthat isn't listed there.  (But Autoloads is suggestive...)

Hadley

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

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


Re: [Rd] Trying to understand the search path and namespaces

2010-11-15 Thread Hadley Wickham
> Well, that's what I thought too.  But:
>
> parents <- function(x) {
>  if (identical(x, emptyenv())) return()
>  c(environmentName(x), parents(parent.env(x)))
> }
>> parents(as.environment("package:devtools"))
> [1] "package:devtools" "package:methods"  "Autoloads"        "base"
>
> And package:testthat isn't listed there.  (But Autoloads is suggestive...)

Hmmm, autoloads isn't it:

> parent.env(parent.env(as.environment("package:devtools")))

attr(,"name")
[1] "Autoloads"
> ls(parent.env(parent.env(as.environment("package:devtools"
character(0)

Hadley

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

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


Re: [Rd] Trying to understand the search path and namespaces

2010-11-15 Thread Martin Morgan
On 11/15/2010 04:56 PM, Hadley Wickham wrote:
>> Well, that's what I thought too.  But:
>>
>> parents <- function(x) {
>>  if (identical(x, emptyenv())) return()
>>  c(environmentName(x), parents(parent.env(x)))
>> }
>>> parents(as.environment("package:devtools"))
>> [1] "package:devtools" "package:methods"  "Autoloads""base"
>>
>> And package:testthat isn't listed there.  (But Autoloads is suggestive...)
> 
> Hmmm, autoloads isn't it:
> 
>> parent.env(parent.env(as.environment("package:devtools")))
> 
> attr(,"name")
> [1] "Autoloads"
>> ls(parent.env(parent.env(as.environment("package:devtools"
> character(0)

1.6 of Writing R Extensions says

Note that adding a name space to a package changes the search strategy.
The package name space comes first in the search, then the imports, then
the base name space and then the normal search path.

I'm not sure of the details, but I think

  parents(getNamespace("devtools"))

will give you what you want, with the gory details in loadNamespace
makeNamespace

Martin


> 
> Hadley
> 


-- 
Computational Biology
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109

Location: M1-B861
Telephone: 206 667-2793

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


Re: [Rd] Trying to understand the search path and namespaces

2010-11-15 Thread Duncan Murdoch

Hadley Wickham wrote:

Hi all,

I'm trying to understand how the search path and namespaces interact.
For example, take the devtools package which suggests the testthat
package.  Here's what the search path looks like after I load each of
those packages:


Luke Tierney wrote up a nice description of this a few years ago.  It's 
either on developer.r-project.org, or in an old issue of R News.


Duncan Murdoch




library(devtools)
search()

 [1] ".GlobalEnv""package:devtools"  "package:stats"
 [4] "package:graphics"  "package:grDevices" "package:utils"
 [7] "package:datasets"  "package:methods"   "Autoloads"
[10] "package:base"

library(testthat)
search()

 [1] ".GlobalEnv""package:testthat"  "package:devtools"
 [4] "package:stats" "package:graphics"  "package:grDevices"
 [7] "package:utils" "package:datasets"  "package:methods"
[10] "Autoloads" "package:base"

My question is this: when I execute the test function in devtools
function it calls the the test_package function in the testthat
package - but that function is located higher up the search path - how
does R find it?

(I ask this question because I'm trying to simulate package loading
from within R to simplify the development cycle, but something is
missing in my knowledge of namespaces, and so I have the devel
versions of my packages can't access packages that are loaded after
they are)

Hadley



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


Re: [Rd] Trying to understand the search path and namespaces

2010-11-15 Thread Hadley Wickham
> 1.6 of Writing R Extensions says
>
> Note that adding a name space to a package changes the search strategy.
> The package name space comes first in the search, then the imports, then
> the base name space and then the normal search path.
>
> I'm not sure of the details, but I think
>
>  parents(getNamespace("devtools"))

Ah, my mistake was assuming that the package namespace and environment
were the same thing.

Interestingly the namespace is dynamic:

> parents(getNamespace("devtools"))
...
[31] "base"
> library(roxygen)
> parents(getNamespace("devtools"))
...
[31] "Autoloads""base"

Hadley

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

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


Re: [Rd] Trying to understand the search path and namespaces

2010-11-15 Thread Mark Leeds
Hi Duncan: Luke's article is in the June,  2003 edition of R-news

On Mon, Nov 15, 2010 at 8:43 PM, Duncan Murdoch wrote:

> Hadley Wickham wrote:
>
>> Hi all,
>>
>> I'm trying to understand how the search path and namespaces interact.
>> For example, take the devtools package which suggests the testthat
>> package.  Here's what the search path looks like after I load each of
>> those packages:
>>
>
> Luke Tierney wrote up a nice description of this a few years ago.  It's
> either on developer.r-project.org, or in an old issue of R News.
>
> Duncan Murdoch
>
>
>>  library(devtools)
>>> search()
>>>
>>  [1] ".GlobalEnv""package:devtools"  "package:stats"
>>  [4] "package:graphics"  "package:grDevices" "package:utils"
>>  [7] "package:datasets"  "package:methods"   "Autoloads"
>> [10] "package:base"
>>
>>> library(testthat)
>>> search()
>>>
>>  [1] ".GlobalEnv""package:testthat"  "package:devtools"
>>  [4] "package:stats" "package:graphics"  "package:grDevices"
>>  [7] "package:utils" "package:datasets"  "package:methods"
>> [10] "Autoloads" "package:base"
>>
>> My question is this: when I execute the test function in devtools
>> function it calls the the test_package function in the testthat
>> package - but that function is located higher up the search path - how
>> does R find it?
>>
>> (I ask this question because I'm trying to simulate package loading
>> from within R to simplify the development cycle, but something is
>> missing in my knowledge of namespaces, and so I have the devel
>> versions of my packages can't access packages that are loaded after
>> they are)
>>
>> Hadley
>>
>>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

[[alternative HTML version deleted]]

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


Re: [Rd] Bug in read.table?

2010-11-15 Thread Ben Bolker
Ben Bolker  gmail.com> writes:

> 
> Ben Bolker  gmail.com> writes:
> 
> >
> >
> 
>Can simplify this still farther:
> 
> a b'c
> d e'f
> g h'i

  This example file leads to duplicate lines.
Arguably it should have behavior analogous to:

> scan(what="")
1: a b'c
3: d e'f
5: g h'i
7: Read 6 items
[1] "a"   "b'c" "d"   "e'f" "g"   "h'i"


> 
> >  One of the first things that happens in read.table is that
> > the first few lines are read with readTableHead:
> > 
> >   lines <- .Internal(readTableHead(file, nlines, comment.char, 
> >blank.lines.skip, quote, sep))
> > 
>   in this case, this reads the first two lines as one line;
> the single quote at pos. 4 of the first line closes on pos.
> 4 of the second line, preventing the first new line from
> ending a line.
> 
>   R then pushes back two copies of the lines that have
> been read (this is normal behavior; I don't quite follow the
> logic).
> 
>   The rest of the file is read with scan(), 1 line at a time.
> However, there is the discrepancy between the way
> that readTableHead interprets new lines in the middle of
> quoted strings (it ignores them) and the way that scan()
> interprets them (it takes them as the end of the quoted string).


  Ping?
  I think this counts as a small, but real, bug. Should I go ahead
and report it as such, or would someone explain why it's not a bug?

  cheers
Ben Bolker

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