[OpenBabel-Devel] using openbabel in R
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: ".Call("test") format: 0 # the format was not found, so 0 was returned NULL Kevin -- Everyone hates slow websites. So do we. Make your web apps faster with AppDynamics Download AppDynamics Lite for free today: http://p.sf.net/sfu/appdyn_d2d_mar ___ OpenBabel-Devel mailing list OpenBabel-Devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/openbabel-devel
Re: [OpenBabel-Devel] using openbabel in R
This isn't exactly answering your question, but have you considered creating the R Swig wrapper. This may just simply work out of the box (or indeed not!) with little effort. - Noel On 20 March 2013 16:13, Kevin Horan wrote: > 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: "< // print out search result, either 0 > > // or an address > return R_NilValue; > } > > > compile: > g++ -I/usr/include/openbabel-2.0 -I/usr/share/R/include -fpic -c > obtest2.cc -o obtest2.o > g++ -o obtest2 obtest2.o -fpic -lopenbabel > -lR # Executable > g++ -shared -o libobtest2.so obtest2.o -fpic -lopenbabel -lR # R > library > > Run executable: > $ ./obtest2 > format: 0x7f1858275d20 #found some result, this is what I expect > > Run in R: > R>dyn.load("libobtest2.so") > R>.Call("test") > format: 0 # the format was not found, so 0 was > returned > NULL > > Kevin > > > -- > Everyone hates slow websites. So do we. > Make your web apps faster with AppDynamics > Download AppDynamics Lite for free today: > http://p.sf.net/sfu/appdyn_d2d_mar > ___ > OpenBabel-Devel mailing list > OpenBabel-Devel@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/openbabel-devel -- Everyone hates slow websites. So do we. Make your web apps faster with AppDynamics Download AppDynamics Lite for free today: http://p.sf.net/sfu/appdyn_d2d_mar ___ OpenBabel-Devel mailing list OpenBabel-Devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/openbabel-devel
Re: [OpenBabel-Devel] using openbabel in R
Thanks, but swig did not help. 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 #this is the correct result 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. Kevin On 03/21/2013 05:51 AM, Noel O'Boyle wrote: > This isn't exactly answering your question, but have you considered > creating the R Swig wrapper. This may just simply work out of the box > (or indeed not!) with little effort. > > - Noel > > On 20 March 2013 16:13, Kevin Horan wrote: >> 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:"<> // print out search result, either 0 >> >> // or an address >> return R_NilValue; >> } >> >> >> compile: >> g++ -I/usr/include/openbabel-2.0 -I/usr/share/R/include -fpic -c >> obtest2.cc -o obtest2.o >> g++ -o obtest2 obtest2.o -fpic -lopenbabel >> -lR # Executable >> g++ -shared -o libobtest2.so obtest2.o -fpic -lopenbabel -lR # R >> library >> >> Run executable: >> $ ./obtest2 >> format: 0x7f1858275d20 #found some result, this is what I expect >> >> Run in R: >> R>dyn.load("libobtest2.so") >> R>.Call("test") >> format: 0 # the format was not found, so 0 was >> returned >> NULL >> >> Kevin >> >> >> -- >> Everyone hates slow websites. So do we. >> Make your web apps faster with AppDynamics >> Download AppDynamics Lite for free today: >> http://p.sf.net/sfu/appdyn_d2d_mar >> ___ >> OpenBabel-Devel mailing list >> OpenBabel-Devel@lists.sourceforge.net >> https://lists.sourceforge.net/lists/listinfo/openbabel-devel -- Own the Future-IntelĀ® Level Up Game Demo Contest 2013 Rise to greatness in Intel's independent game demo contest. Compete for recognition, cash, and the chance to get your game on Steam. $5K grand prize plus 10 genre and skill prizes. Submit your demo
Re: [OpenBabel-Devel] using openbabel in R
To get things to work in Python, I have to set some dlopen flags as follows: sys.setdlopenflags(sys.getdlopenflags() | dl.RTLD_GLOBAL) The following discussion hints at something similar for R: http://www.mail-archive.com/r-help@r-project.org/msg143108.html. - Noel On 25 March 2013 20:30, Kevin Horan wrote: > Thanks, but swig did not help. > > > 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 #this is the correct result > 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. > > Kevin > > On 03/21/2013 05:51 AM, Noel O'Boyle wrote: >> This isn't exactly answering your question, but have you considered >> creating the R Swig wrapper. This may just simply work out of the box >> (or indeed not!) with little effort. >> >> - Noel >> >> On 20 March 2013 16:13, Kevin Horan wrote: >>> 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:"<>> // print out search result, either 0 >>> >>> // or an address >>> return R_NilValue; >>> } >>> >>> >>> compile: >>> g++ -I/usr/include/openbabel-2.0 -I/usr/share/R/include -fpic -c >>> obtest2.cc -o obtest2.o >>> g++ -o obtest2 obtest2.o -fpic -lopenbabel >>> -lR # Executable >>> g++ -shared -o libobtest2.so obtest2.o -fpic -lopenbabel -lR # R >>> library >>> >>> Run executable: >>> $ ./obtest2 >>> format: 0x7f1858275d20 #found some result, this is what I expect >>> >>> Run in R: >>> R>dyn.load("libobtest2.so") >>> R>.Call("test") >>> format: 0 # the format was not found, so 0 was >>> returned >>> NULL >>> >>> Kevin >>> >>> >>> -- >>> Everyone hates slow websites. So do we. >>> Make your web apps faster with AppDynamics >>> Download AppDynamics Lite for free today: >>> http://p.sf.net/sfu/appdyn_d2d_mar >>> ___ >>> OpenBabel-Devel mailing l
Re: [OpenBabel-Devel] using openbabel in R
After some more testing I have found that it actually does work if I compile without the plugin library but load it with dyn.load. I'm not sure why this wasn't working before. It only works though if the plugin library is loaded before libobtest2.so (the open babel main lib basically). So, to clarify, the following works now: g++ -shared -o libobtest2.so obtest2.o -fpic -lopenbabel -lR R>dyn.load("/usr/lib/openbabel/2.2.3/mdlformat.so") R>dyn.load("libobtest2.so") R>.Call("test") format: 0x7fe114c96d20 #this is the correct result NULL But now I have a chicken and egg problem. The plugin libraries are not stored in a standard directory, but open babel provides a function to list their paths. So I need to load the open babel library to fetch the plugin paths, then I can load the plugins, but, oops, too late, the open babel library is already loaded so loading the plugins now doesn't work. I tried using dyn.unload("libobtest2.so") but it didn't work. It seems like I'd have to compile a small executable program that uses openbabel to fetch the plugin paths, then run it as an external program from within R, then load the plugins, then load the open babel lib. Does it make any sense that the order in which these are loaded affects things? Is there a way to load the plugin lib later and still have it work? If the order does have to be maintained, any better ideas how to accomplish this? Thanks. Also, here is the dlopen command that openbabel uses: dlopen(lib_name.c_str(), RTLD_LAZY | RTLD_GLOBAL) Kevin On 03/26/2013 04:33 AM, Noel O'Boyle wrote: > To get things to work in Python, I have to set some dlopen flags as follows: > > sys.setdlopenflags(sys.getdlopenflags() | dl.RTLD_GLOBAL) > > The following discussion hints at something similar for R: > http://www.mail-archive.com/r-help@r-project.org/msg143108.html. > > - Noel > > On 25 March 2013 20:30, Kevin Horan wrote: >> Thanks, but swig did not help. >> >> >> 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 #this is the correct result >> 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. >> >> Kevin >> >> On 03/21/2013 05:51 AM, Noel O'Boyle wrote: >>> This isn't exactly answering your question, but have you considered >>> creating the R Swig wrapper. This may just simply work out of the box >>> (or indeed not!) with little effort. >>> >>> - Noel >>> >>> On 20 March 2013 16:13, Kevin Horan >>> wrote: 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 mak