[R-pkg-devel] Adding non-exported functions in an extra environment in new S4 object

2016-01-21 Thread Rainer M Krug
Hi

I have a package named asm which contains many non-exported functions
which are all starting with "for.eq.".

I also have an S4 class ASM which, when a new object asm of class ASM is
created by using the function newASM() (in the package asm) contains
among other slots one sot with an environment called "equations"
(including the definition of the function"equations" and "equations<-").

Now in this function newASM() I want to copy all functions starting with
"for.eq." into this environment.

I have the following code:

--8<---cut here---start->8---
ASM@equations <- new.env()
funs <- apropos("^for\\.eq\\.", mode="function")
for (fun in funs) {
assign(
gsub("for.eq.", "", fun),
get(fun),
ASM@equations
)
}
--8<---cut here---end--->8---

outside the package I call it as follow:

--8<---cut here---start->8---
asm <- newASM()
--8<---cut here---end--->8---

This worked fine when the functions "for.eq.*" were exported, but does
not work anymore now that I don't export them anymore.

This is a bit surprising to me, as the function newASM() is in the
package and I assumed that inside of a package, all exported and
non-exported functions are seen.

My question:

how can I add all functions starting with "for.eq." into the
environment, irrespective if they are exported or not?

Why I want to do this: I want to be able to have the functions
encapsulated in the environment of the object asm as they are used in a
simulation executed by the sim() function, which attaches the equations
environment at the beginning and detaches it at the end.

This enables me to have different simulation objects with different
functions.

Thanks,

Rainer

-- 
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, 
UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Stellenbosch University
South Africa

Tel :   +33 - (0)9 53 10 27 44
Cell:   +33 - (0)6 85 62 59 98
Fax :   +33 - (0)9 58 10 27 44

Fax (D):+49 - (0)3 21 21 25 22 44

email:  rai...@krugs.de

Skype:  RMkrug

PGP: 0x0F52F982


signature.asc
Description: PGP signature
__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel

Re: [R-pkg-devel] Adding non-exported functions in an extra environment in new S4 object

2016-01-21 Thread Rainer M Krug
Rainer M Krug  writes:

> Hi
>
> I have a package named asm which contains many non-exported functions
> which are all starting with "for.eq.".
>
> I also have an S4 class ASM which, when a new object asm of class ASM is
> created by using the function newASM() (in the package asm) contains
> among other slots one sot with an environment called "equations"
> (including the definition of the function"equations" and "equations<-").
>
> Now in this function newASM() I want to copy all functions starting with
> "for.eq." into this environment.
>
> I have the following code:
>
> ASM@equations <- new.env()
> funs <- apropos("^for\\.eq\\.", mode="function")
> for (fun in funs) {
> assign(
> gsub("for.eq.", "", fun),
> get(fun),
> ASM@equations
> )
> }
>
> outside the package I call it as follow:
>
> asm <- newASM()
>
> This worked fine when the functions "for.eq.*" were exported, but does
> not work anymore now that I don't export them anymore.
>
> This is a bit surprising to me, as the function newASM() is in the
> package and I assumed that inside of a package, all exported and
> non-exported functions are seen.
>
> My question:
>
> how can I add all functions starting with "for.eq." into the
> environment, irrespective if they are exported or not?

I found a solution:

--8<---cut here---start->8---
funs <- grep(
pattern = "for.eq.*",
x = ls(
getNamespace("asm"),
all.names=TRUE
),
value = TRUE
)
for (fun in funs) {
assign(
gsub("for.eq.", "", fun),
get(fun, getNamespace("asm")),
ASM@equations
)
}
--8<---cut here---end--->8---

It adds all objects starting with "for.eq." but this is fine with me.

Thanks,

Rainer

>
> Why I want to do this: I want to be able to have the functions
> encapsulated in the environment of the object asm as they are used in a
> simulation executed by the sim() function, which attaches the equations
> environment at the beginning and detaches it at the end.
>
> This enables me to have different simulation objects with different
> functions.
>
> Thanks,
>
> Rainer

-- 
Rainer M. Krug
email: Rainerkrugsde
PGP: 0x0F52F982


signature.asc
Description: PGP signature
__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel

[R-pkg-devel] How to pass a connection into a C function (R 3.2)

2016-01-21 Thread Tom Quarendon
I would like to implement a package that contains a C function that writes to a 
connection.
So the R_ext/Connections.h defines what a connection IS, but not how to get one.

What seems to work is to manually declare
Rconnection getConnection(int n);
In my C file, and while this works, and I can then use the connection object, 
it fails package validation with:

�Found non-API call to R: �getConnection�
Compiled code should not call non-API entry points in R.�

So how am I supposed to do this? Is there a way? Or do I just live with the 
check warning (ideally not)?

Thanks.

[[alternative HTML version deleted]]

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

Re: [R-pkg-devel] How to pass a connection into a C function (R 3.2)

2016-01-21 Thread Duncan Murdoch

On 21/01/2016 11:39 AM, Tom Quarendon wrote:

I would like to implement a package that contains a C function that writes to a 
connection.
So the R_ext/Connections.h defines what a connection IS, but not how to get one.

What seems to work is to manually declare
Rconnection getConnection(int n);
In my C file, and while this works, and I can then use the connection object, 
it fails package validation with:

�Found non-API call to R: �getConnection�
Compiled code should not call non-API entry points in R.�

So how am I supposed to do this? Is there a way? Or do I just live with the 
check warning (ideally not)?


The usual way to do that would be to create the connection in R code, 
and pass it in with your call.


If you don't know what connection you need at the time you call your C 
code, you can evaluate an R expression from C to do the same, but this 
is relatively tricky, so I'd advise the other method.


Duncan Murdoch

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

Re: [R-pkg-devel] How to pass a connection into a C function (R 3.2)

2016-01-21 Thread Tom Quarendon
But that's what I want to do, create the connection in R code and pass it on to 
the C function.
My question is, how do I do that?
You appear to need the C getConnection entry point, but it's not part of the 
API. 
So I'm wondering how I'm supposed to do what you describe.

Sorry if I was unclear.

Sent from my iPhone

> On 21 Jan 2016, at 5:44 pm, Duncan Murdoch  wrote:
> 
>> On 21/01/2016 11:39 AM, Tom Quarendon wrote:
>> I would like to implement a package that contains a C function that writes 
>> to a connection.
>> So the R_ext/Connections.h defines what a connection IS, but not how to get 
>> one.
>> 
>> What seems to work is to manually declare
>> Rconnection getConnection(int n);
>> In my C file, and while this works, and I can then use the connection 
>> object, it fails package validation with:
>> 
>> �Found non-API call to R: �getConnection�
>> Compiled code should not call non-API entry points in R.�
>> 
>> So how am I supposed to do this? Is there a way? Or do I just live with the 
>> check warning (ideally not)?
> 
> The usual way to do that would be to create the connection in R code, and 
> pass it in with your call.
> 
> If you don't know what connection you need at the time you call your C code, 
> you can evaluate an R expression from C to do the same, but this is 
> relatively tricky, so I'd advise the other method.
> 
> Duncan Murdoch
__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel

Re: [R-pkg-devel] How to pass a connection into a C function (R 3.2)

2016-01-21 Thread Tom Quarendon
But that's what I want to do, create the connection in R code and pass it on to 
the C function.
My question is, how do I do that?
You appear to need the C getConnection entry point, but it's not part of the 
API. 
So I'm wondering how I'm supposed to do what you describe.

Sorry if I was unclear.

Sent from my iPhone

>> On 21 Jan 2016, at 5:44 pm, Duncan Murdoch  wrote:
>> 
>> On 21/01/2016 11:39 AM, Tom Quarendon wrote:
>> I would like to implement a package that contains a C function that writes 
>> to a connection.
>> So the R_ext/Connections.h defines what a connection IS, but not how to get 
>> one.
>> 
>> What seems to work is to manually declare
>> Rconnection getConnection(int n);
>> In my C file, and while this works, and I can then use the connection 
>> object, it fails package validation with:
>> 
>> �Found non-API call to R: �getConnection�
>> Compiled code should not call non-API entry points in R.�
>> 
>> So how am I supposed to do this? Is there a way? Or do I just live with the 
>> check warning (ideally not)?
> 
> The usual way to do that would be to create the connection in R code, and 
> pass it in with your call.
> 
> If you don't know what connection you need at the time you call your C code, 
> you can evaluate an R expression from C to do the same, but this is 
> relatively tricky, so I'd advise the other method.
> 
> Duncan Murdoch
__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel

Re: [R-pkg-devel] How to pass a connection into a C function (R 3.2)

2016-01-21 Thread Duncan Murdoch

On 21/01/2016 12:50 PM, Tom Quarendon wrote:

But that's what I want to do, create the connection in R code and pass it on to 
the C function.
My question is, how do I do that?
You appear to need the C getConnection entry point, but it's not part of the 
API.
So I'm wondering how I'm supposed to do what you describe.


Sorry, my advice was wrong.  I remembered that we exposed code to create 
new connections, and assumed it had some code to work with them, but it 
doesn't.


Duncan Murdoch


Sorry if I was unclear.

Sent from my iPhone

>> On 21 Jan 2016, at 5:44 pm, Duncan Murdoch  wrote:
>>
>> On 21/01/2016 11:39 AM, Tom Quarendon wrote:
>> I would like to implement a package that contains a C function that writes 
to a connection.
>> So the R_ext/Connections.h defines what a connection IS, but not how to get 
one.
>>
>> What seems to work is to manually declare
>> Rconnection getConnection(int n);
>> In my C file, and while this works, and I can then use the connection 
object, it fails package validation with:
>>
>> �Found non-API call to R: �getConnection�
>> Compiled code should not call non-API entry points in R.�
>>
>> So how am I supposed to do this? Is there a way? Or do I just live with the 
check warning (ideally not)?
>
> The usual way to do that would be to create the connection in R code, and 
pass it in with your call.
>
> If you don't know what connection you need at the time you call your C code, 
you can evaluate an R expression from C to do the same, but this is relatively 
tricky, so I'd advise the other method.
>
> Duncan Murdoch


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

Re: [R-pkg-devel] How to pass a connection into a C function (R 3.2)

2016-01-21 Thread Tom Quarendon
So upshot is that I can only do why I want by using the function that's not 
part of the api, or just not using connections at all and just using normal C 
file io, opened from C, and simply pass the file name in instead, rather than a 
proper connection object?

> On 21 Jan 2016, at 6:04 pm, Duncan Murdoch  wrote:
> 
>> On 21/01/2016 12:50 PM, Tom Quarendon wrote:
>> But that's what I want to do, create the connection in R code and pass it on 
>> to the C function.
>> My question is, how do I do that?
>> You appear to need the C getConnection entry point, but it's not part of the 
>> API.
>> So I'm wondering how I'm supposed to do what you describe.
> 
> Sorry, my advice was wrong.  I remembered that we exposed code to create new 
> connections, and assumed it had some code to work with them, but it doesn't.
> 
> Duncan Murdoch
>> 
>> Sorry if I was unclear.
>> 
>> Sent from my iPhone
>> 
>> >> On 21 Jan 2016, at 5:44 pm, Duncan Murdoch  
>> >> wrote:
>> >>
>> >> On 21/01/2016 11:39 AM, Tom Quarendon wrote:
>> >> I would like to implement a package that contains a C function that 
>> >> writes to a connection.
>> >> So the R_ext/Connections.h defines what a connection IS, but not how to 
>> >> get one.
>> >>
>> >> What seems to work is to manually declare
>> >> Rconnection getConnection(int n);
>> >> In my C file, and while this works, and I can then use the connection 
>> >> object, it fails package validation with:
>> >>
>> >> �Found non-API call to R: �getConnection�
>> >> Compiled code should not call non-API entry points in R.�
>> >>
>> >> So how am I supposed to do this? Is there a way? Or do I just live with 
>> >> the check warning (ideally not)?
>> >
>> > The usual way to do that would be to create the connection in R code, and 
>> > pass it in with your call.
>> >
>> > If you don't know what connection you need at the time you call your C 
>> > code, you can evaluate an R expression from C to do the same, but this is 
>> > relatively tricky, so I'd advise the other method.
>> >
>> > Duncan Murdoch
> 
__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel

Re: [R-pkg-devel] How to pass a connection into a C function (R 3.2)

2016-01-21 Thread Duncan Murdoch

On 21/01/2016 1:07 PM, Tom Quarendon wrote:

So upshot is that I can only do why I want by using the function that's not 
part of the api, or just not using connections at all and just using normal C 
file io, opened from C, and simply pass the file name in instead, rather than a 
proper connection object?


You could also call R from C to do the I/O for you.

Duncan Murdoch


On 21 Jan 2016, at 6:04 pm, Duncan Murdoch  wrote:


On 21/01/2016 12:50 PM, Tom Quarendon wrote:
But that's what I want to do, create the connection in R code and pass it on to 
the C function.
My question is, how do I do that?
You appear to need the C getConnection entry point, but it's not part of the 
API.
So I'm wondering how I'm supposed to do what you describe.


Sorry, my advice was wrong.  I remembered that we exposed code to create new 
connections, and assumed it had some code to work with them, but it doesn't.

Duncan Murdoch


Sorry if I was unclear.

Sent from my iPhone


On 21 Jan 2016, at 5:44 pm, Duncan Murdoch  wrote:

On 21/01/2016 11:39 AM, Tom Quarendon wrote:
I would like to implement a package that contains a C function that writes to a 
connection.
So the R_ext/Connections.h defines what a connection IS, but not how to get one.

What seems to work is to manually declare
Rconnection getConnection(int n);
In my C file, and while this works, and I can then use the connection object, 
it fails package validation with:

�Found non-API call to R: �getConnection�
Compiled code should not call non-API entry points in R.�

So how am I supposed to do this? Is there a way? Or do I just live with the 
check warning (ideally not)?


The usual way to do that would be to create the connection in R code, and pass 
it in with your call.

If you don't know what connection you need at the time you call your C code, 
you can evaluate an R expression from C to do the same, but this is relatively 
tricky, so I'd advise the other method.

Duncan Murdoch




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