[Rd] using openbabel plugins in R

2013-03-26 Thread Kevin Horan
I posted this in openbabel-devel but didn't get much help, so hopefully 
someone here can help. I don't think its too openbabel specific.


I would like to make use of open babel from within the R language.
Initially I just need to do some format conversions, but may expand the
usage to other parts of OpenBabel as well. I am familiar with embedding
C/C++ code in R, but I'm having some trouble with the plugin mechanism
of OpenBabel in this case. The  problem is that the formats are not
available when I run the OpenBabel code from within R. So, for example,
if I search for the SDF format like so:
 OBFormat *format = conv.FindFormat("SDF");
I always get back a 0 value. The same chunk of code executed outside of
R, as a normal stand-alone program, works fine. So does anyone know how
I can ensure that the formats get loaded? Thanks.
 One other thing to mention, someone might suggest linking against a
static version of openbabel which includes all the plugins. I would like
to avoid that if possible since this needs to work in an R package that
will be distributed across platforms, so it would be hard to ask people
to compile a special, static, version of openbabel just to compile this
R package. Since it needs to work on windows, mac and linux, it would be
nice  if I can make use of any existing installed shared obenbabel
libraries. If it turns out it can't be done, then I'll go down that
path. Thanks.

 Here is an example of the problem:

test program (obtest2.cc):

#include 
#include 
#include 
#include 

extern "C" {   SEXP test(); }

int main(){
test();
}
SEXP  test()
{
OpenBabel::OBConversion conv;
OpenBabel::OBFormat *format = conv.FindFormat("SDF");   //
search for SDF format
std::cout<<"format: "dyn.load("libobtest2.so")
 R>.Call("test")
 format: 0  # the format was not found, so 0 was
returned
 NULL


After some more experimentation, I have discovered I can get it to work 
in the following way, but I think it is a bit impractical. If I compile 
the shared library as:
g++ -shared -o libobtest2.so obtest2.o -fpic 
/usr/lib/openbabel/2.2.3/mdlformat.so -lopenbabel -lR

so the name of one of the plugins is specified. Then, in R I run:

R>dyn.load("/usr/lib/openbabel/2.2.3/mdlformat.so")
R>dyn.load("libobtest2.so")
R>.Call("test")
format: 0x7fe114c96d20
NULL
So then it works. But this requires that I know the full path to every 
plugin when the code is compiled and when the library is loaded. Is 
there a practical way to do this, say, if this were part of an R 
package? I have also tried compiling the shared library as before, 
without the plugin, and then just loading the plugin with dyn.load but 
this does not work. It seems like it should though, does anyone know why 
it doesn't? Conversely, if you compile with the plugin specified, but 
don't load it with dyn.load it seg faults.
The way it works normally in OpenBabel is that each plugin is its 
own shared library and then they get loaded at run time with the dlopen 
function (on linux at least). I have verified that this code is still 
being executed when called from within R, but it doesn't work for some 
reason.

Also, swig does not help.

Thanks.

Kevin

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


Re: [Rd] using openbabel plugins in R

2013-03-26 Thread Dirk Eddelbuettel

On 25 March 2013 at 12:50, Kevin Horan wrote:
| I posted this in openbabel-devel but didn't get much help, so hopefully 
| someone here can help. I don't think its too openbabel specific.
| 
| I would like to make use of open babel from within the R language.
| Initially I just need to do some format conversions, but may expand the
| usage to other parts of OpenBabel as well. I am familiar with embedding
| C/C++ code in R, but I'm having some trouble with the plugin mechanism
| of OpenBabel in this case. The  problem is that the formats are not
| available when I run the OpenBabel code from within R. So, for example,
| if I search for the SDF format like so:
|   OBFormat *format = conv.FindFormat("SDF");

[...]

|  The way it works normally in OpenBabel is that each plugin is its 
| own shared library and then they get loaded at run time with the dlopen 
| function (on linux at least). I have verified that this code is still 
| being executed when called from within R, but it doesn't work for some 
| reason.

I would try to start from the smallest possible working examples.  R itself
uses dlopen (see eg $RHOME/modules/ for the files it loads), and so does
OpenBabel. Maybe some wires get crossed. You may well have to dig down with
the debugger and see what assumptions / environment variables / ... are valid
or invalid between the working and non-working case.

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] removing union class

2013-03-26 Thread Martin Maechler
> "MM" == Martin Maechler 
> on Tue, 19 Mar 2013 17:16:42 +0100 writes:

> "RG" == Renaud Gaujoux 
> on Sun, 17 Mar 2013 10:38:44 +0200 writes:

RG> Late report is better than never isn't it? :)
>>> > Well,... you forgot to show the error (and the
>>> traceback) :
>>> 

RG> Apologies, I usually include them (and sessionInfo ...).


>>> 
>>> > Note that this problem is somewhat dependent on the
>>> use of the > infamous "matrix" class {not properly
>>> defined as a class in S3, > as it may or may not have
>>> dimnames, and then tried to be made S4 > compatible "as
>>> well as possible" in the methods package, see
>>> 

RG> Ok. What I want to do is to define a class in my
RG> namespace that gather some matrix-like classes so that I
RG> can define a set of common functions for them.
RG> Initially I want matrix and ExpressionSet objects, and
RG> possibly add array objects later.  Union classes seem to
RG> be exactly the way to go, but maybe there is an
RG> alternative?

MM> There's really *no* problem with class unions.  They
MM> work fine and nicely... we also use them in the Matrix
MM> package (part of the R distro).

MM> The problem you've reported is only a propblem of
MM> removeClass().

I've committed a patch for this to R-devel yesterday, and to 
R 3.0.0_beta, a few minutes ago.

The solution for now has been to cut one of the ties between
"matrix" and "array" in ``class space''.

Martin


>>> > Of course, I could not have thought of a realistic
>>> situation > where this was a problem, but then you
>>> exemplify one :
>>> 
>>> >> Hadley, this is problematic for devtools, because
>>> load_all tries to cleanup >> S4 classes when an error
>>> occurs when loading a development package and >> crashes
>>> with no hint on the original error.
>>> 
>>> 
RG> I guess a quick fix for devtools, would be to wrap the
RG> cleanup procedure into a try or tryCatch (Hadley?).


RG> Thanks, Renaud

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


[Rd] C API entry point to currentTime()

2013-03-26 Thread Matthew Dowle


Hi,

I used to use currentTime() (from /src/main/datetime.c) to time various 
sections of data.table C code in wall clock time in sub-second accuracy 
(type double), consistently across platforms. The consistency across 
platforms is a really nice feature of currentTime(). But currentTime() 
isn't part of R's API so I changed to clock() in order to pass R3 
checks. This is nicer in many ways but I'd still like to time elapsed 
wall clock time as well, since some of the operations are i/o bound.


Does R provide a C entry point to currentTime() (or equivalent) 
suitable for use by packages?  I searched r-devel archive and the 
manuals but may well have missed it.


Thanks, Matthew

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


[Rd] as.Date.POSIXct

2013-03-26 Thread Joshua Ulrich
Would it make sense for as.Date.POSIXct to not assume tz="UTC" if the
POSIXct object has a valid tzone attribute?  Current behavior may be
confusing in certain cases, for example:

> (d <- structure(1090450800, tzone="Europe/Berlin",
+ class=c("POSIXct","POSIXt")))
[1] "2004-07-22 01:00:00 CEST"
> as.Date(d)
[1] "2004-07-21"
> as.Date(as.POSIXlt(d))
[1] "2004-07-22"
> as.Date(d, tz=attr(d,'tzone'))
[1] "2004-07-22"

Best,
--
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com

R/Finance 2013: Applied Finance with R  | www.RinFinance.com

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


Re: [Rd] source, sys.source and error line numbers

2013-03-26 Thread Hadley Wickham
It turns out the reason for this is pretty simple:

sys.source does:
for (i in exprs) eval(i, envir)

where source basically does
n <- length(exprs)
for (i in seq_len(n)) eval(expr[i], envir)

so the problem is presumably related to the way that for strips attributes.

Hadley

On Tue, Mar 19, 2013 at 4:03 AM, Renaud Gaujoux
 wrote:
> Hi,
>
> is there a way to retrieve the line number of where en error occurred when
> sourcing a file in a tryCatch statement? Is it stored somewhere accessible?
> It is not found in the error object.
>
> Consider the following code/output and note the difference in the traceback
> between source (has line number) and sys.source (has no line number).
>
> Thank you,
> Renaud
>
>
> 
> # code
> 
> codefile <- tempfile()
> write("# some comment
> # some good lines
> a <- 1
> # a bad line
> stop('an error')
> # another good line
> b <- 2
> ", file=codefile)
>
> # with source() the line number is displayed
> source(codefile)
> traceback()
> tryCatch(source(codefile), error= function(e){ str(e) })
>
> # with sys.source() the line number is _not_ displayed
> e <- new.env()
> sys.source(codefile, e)
> traceback()
>
> sessionInfo()
>
> #
> # output
> #
>
>> codefile <- tempfile()
>> write("# some comment
> + # some good lines
> + a <- 1
> + # a bad line
> + stop('an error')
> + # another good line
> + b <- 2
> + ", file=codefile)
>> # with source() the line number is displayed
>> source(codefile)
> Error in eval(expr, envir, enclos) : an error
>> traceback()
> 5: stop("an error") at file46641af8754#5
> 4: eval(expr, envir, enclos)
> 3: eval(ei, envir)
> 2: withVisible(eval(ei, envir))
> 1: source(codefile)
>> tryCatch(source(codefile), error= function(e){ str(e) })
> List of 2
>  $ message: chr "an error"
>  $ call   : language eval(expr, envir, enclos)
>  - attr(*, "class")= chr [1:3] "simpleError" "error" "condition"
>>
>> # with sys.source() the line number is _not_ displayed
>> e <- new.env()
>> sys.source(codefile, e)
> Error in eval(expr, envir, enclos) : an error
>> traceback()
> 4: stop("an error")
> 3: eval(expr, envir, enclos)
> 2: eval(i, envir)
> 1: sys.source(codefile, e)
>>
>> sessionInfo()
> R version 2.15.3 (2013-03-01)
> Platform: i686-pc-linux-gnu (32-bit)
>
> locale:
>  [1] LC_CTYPE=en_US.UTF-8   LC_NUMERIC=C
>  [3] LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8
>  [5] LC_MONETARY=en_US.UTF-8LC_MESSAGES=en_US.UTF-8
>  [7] LC_PAPER=C LC_NAME=C
>  [9] LC_ADDRESS=C   LC_TELEPHONE=C
> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats graphics  grDevices utils datasets  methods   base
>
> [[alternative HTML version deleted]]
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



-- 
Chief Scientist, RStudio
http://had.co.nz/

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


Re: [Rd] source, sys.source and error line numbers

2013-03-26 Thread Duncan Murdoch

On 13-03-26 6:45 PM, Hadley Wickham wrote:

It turns out the reason for this is pretty simple:

sys.source does:
for (i in exprs) eval(i, envir)

where source basically does
n <- length(exprs)
for (i in seq_len(n)) eval(expr[i], envir)

so the problem is presumably related to the way that for strips attributes.


That's part of it, but there are also different defaults for 
keep.source.  It needs to be TRUE or the error location won't be known.


I'll fix the difference you noticed after 3.0.0 is released; I think it 
is not serious enough to slip in at this point.


Duncan Murdoch



Hadley

On Tue, Mar 19, 2013 at 4:03 AM, Renaud Gaujoux
 wrote:

Hi,

is there a way to retrieve the line number of where en error occurred when
sourcing a file in a tryCatch statement? Is it stored somewhere accessible?
It is not found in the error object.

Consider the following code/output and note the difference in the traceback
between source (has line number) and sys.source (has no line number).

Thank you,
Renaud



# code

codefile <- tempfile()
write("# some comment
# some good lines
a <- 1
# a bad line
stop('an error')
# another good line
b <- 2
", file=codefile)

# with source() the line number is displayed
source(codefile)
traceback()
tryCatch(source(codefile), error= function(e){ str(e) })

# with sys.source() the line number is _not_ displayed
e <- new.env()
sys.source(codefile, e)
traceback()

sessionInfo()

#
# output
#


codefile <- tempfile()
write("# some comment

+ # some good lines
+ a <- 1
+ # a bad line
+ stop('an error')
+ # another good line
+ b <- 2
+ ", file=codefile)

# with source() the line number is displayed
source(codefile)

Error in eval(expr, envir, enclos) : an error

traceback()

5: stop("an error") at file46641af8754#5
4: eval(expr, envir, enclos)
3: eval(ei, envir)
2: withVisible(eval(ei, envir))
1: source(codefile)

tryCatch(source(codefile), error= function(e){ str(e) })

List of 2
  $ message: chr "an error"
  $ call   : language eval(expr, envir, enclos)
  - attr(*, "class")= chr [1:3] "simpleError" "error" "condition"


# with sys.source() the line number is _not_ displayed
e <- new.env()
sys.source(codefile, e)

Error in eval(expr, envir, enclos) : an error

traceback()

4: stop("an error")
3: eval(expr, envir, enclos)
2: eval(i, envir)
1: sys.source(codefile, e)


sessionInfo()

R version 2.15.3 (2013-03-01)
Platform: i686-pc-linux-gnu (32-bit)

locale:
  [1] LC_CTYPE=en_US.UTF-8   LC_NUMERIC=C
  [3] LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8
  [5] LC_MONETARY=en_US.UTF-8LC_MESSAGES=en_US.UTF-8
  [7] LC_PAPER=C LC_NAME=C
  [9] LC_ADDRESS=C   LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats graphics  grDevices utils datasets  methods   base

 [[alternative HTML version deleted]]

__
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


[Rd] mean.data.frame: R 3.0.0 help page wrong?

2013-03-26 Thread David Winsemius

The help page for mean still says there is a method for data.frame although 
this has been deprecated for several versions and in R 3.0.0 beta I get:

 mean(data.frame(x=rnorm(10), y=rnorm(10))  )
[1] NA
Warning message:
In mean.default(data.frame(x = rnorm(10), y = rnorm(10))) :
  argument is not numeric or logical: returning NA

I read in news():

o   mean() for data frames and sd() for data frames and matrices are
defunct.


Shouldn't the help page be amended?
-- 
David Winsemius
Alameda, CA, USA

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


Re: [Rd] source, sys.source and error line numbers

2013-03-26 Thread Renaud Gaujoux
Sounds good for me.
Thanks.


2013/3/27 Duncan Murdoch 

> On 13-03-26 6:45 PM, Hadley Wickham wrote:
>
>> It turns out the reason for this is pretty simple:
>>
>> sys.source does:
>> for (i in exprs) eval(i, envir)
>>
>> where source basically does
>> n <- length(exprs)
>> for (i in seq_len(n)) eval(expr[i], envir)
>>
>> so the problem is presumably related to the way that for strips
>> attributes.
>>
>
> That's part of it, but there are also different defaults for keep.source.
>  It needs to be TRUE or the error location won't be known.
>
> I'll fix the difference you noticed after 3.0.0 is released; I think it is
> not serious enough to slip in at this point.
>
> Duncan Murdoch
>
>
>
>> Hadley
>>
>> On Tue, Mar 19, 2013 at 4:03 AM, Renaud Gaujoux
>>  wrote:
>>
>>> Hi,
>>>
>>> is there a way to retrieve the line number of where en error occurred
>>> when
>>> sourcing a file in a tryCatch statement? Is it stored somewhere
>>> accessible?
>>> It is not found in the error object.
>>>
>>> Consider the following code/output and note the difference in the
>>> traceback
>>> between source (has line number) and sys.source (has no line number).
>>>
>>> Thank you,
>>> Renaud
>>>
>>>
>>> 
>>> # code
>>> 
>>> codefile <- tempfile()
>>> write("# some comment
>>> # some good lines
>>> a <- 1
>>> # a bad line
>>> stop('an error')
>>> # another good line
>>> b <- 2
>>> ", file=codefile)
>>>
>>> # with source() the line number is displayed
>>> source(codefile)
>>> traceback()
>>> tryCatch(source(codefile), error= function(e){ str(e) })
>>>
>>> # with sys.source() the line number is _not_ displayed
>>> e <- new.env()
>>> sys.source(codefile, e)
>>> traceback()
>>>
>>> sessionInfo()
>>>
>>> #
>>> # output
>>> #
>>>
>>>  codefile <- tempfile()
 write("# some comment

>>> + # some good lines
>>> + a <- 1
>>> + # a bad line
>>> + stop('an error')
>>> + # another good line
>>> + b <- 2
>>> + ", file=codefile)
>>>
 # with source() the line number is displayed
 source(codefile)

>>> Error in eval(expr, envir, enclos) : an error
>>>
 traceback()

>>> 5: stop("an error") at file46641af8754#5
>>> 4: eval(expr, envir, enclos)
>>> 3: eval(ei, envir)
>>> 2: withVisible(eval(ei, envir))
>>> 1: source(codefile)
>>>
 tryCatch(source(codefile), error= function(e){ str(e) })

>>> List of 2
>>>   $ message: chr "an error"
>>>   $ call   : language eval(expr, envir, enclos)
>>>   - attr(*, "class")= chr [1:3] "simpleError" "error" "condition"
>>>

 # with sys.source() the line number is _not_ displayed
 e <- new.env()
 sys.source(codefile, e)

>>> Error in eval(expr, envir, enclos) : an error
>>>
 traceback()

>>> 4: stop("an error")
>>> 3: eval(expr, envir, enclos)
>>> 2: eval(i, envir)
>>> 1: sys.source(codefile, e)
>>>

 sessionInfo()

>>> R version 2.15.3 (2013-03-01)
>>> Platform: i686-pc-linux-gnu (32-bit)
>>>
>>> locale:
>>>   [1] LC_CTYPE=en_US.UTF-8   LC_NUMERIC=C
>>>   [3] LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8
>>>   [5] LC_MONETARY=en_US.UTF-8LC_MESSAGES=en_US.UTF-8
>>>   [7] LC_PAPER=C LC_NAME=C
>>>   [9] LC_ADDRESS=C   LC_TELEPHONE=C
>>> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
>>>
>>> attached base packages:
>>> [1] stats graphics  grDevices utils datasets  methods   base
>>>
>>>  [[alternative HTML version deleted]]
>>>
>>> __**
>>> 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