tag:blogger.com,1999:blog-3473399517246044360.post7900833555453223951..comments2019-04-26T12:54:05.903+02:00Comments on Harald Schilly: Mathematica 9's R integration vs. Rpy2Unknownnoreply@blogger.comBlogger4125tag:blogger.com,1999:blog-3473399517246044360.post-79806730961088688742012-12-05T20:53:51.460+01:002012-12-05T20:53:51.460+01:00[I was pointed here by one of your readers - may b...[I was pointed here by one of your readers - may be there are points I can clarify]<br /><br />The link to the rpy2 documentation points to the 2.1.x series. 2.3.0 is ready for release (been beta for some time - seem to work well for most people). Try instead:<br />http://rpy.sourceforge.net/rpy2/doc-2.3/html/rinterface.html#pass-by-value-paradigm<br /><br />Rpy2 is using an embedded R, and is able to expose the data in R to Python without copies. As a result accessing R data through rpy2 can be faster than accessing them through R itself. If higher-level protocols are required (say Thrift or else), chances are that there is already a Python package that implements it.<br /><br />The bottom of the dendrogram is looking bad because the R function will try to deparse the variables in the function and print it. Here this is an anonymous variable from the R side, so the string ends up as the full structure of the object. A workaround is to bind the variable to an R symbol and use that in the call. I have thought of ways to make it automagic for the Python user but did not find the time to implement it.<br /><br />Finally, creating list in rpy2 does not have to be that complicated. Try:<br /><br />import rpy2.robjects as ro<br />l = ro.ListVector({'a': 1, 'b': 2}<br />Laurent Gautierhttps://www.blogger.com/profile/18125414407474803939noreply@blogger.comtag:blogger.com,1999:blog-3473399517246044360.post-30308492737925611872012-12-04T21:51:30.555+01:002012-12-04T21:51:30.555+01:00Well, I was searching the web for some reaction on...Well, I was searching the web for some reaction on Mathematica - R integration,and found your nice post (which I was lucky to have found since it reminded me about RPy and that it's a good idea to study it) :)<br /><br />As for the slot assignments: actually RLink currently does not provide the low-level interface needed to make slot assigments. It may be a good idea to add that in the future, since this is obviously a limitation of how low one can go. OTOH, it is not clear to me whether this won't be an overkill given that Mathematica is extremely high-level. It may happen that there won't be many productive uses of such lower-level API, either because it will not be possible to make it fast enough on the Mathematica side, or for some other reasons.<br /><br />Right now, RLink is built on top of JLink, which is Mathematica interface to Java. JLink is very cool, but there is a very significant overhead in method calls. If a large amount of data is transferred at once (so that the number of method calls is minimized), this is less of an issue. But in all other cases, performance becomes a major factor. I don't yet know how RPy interacts with R, but if it loads dynamic R libraries directly into its process (which would be a logical thing to expect), then it can be way more efficient in data transfer and function calls. And Mathematica can not do the same, just because of the licensing issue - R is GPL (technologically, it should be possible, since Mathematica since version 8 has the LibraryLink interface to load dynamic libraries directly into the kernel process).Leonidhttps://www.blogger.com/profile/12524296169845031502noreply@blogger.comtag:blogger.com,1999:blog-3473399517246044360.post-91861254889096331912012-12-02T13:21:28.990+01:002012-12-02T13:21:28.990+01:00Hi, wow, I didn't expect to get a feedback dir...Hi, wow, I didn't expect to get a feedback directly from "the source"! :)<br /><br />I'm aware that MMA has a different model for it's language, which has its own strengths and weaknesses. R is just a bit closer to Python, that's why it seems more natural to me.<br /><br />About the mutability, does this refer to the slot assignment done in the block of strings? I've updated my posting slightly with a way how this is done via Python. Basically, a special assignment function just does what happens inside the string. Also, R's "list" can be referred directly and "paste" replaced by Python's list comprehensions ... but one has to map this list of strings to a proper R expression, though.<br /><br />http://rpy.sourceforge.net/rpy2/doc-2.1/html/rinterface.html#pass-by-value-paradigm<br /><br />(I haven't studied rpy2 very deeply, maybe this "low-level" approach is somewhere wrapped into something more "high-level", i don't know)Harald Schillyhttps://www.blogger.com/profile/13103857943334302170noreply@blogger.comtag:blogger.com,1999:blog-3473399517246044360.post-23459522450366464852012-12-01T23:41:15.568+01:002012-12-01T23:41:15.568+01:00Thanks, very interesting post, a definite food for...Thanks, very interesting post, a definite food for thought. RPy seems to have achieved a remarkable level of mapping R language constructs on Python language constructs. Mathematica does not directly support OO, and is based on immutable expressions, which are some reasons why it is perhaps not so easy to achieve that in Mathematica. I also agree that string escaping is a mess. <br /><br />The behavior of RSet which injects the variable into R'w workspace and does not bind it directly to some variable in Mathematica workspace is quite intentional. R objects mix state and immutability in rather interesting way IMO, and we picked the immutability part, to have transparent mapping between R expressions and Mathematica expressions (since Mathematica is expression-based and expressions are immutable). This approach is idiomatic for Mathematica, and has both strengths and weaknesses. <br /><br />One serious weakness (at the moment) is that we can not yet write RLink code mostly as Mathematica code (similar to how you sript R in Python in your post) and have to resort to strings of R code, but in Mathematica it is fairly easy to create DSLs(due to its symbolic nature), so I think that this could be improved in the future. OTOH, it is fairly easy / automatic to translate complex (nested) Mathematoca expressions to the corresponding R objects, and it is done transparently to the user - which can be beneicial in some cases. <br /><br />The other thing is that RLink currently only provides a fairly basic support for the core R language constructs, which has not been extended to the R library. This will probably change in the future.<br /><br />In any case, I will be definitely studying RPy, it seems like it has many lessons to teach. Disclosure: I am the developer of RLink. Leonidhttps://www.blogger.com/profile/12524296169845031502noreply@blogger.com