My apologies if this is a FAQ, I searched the mailing list archives before posting. Background: I am a long time user of SPlus, and a recent user of R. My work normally involves converting the raw output of something interesting into data to be analyzed in S/R, for which I use Perl extensively. I then import the data into S/R, perform the analyses I need, dumping the values into a new file, further Perl processing, eventual database storage. I've recently become acquainted with a new Perl module called "Inline" which allows you to embed C/C++/Python/etc code within your Perl program and use those functions as part of your regular perl program. Briefly, Inline is taking care of all the code creation, compiling and dynamic loading necessary to make this work "transparently". I am hoping to write a similar "filter" for R, and so have just begun to delve into the R api (remember, I've only been a command-line/batch script user previously). I see that I can write extensions in C/Fortran, which can be dynamically loaded into R, and that the extension code can access R functions and data. But that's all still running under R. What about a program that dynamically loads R itself (trivial example follows): #include <stdio.h> #include <R.h> #define MAXNUM = 100; int main(int argc, char **argv) { int n; int i[MAXNUM]; for(n = 1 ; n < argc && n <= MAXNUM ; n++) { i[n-1] = atoi(argv[n]); } printf("mean: %4.2f\n", R_mean(i, n - 1)); exit(0); } % gcc -lR mean.c -o mean % mean 4 5 6 7 mean: 5.5 % exit Where "R_mean" is calling the R function mean() ??? Any help is appreciated. And I'm not going to worry about graphical functions just yet. Just need a pointer or two into the right piece of documentation. -Aaron -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Many have pointed me to www.omegahat.org, which provides the RSPerl interface. This looks great, but isn't exactly what I was looking for (although it could be argued that perhaps I should change my mind about what I'm looking for). I do not wish to embed Perl within R, but the opposite. I'd like to have an R interpreter available to my Perl interpreter, i.e. embed R in Perl. But thank you all for flooding my inbox with valuable responses, I will indeed investigate RSPerl further. I will also investigate http://developer.r-project.org/embedded.html further ... -Aaron -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Aaron J Mackey <ajm6q at virginia.edu> writes:> int main(int argc, char **argv) { > > int n; > int i[MAXNUM]; > > for(n = 1 ; n < argc && n <= MAXNUM ; n++) { > i[n-1] = atoi(argv[n]); > } > > printf("mean: %4.2f\n", R_mean(i, n - 1)); > exit(0); > } > > % gcc -lR mean.c -o mean > % mean 4 5 6 7 > mean: 5.5 > % exit > > Where "R_mean" is calling the R function mean() ??? > > Any help is appreciated. And I'm not going to worry about graphical > functions just yet. Just need a pointer or two into the right piece of > documentation.This easily gets involved, but: You can link R as a dynamic library. You'll need to convert C structures to and from R internal storage form. Some of the lower-level functions work on C structures directly. There's also the possibility of building the standalone mathlib. Much of R sits at the interpreted level, so you'd probably want to be able to call the parser/evaluator rather than R's internal C code. There's a rudimentary example of such a thing in the R_eval function in tcltk.c. (This assumes that the variables you're dealing with are already known to R, so you'd have to find a way of making that so...). -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> Background: I am a long time user of SPlus, and a recent user of R. My > work normally involves converting the raw output of something interesting > into data to be analyzed in S/R, for which I use Perl extensively. I then > import the data into S/R, perform the analyses I need, dumping the values > into a new file, further Perl processing, eventual database storage.I have a similar modus operandi: I collect data with Perl, usually resulting in either comma-separated value (.csv) or tab-separated value (.tab, .dat, .txt or .tsv) format. Then, for *manual* analysis, I import the data into either Excel, Minitab, or R directly, or, for larger data sets, import the data into Microsoft Access and process in Excel or Minitab using ODBC queries. In the near future I'll add "R-ODBC" to this collection of tools. My outputs tend to be pictures rather than large amounts of processed data, however, so mostly I'm interested in graphs that I can import into a Word document which is the final report. I've made some attempts at automating all of this, but there is a UNIX/Windows "seam" involved. It would be a big help in some cases if UNIX "R" could act as an ODBC *server*; I can't use the obvious choice, "mySQL", because of license restrictions. The other thing I've done fairly recently in the *automated* analysis is create the "R" code that does the plotting on the fly in the Perl script, then execute the code with a Perl 'system' statement. I'm looking for a way to make the "R" code *constant*, and pass such parameters as input and output file names, variable names and graph labels to the "R" code, rather than having the Perl code write an "R" code file for each graph I want to plot. I think there's a way to do this with environment variables, but I haven't dug into how "R" deals with environment variables yet. More elegant would be the capability of passing parameters to the "R" command line just like you pass parameters to any other UNIX or Windows command line. -- Ed Borasky http://www.aracnet.com/~znmeb mailto:znmeb at aracnet.com Q: What phrase will you never hear Candice Bergen use? A: "My daddy didn't raise no dummies". -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Have a look at www.omegahat.org where you can find, among other things, how to embed Perl into R -- this way you keep you data acquisition 'as is'. There are lots of other goodies on the omegahat page too... Hope this helps, Dirk -- According to the latest figures, 43% of all statistics are totally worthless. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Sun, 11 Feb 2001, Aaron J Mackey wrote:> ... What about a program that > dynamically loads R itself ...Aaron- I've been interested in this topic for a while, too, but haven't done much with it yet. I saw some of the replies, pointing to omegahat, the directions for embedding R. Another shortcut to perl <-> R interactivity is to use the Expect perl module (I think expect was originally in tcl), which is basically a fancy bi-directional pipe. You're still faced with using perl to prepare R input, and parse R output, but you can do so one call at a time. Also, expect just waits until it sees the next prompt or times out (though the time-out can be disabled). I tried to automatically convert R output into perl data structures; this can be messy. I'm interested in web application development that utilizes R. Duncan Temple Lang's RSPerl package, embedding perl in R, lets you access great perl modules, like CGI.pm; a hack to R lets it handle scripts like #!/usr/local/R-1.2.0/lib/R/bin/R.bin --source invisible(library(RSPerl)); foo <- .PerlExpr("use CGI;"); foo <- .PerlExpr("$q = new CGI; 1;"); a <- .PerlExpr("$q->param('a')"); cat(.PerlExpr("$q->header . $q->start_html . 'Hellooooo....';"), a, .PerlExpr("$q->end_html")); Anyway, that's about all I did with it. (OK, maybe that's exactly what I did; proof of concept and no more.) It has R functions for accessing perl data structures, which might serve as a useful template for the reverse process-- having perl functions to access R data structures, for R embedded in perl. I'm a little fuzzy on what the appropriate mapping is for different data types. A random aside-- there's a package for perl, PDL (perl data language), that stores data in C arrays, and has optimized code for computation. This also provides methods for converting perl arrays to and from pdl structures, as well as fast I/O routines for pdl structures. The icing on the perl-with-embedded-R cake would be conversion methods directly between R arrays and pdl arrays. I'm interested if you decide to pursue this further. -John Barnett -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
As Luke once put it "This is a long one; you know what key to press if you aren't interested...." Apologies for coming into the R within Perl discussion late. (I have been away from e-mail for several days.) The RSPerl package (http:://www.omegahat.org/RSPerl) is designed to be symmetric in allowing the embedding in either direction. That is embedding Perl within an R session or running R within a Perl session. Either can be "in charge". The embedding R within Perl is less well documented, to say the least. I plan to release a version of the package soon with more information about this and also more user-level support for specifying converter functions. The very same model that is used in the Java, CORBA, Python, JavaScript, etc. works for Perl also. The Python package (of which there will be a new release within the next day or two) might provide a useful place to see the setup immediately. The approach of using Expect perl works fine. However, using strings can be very limiting for certain types of functionality and places a lot of work on each programmer using it. They have to construct the R command as a string and then send it to be evaluated. Parsing R output is very fragile and difficult to do in a flexible way (what if the page width changes, etc.). It is hard to come up with general and reusable ways to handle reading the results back in and hence one is frequently coding context-specific approaches. Very time consuming. (If one really wanted to do this, one might use XML to represent the R values being returned and then read them into Perl. That is general and extensible. Some of us (Vincent Carey, Robert Gentleman and I currently) are working on this at present.) And perhaps the biggest drawback to using strings to effect the inter-system interface is that one end up programming in both languages and that also means debugging the code for each but not necessarily in the easiest place. Basically, the overall problem is that the syntax of the other language is being used and that breaks encapsulation. Suppose you are using Perl from within R to read the contents of URLs. Then, you may decide that Java or Python is better (say it handles firewalls or SSL). Then you would have to rewrite your R code to generate expressions in Omegahat or Python. The approach we have been using in all the Omegahat inter-system interface packages (the CORBA, Java, ....) is to allow one to call functions and create objects and call their methods in the other language. The functions look as if they are S functions. And so, it fits naturally with the programming model, encapsulating what is really happening. Whatever approach one uses is good if it works for the problem at hand. The important thing is to try to reduce the number of times one has to hand-craft solutions. I hope this helps explain what we are doing and how the S-Perl connection is intended to work. All the best. D.> Date: Tue, 13 Feb 2001 17:28:31 -0500 (EST) > From: "John D. Barnett" <jdb at young39.wi.mit.edu> > cc: r-help at stat.math.ethz.ch >> On Sun, 11 Feb 2001, Aaron J Mackey wrote: > > > ... What about a program that > > dynamically loads R itself ... > > Aaron- > > I've been interested in this topic for a while, too, but haven't done much > with it yet. I saw some of the replies, pointing to omegahat, the > directions for embedding R. > > Another shortcut to perl <-> R interactivity is to use the Expect perl > module (I think expect was originally in tcl), which is basically a fancy > bi-directional pipe. You're still faced with using perl to prepare R > input, and parse R output, but you can do so one call at a time. Also, > expect just waits until it sees the next prompt or times out (though the > time-out can be disabled). I tried to automatically convert R output into > perl data structures; this can be messy. > > I'm interested in web application development that utilizes R. Duncan > Temple Lang's RSPerl package, embedding perl in R, lets you access great > perl modules, like CGI.pm; a hack to R lets it handle scripts like > > #!/usr/local/R-1.2.0/lib/R/bin/R.bin --source > invisible(library(RSPerl)); > foo <- .PerlExpr("use CGI;"); > foo <- .PerlExpr("$q = new CGI; 1;"); > a <- .PerlExpr("$q->param('a')"); > cat(.PerlExpr("$q->header . $q->start_html . 'Hellooooo....';"), > a, > .PerlExpr("$q->end_html")); > > > Anyway, that's about all I did with it. (OK, maybe that's exactly what > I did; proof of concept and no more.) It has R functions for accessing > perl data structures, which might serve as a useful template for the > reverse process-- having perl functions to access R data structures, for R > embedded in perl. I'm a little fuzzy on what the appropriate mapping is > for different data types. > > A random aside-- there's a package for perl, PDL (perl data language), > that stores data in C arrays, and has optimized code for > computation. This also provides methods for converting perl arrays to > and from pdl structures, as well as fast I/O routines for pdl > structures. The icing on the perl-with-embedded-R cake would be > conversion methods directly between R arrays and pdl arrays. > > I'm interested if you decide to pursue this further. > > -John Barnett > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-- _______________________________________________________________ Duncan Temple Lang duncan at research.bell-labs.com Bell Labs, Lucent Technologies office: (908)582-3217 700 Mountain Avenue, Room 2C-259 fax: (908)582-3340 Murray Hill, NJ 07974-2070 http://cm.bell-labs.com/stat/duncan -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._