In [1]:
using RCall
In [2]:
reval("pdf('/tmp/aa.pdf')"); # opens new device
reval("plot(1:10)"); # create plot
reval("dev.off()"); # close device (writes to file)
isfile("/tmp/aa.pdf")
Out[2]:
If doing this from within IJulia (see below), you should create and close the device within the one cell.
In [3]:
rprint("plot(1:10)")
The NULL here is the return value of the plot function. As in R, the graphics are side effects, and not directly representable by values.
The implementation is fairly simple:
png(...) or svg(...)).dev.off()), and displayed.The graphics device is controlled by the RCall.ijulia_setdevice function:
MIME object. Currently supported are:MIME("image/png") [default]MIME("image/svg+xml")
In [4]:
RCall.ijulia_setdevice(MIME("image/svg+xml")) # use SVG device
In [5]:
rprint("library(ggplot2)")
In [6]:
rprint("ggplot(mtcars, aes(wt, mpg)) + geom_point()")
In [7]:
RCall.ijulia_setdevice(MIME("image/png"),width=480,height=400) # use PNG device, options passed as keywords
In [8]:
X = linspace(0,pi,10)
rprint(rcall(:plot,X,sin(X)))
The crazy axis labels are due to R's non-standard evaluation. Unfortunately, Julia is very much standard in its evaluation, so you have to specify your own axis labels:
In [9]:
rprint(rcall(:plot,X,sin(X),xlab="x",ylab="sin(x)"))
In a non-IJulia interactive session, by R will by default open a new window to display the plot. In order to enable interactive features, such as plot-resizing, you should start the R event loop via
RCall.rgui_start()
This runs frequent calls to R to check if the plot has changed, and redraw if necessary. It can be stopped with
RCall.rgui_stop()