Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

There is no need to change any r.py code, as kcrisman suggested in his answer. The R code always runs, but when the code is longer than 1024 characters R code is run as a script file instead of as a command line, and then Sage fails to detect any output that is generated.

The workaround that I found is to save output from long code snippets into a known file, e.g. "~/temp.png" for plots. Then you can use the next cell to draw a dummy plot, and then write your previously created plot onto the canvas. Since this plot is generated from a shorter code (less than 1024 characters), Sage will be able to detect the output that is generated the second time around. Here is the code I used:

png("~/output.png", width=1800, height=1000)
par(mfrow=c(2,2))
# do fancy plot stuff here
dev.off()
# <---- I used a second cell, but can probably be done in same cell ---->
library(png)
x <- readPNG("~/output.png")
png("tmp.png", width=1800, height=1000)
plot(1:2, type="n", axes=F, xlab="", ylab="")
# rasterImage() prints on the plot. The plot goes from x = c(1,2) to y = c(1,2).
# So we tell rasterImage() to print within that x/y (i.e. "1,1,2,2") to keep
# the plot scaled correctly.
rasterImage(x, 1, 1, 2, 2)
dev.off()

You could of course do it like a function or something. But since it only applies on rare occasions when plot arguments are so long that the code length exceeds 1024 characters, I have only done it as a special cell on its own.

There is no need to change any r.py code, as kcrisman suggested in his answer. The R code always runs, but when the code is longer than 1024 characters R code is run as a script file instead of as a command line, and then Sage fails to detect any output that is generated.

The workaround that I found is to save output from long code snippets into a known file, e.g. "~/temp.png" for plots. Then you can use the next cell to draw a dummy plot, and then write your previously created plot onto the canvas. Since this plot is generated from a shorter code (less than 1024 characters), Sage will be able to detect the output that is generated the second time around. Here is the code I used:

png("~/output.png", width=1800, height=1000)
par(mfrow=c(2,2))
# do fancy plot stuff here
dev.off()
# <---- I used a second cell, but can probably be done in same cell from here ---->
library(png)
x <- readPNG("~/output.png")
png("tmp.png", width=1800, height=1000)
plot(1:2, type="n", axes=F, xlab="", ylab="")
# rasterImage() prints on the plot. The plot goes from x = c(1,2) to y = c(1,2).
# So we tell rasterImage() to print within that x/y (i.e. "1,1,2,2") to keep
# the plot scaled correctly.
rasterImage(x, 1, 1, 2, 2)
dev.off()

You could of course do it like a function or something. But since it only applies on rare occasions when plot arguments are so long that the code length exceeds 1024 characters, I have only done it as a special cell on its own.