RCall example

140 views
Skip to first unread message

David Anthoff

unread,
Apr 27, 2016, 6:11:35 PM4/27/16
to julia...@googlegroups.com

Hi,

 

does someone have an example on how to use RCall.jl? I’d like to take a julia DataFrame, move that data into an R session and then call ggplot2 on it. But right now the package has no tutorial, or example or any hint for a beginner on how to use it :) Any example of how to use that package works would be appreciated.

 

Thanks,

David

 

--

David Anthoff

University of California, Berkeley

 

http://www.david-anthoff.com

 

Stefan Karpinski

unread,
Apr 27, 2016, 6:20:34 PM4/27/16
to julia-stats
I've found that the tests are the best examples at the moment (this is often the case):


--
You received this message because you are subscribed to the Google Groups "julia-stats" group.
To unsubscribe from this group and stop receiving emails from it, send an email to julia-stats...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andreas Noack

unread,
Apr 27, 2016, 7:11:03 PM4/27/16
to julia...@googlegroups.com
Also, take a look here http://juliastats.github.io/RCall.jl/latest/lib/public/#methods

I was able to make a plot with

julia> x = randn(100);

julia> df = DataFrame(x = x, y = [x[1:50]; 2x[51:100]] + 0.1randn(100), tp = [zeros(Int, 50); ones(Int, 50)]);

julia> @rput df;

julia> @rlibrary ggplot2

julia> reval(rparse("qplot(x, y, data = df, colour = factor(tp))"))
RCall.RObject{RCall.VecSxp}

Simon Byrne

unread,
Apr 28, 2016, 6:43:11 AM4/28/16
to julia-stats
Sorry the documentation isn't great, part of the problem here is that we've been experimenting a lot with different approaches.

My recommendation would be to use the R"" string macro:

julia> R"library(ggplot2)";


julia> R"qplot(x, y, data = $df, colour = factor(tp))"


-simon

Douglas Bates

unread,
Apr 28, 2016, 12:48:11 PM4/28/16
to julia-stats
On Thursday, April 28, 2016 at 5:43:11 AM UTC-5, Simon Byrne wrote:
Sorry the documentation isn't great, part of the problem here is that we've been experimenting a lot with different approaches.

My recommendation would be to use the R"" string macro:

julia> R"library(ggplot2)";


julia> R"qplot(x, y, data = $df, colour = factor(tp))"


Exactly.  The simplest instructions for using RCall are to write R code in Julia but surround it with

R"""
  # write R code here
"""

As Simon shows, if you want to refer to a Julia object, well at least a Julia object that can be mapped to an R object, just use $<Julia object name>.  Simon did some wonderful work to ensure that the references to Julia objects are not confused with R's use of the $ symbol.

David Anthoff

unread,
Apr 28, 2016, 1:37:56 PM4/28/16
to julia...@googlegroups.com

This is absolutely fantastic, what a hidden gem!

 

This might also be the best way to produce high quality graphs from julia at the moment, right?

 

I feel this whole approach should be made much more well known, I think many more people would be willing to use julia for their day-to-day work if they knew that they could easily use e.g. ggplot2 for plotting, and things like dplyr for data transformation etc.

Michael Krabbe Borregaard

unread,
Apr 28, 2016, 1:53:36 PM4/28/16
to julia...@googlegroups.com
There are many ways to produce high-quality graphs from Julia at the moment. Check out for example PlotlyJS.jl , which offers offline plotly capability in Julia (i.e. not using the online plotly service).

Michael Krabbe Borregaard

unread,
Apr 28, 2016, 1:54:32 PM4/28/16
to julia...@googlegroups.com
If I may ask, purely out of curiosity, which functionality from dplyr are you missing from DataFramesMeta?

David Anthoff

unread,
Apr 28, 2016, 6:22:54 PM4/28/16
to julia...@googlegroups.com

Yeah, I really like the work on PlotlyJS.jl.

 

Having said that, at this point I see a whole bunch of really good things about using ggplot2 via RCall:

- There are a gazillion examples, tutorials etc. out there on the web on how to create any type of graph imaginable. Makes it much more approachable for many users.

- It has a grammar of graphics API. Many people really like that. The only other julia package that seems to support that is Gadfly (but that seems to have vastly less features than ggplot2).

- It is stable. They have released a 1.0, so most likely my plotting code will still work in half a year. Many of the julia plotting packages right now seem under pretty heavy dev.

- Generally transferability of skills. There are probably a couple hundred thousand people out there that know how to use ggplot2, if they can all bring that knowledge across to julia with so little effort, it seems a major positive that we as the julia community might to highlight.

 

I’m not at all saying that I don’t like the more homegrown plotting approaches in julia, but at this point ggplot2 seems just way, way more mature, and at least I was really surprised today how super simple it is to use it from within julia.

 

Cheers,

David

David Anthoff

unread,
Apr 28, 2016, 6:24:47 PM4/28/16
to julia...@googlegroups.com

I’m actually not that familiar with dplyr, so I can’t tell. But there are just a lot of people who have built up a lot of skill in using dplyr, and this makes it easy for them to continue to use those skills.

colint...@gmail.com

unread,
Apr 28, 2016, 7:39:38 PM4/28/16
to julia-stats
Not sure how much help it will be, but I wrote a Julia package that wraps the ARIMA functionality in R, and it uses RCall fairly heavily. I haven't added it to metadata, but it sounds like you only really want have a look at the source anyway to get some example code. The package source code is here:

https://github.com/colintbowers/RARIMA.jl/blob/master/src/RARIMA.jl

Cheers,

Colin

David Anthoff

unread,
Apr 28, 2016, 7:45:47 PM4/28/16
to julia...@googlegroups.com

Thanks, that is also helpful!

 

Simon actually wrote a really short Getting Started in the meantime, that essentially covers everything I had wanted to know:

 

http://juliastats.github.io/RCall.jl/latest/gettingstarted/

 

 

--

Tom Breloff

unread,
Apr 28, 2016, 10:24:52 PM4/28/16
to julia...@googlegroups.com
Hi David.  I agree that there's a lot of development happening in the julia graphics world, but I would argue that it's pretty usable for day-to-day work.  A discussion about ggplot2 just happened in the Plots issues (https://github.com/tbreloff/Plots.jl/issues/201#issuecomment-214381703) that I think you might find interesting.  If you could post some visualization examples that you prefer in ggplot2 (with the input data), we might be able to show you an easier solution.

Best,
Tom

David Anthoff

unread,
Apr 29, 2016, 2:30:30 PM4/29/16
to julia...@googlegroups.com

This looks nice!

 

Having said that, I really like a grammar of graphics API, so having ggplot2 available now is just great (and as far as I know the only other grammar of graphic like package accessible from julia is Gadfly, right?)

 

From: julia...@googlegroups.com [mailto:julia...@googlegroups.com] On Behalf Of Tom Breloff


Sent: Thursday, April 28, 2016 7:25 PM
To: julia...@googlegroups.com

Tom Breloff

unread,
Apr 29, 2016, 3:06:21 PM4/29/16
to julia...@googlegroups.com
I'm really curious... what is it about a "grammar of graphics API" that you really like?  I'm sure it would be feasible to layer a grammar on top of Plots calls (maybe it belongs in a separate package though?)  For an example (taken from http://r4stats.com/examples/graphics-ggplot2/):

ggplot(mydata100,
  aes(x = factor(""), fill = workshop) ) +
  geom_bar() +
  coord_polar(theta = "y") +
  scale_x_discrete("")

I could see implementing julia versions of ggplot, aes, geom_bar, etc all just populating a type like:

type PlotDescription
  args::Vector{Any}
  kw::KW
end

and then overloading Base.+ to vcat the args and merge the kw.  Then you could just splat the result into Plots when a display is requested, and you're done.

The benefit to doing something like this (rather than just going straight to ggplot2) is that you'd be able to use this "grammar" for many different scenarios... real-time updating using GR or GLVisualize, or server-side REPL plotting with UnicodePlots, etc.  (not to mention we could start weaning people away from R... it's one less reason to use it)

The obvious downside is that there might be things that are possible in ggplot2 but not in Plots.  If so, please open an issue!  :)

Let me know what you think about all this... I might be crazy but implementing this seems like it could be pretty straightforward if people really want it.

Best,
Tom

David Anthoff

unread,
Apr 29, 2016, 3:40:44 PM4/29/16
to julia...@googlegroups.com

I very much doubt that you can create a proper grammar of graphics like API by just vcating args and kws like you describe. I once read (not carefully) the original book by Wilkinson, and properly implementing that seems like a major effort to me. This is *not* just a light API layer on top of some other API. I’m sure one could create such a gg API that then calls into Plots, but I think it would be a lot of work and would require an intimate familiarity with the whole gg philosophy to get the design right. Wickham’s writing on this would probably be also really helpful.

 

What I like about a gg API is pretty well described in either Wilkinson’s book, or for example Wickham’s ggplot2 book intro chapter.

 

I see the benefits one can get from Plots that you describe, but those are all things that I don’t need in my kind of work. Having a package that is super well documented, where there are a gazillion tutorials, examples etc. available and where I can be pretty sure that any type of graph that I might ever want to create can probably be done by far outweighs the downside of having to mix julia and R. My sense is that there are a lot of academic people out there that would be in a similar situation.

Tom Breloff

unread,
Apr 29, 2016, 3:58:55 PM4/29/16
to julia...@googlegroups.com
Ok if I get around to it maybe I'll throw together a proof of concept.  Generally I agree that this sort of thing would be a massive project, but the framework of Plots makes this a reasonably tractable problem to me.  I found this cheat sheet... is this a good aggregation of functionality that you find important, or is there more?

David Anthoff

unread,
Apr 29, 2016, 4:05:03 PM4/29/16
to julia...@googlegroups.com

Well, one of the nice things with ggplot2 is that even though I don’t use even a small fraction of it, I can be reasonably sure that anything I might want to use in the future is already there ;) So yes, that cheat sheet summarizes what I’ve been using. I would recommend to read the Wilkinson book before you start with this, the key point about a gg API is not a list like this cheat sheet, but rather the underlying consistent grammar, and I don’t think one can grasp that without reading the book…

Tom Breloff

unread,
Apr 29, 2016, 4:31:59 PM4/29/16
to julia...@googlegroups.com
Ok you're probably referring to his 2005 book but I think this one is probably more relevant? http://byrneslab.net/classes/biol607/readings/wickham_layered-grammar.pdf

I've read it, and I also have extensive experience decomposing Gadfly.  Like I said, someday I'll try to throw together a proof of concept so you can see what I mean.  Actions speak louder than words, so... to be continued.

David Anthoff

unread,
Apr 29, 2016, 4:33:39 PM4/29/16
to julia...@googlegroups.com

Michael Krabbe Borregaard

unread,
Apr 29, 2016, 4:45:08 PM4/29/16
to julia...@googlegroups.com
I just want to give a +1 to Tom's idea of implementing a gog api on top of Plots. Especially useful for me who sometimes find gog useful but often not. Also I have hoped for a Theme() object argument for a long time - there could even be a big public repo of themes to download and exchange among users.

Tom Breloff

unread,
Apr 29, 2016, 4:53:45 PM4/29/16
to julia...@googlegroups.com
BTW... If you want a small taste of what's currently possible, here's a notebook that I've been using to track regressions during a pyplot backend refactor: https://github.com/tbreloff/ExamplePlots.jl/blob/master/notebooks/scratch/pyplot_refactor_tests.ipynb
Reply all
Reply to author
Forward
0 new messages