Re: [Rd] Makefile for embedding OpenBUGS in R package

From: Tobias Verbeke <tobias.verbeke_at_telenet.be>
Date: Mon, 06 Aug 2007 21:15:17 +0200

Hi Uwe,

> As the BRugs maintainer, as far as I can tell, the most recent OpenBUGS 
> brugs.so is not compatible with (at least) my system and I do not have 
> the compilers to try it myself. Hence we still only ship BRugs for 
> Windows, but if anybody else has ideas how to get the BRugs.so compiled 
> (or just running without "traps", I'd be happy to hear about it.

My strategy is to produce the data, model and inits file in a BRugs manner and then produce a script along the lines of

modelCheck('Ratsmodel.txt')
modelData('Ratsdata.txt')
modelCompile()
modelInits('Ratsinits.txt')
modelUpdate(1000)
statsSet('alpha')
statsSet('beta')
modelUpdate(2000)
samplesStats('*')
modelQuit()

which is fed to bugs (compiled as described below) like

./bugs < rats.script > rats.out

Afterwards I use some simple R utilities to clean up rats.out and read it in as
as dataframe for further processing.

There are however two (painfully learned and undocumented) things to keep in mind when producing these files:

(1) such a script (as well as all other BUGS files) should use CR LF line endings

(2) There may be absolutely no character (no newline!) after the final modelQuit() [otherwise you can end up (like me) with 47 GB of error messages being joyfully output to rats.out... which is when I had a phone call from a local sysasmin]

The only missing piece to embed current OpenBUGS in an R package for automating analyses on Linux is being able to compile CBugs.c (see below) when building the R package.

I hope this is useful to someone.

Kind regards,
Tobias

> Tobias Verbeke wrote:

>>> ----- Oorspronkelijk bericht -----
>>> Van: Prof Brian Ripley [mailto:ripley_at_stats.ox.ac.uk]
>>> Verzonden: maandag, augustus 6, 2007 03:46 PM
>>> Aan: 'Tobias Verbeke'
>>> CC: r-devel_at_r-project.org
>>> Onderwerp: Re: [Rd] Makefile for embedding OpenBUGS in R package
>>>
>>> On Mon, 6 Aug 2007, Tobias Verbeke wrote:
>>>
>>>> Dear list,
>>>>
>>>> I'm trying to embed OpenBUGS in an R package for use of it
>>>> on 64-bit Linux. In order to get the CLI working one has to
>>>> compile C code contained in $OpenBUGS/Manuals/CBugs.html
>>> same as
>>>
>>> http://mathstat.helsinki.fi/openbugs/data/Docu/CBugs.html
>>>
>>> I presume.
>>
>> Actually, these files appear to differ. The file I referred to
>> was the file ./Manuals/CBugs.html contained in the current OpenBUGS 
>> release:
>>
>> http://mathstat.helsinki.fi/openbugs/OpenBUGS.zip
>>

>>>> (copied to say CBugs.c) using
>>>>
>>>> gcc -m32 -o bugs CBugs.c -ldl
>>>>
>>>> I put the OpenBUGS distribution in the ./inst subdirectory of
>>>> the package root. Where should I now put the CBugs.c and how
>>> Why do you want to install CBugs.c?
>>
>> The CBugs.c file itself is indeed not needed in the built package.

>>>> should the Makefile look like in order to be able to call
>>>> $PKG_ROOT/OpenBUGS/bugs afterwards ?
>>>>
>>>> Naively putting the following Makefile in ./src does not work
>>> What does 'does not work' mean? It's hard to know whether this is
>>> just not doing what you wanted, or something else is wrong.
>>
>> Apologies. This is the error message when putting the Makefile
>> in ./src and launching the package checker:
>>
>> $ R.250 CMD check CGHmix
>> DISPLAY=localhost:10.0
>> * checking for working latex ... OK
>> * using log directory '/home/tverbek1/pkg/CGHmix.Rcheck'
>> * using R version 2.5.0 (2007-04-23)
>> * checking for file 'CGHmix/DESCRIPTION' ... OK
>> * checking extension type ... Package
>> * this is package 'CGHmix' version '0.1-2'
>> * checking package dependencies ... OK
>> * checking if this is a source package ... OK
>> * checking whether package 'CGHmix' can be installed ... ERROR
>> Installation failed.
>> See '/home/tverbek1/pkg/CGHmix.Rcheck/00install.out' for details.
>>
>> The file 00install.out contains:
>>
>> * Installing *source* package 'CGHmix' ...
>> ** libs
>> ** arch -
>> gcc -m32 -o ../inst/OpenBUGS/bugs ../inst/OpenBUGS/CBugs.c -ldl
>> cp: cannot stat `*.so': No such file or directory
>> ERROR: compilation failed for package 'CGHmix'
>> ** Removing '/home/tverbek1/pkg/CGHmix.Rcheck/CGHmix'
>>

>>>> -%--------
>>>> bugs: ../inst/OpenBUGS/CBugs.c
>>>> gcc -m32 -o bugs ../inst/OpenBUGS/CBugs.c -ldl
>>>> -%-------
>>>>
>>>> The objective is to use something along the following
>>>>
>>>> execfile <- system.file("OpenBUGS", "bugs", package = mypkg)
>>>> system(paste(execfile, "< somescript.script > somefile.out"))
>>>>
>>>> This system call to the CLI is currently the only (non-WINE)
>>>> way of using OpenBUGS on Linux in batch mode.
>>> I think you need to make ../inst/OpenBUGS/bugs, not src/bugs. So
>>> something like
>>>
>>> all: ../inst/OpenBUGS/bugs
>>>
>>> ../inst/OpenBUGS/bugs: ../inst/OpenBUGS/CBugs.c
>>> gcc -m32 -o ../inst/OpenBUGS/bugs ../inst/OpenBUGS/CBugs.c -ldl
>>
>> Thank you.

>>> However, -m32 builds a 32-bit executable on 64-bit linux. Is that
>>> what you wanted?
>>
>> Yes. The shared object (brugs.so) comes with the OpenBUGS distribution 
>> and currently can only be cross-compiled by the main OpenBUGS 
>> developer (Andrew Thomas) who knows all secrets of the Windows only 
>> BlackBox (nomen omen) Component Pascal compiler.
>> $ file brugs.so
>> brugs.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), 
>> stripped
>>
>> Many thanks for your ever instructive answer.
>>
>> Tobias
>>
>> P.S. Contents of CBugs.c from OpenBUGS distribution (3.0.1)
>>
>> /* GNU General Public Licence
>>       This small C program loads the brugs.so ELF shared library and 
>> calls the CLI function.
>>    Save it as a .c file and then compile it on Linux using gcc -o bugs 
>> CBugs.c -ldl
>>
>> */
>>
>> #include <dlfcn.h>
>> #include <stdio.h>
>> #include <string.h>
>>
>> int main (int argc, char **argv)
>> {
>>   void * handle;
>>   void (*cli)(void);
>>
>>   handle = dlopen("./brugs.so", RTLD_LAZY);
>>   if (!handle)
>>     return 1;
>>
>>   * (void **) (&cli) = dlsym(handle, "CLI");
>>   (*cli)();
>>   dlclose(handle);
>>
>>   return 0;
>>    }
>>
>>
>>

>>>
>>> --
>>> Brian D. Ripley, ripley@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, UK Fax: +44 1865 272595
>>>
>>>
>>
>> ______________________________________________
>> R-devel_at_r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
> 
>

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Mon 06 Aug 2007 - 20:54:34 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 06 Aug 2007 - 22:38:38 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.