Ask Your Question
1

Code length limit in cells?

asked 2013-05-21 23:35:47 -0500

Dan-Erik gravatar image

updated 2013-05-21 23:43:26 -0500

I have a worksheet that is set to R code. Then I do a bunch of stuff, and at the end I want to produce a nice looking plot. To make the plot look nice, I am setting lots of options, which makes it become a huge block of text.

It seems there is some sort of limit on the length of code that a cell may contain? If I do this:

plot(...)
#lines(...)
#etc

Then I get no output, no png is produced, nothing at all and no error message either. However, if I do the same, but remove the commented lines (starting with #) then I do get a plot.

For more details, I started out by asking this question on StackExchange also. But I figure this question might belong here instead...

How do I set Sage to accept cells with longer text contents, so I can do my plots with all the options I want?

EDIT (for reproducibility):

It seems the limit is around 1000 characters. The following code can be pasted in a cell, and produces nothing, but if you remove one single character anywhere then you will get a plot output:

png("temp.png")
plot(1:20)
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111111
#111111111111111111111111111
dev.off()
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2013-05-22 18:54:38 -0500

Dan-Erik gravatar image

updated 2013-05-22 19:01:48 -0500

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 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.

edit flag offensive delete link more

Comments

Great solution! That doesn't still mean that on occasion one couldn't also change the r.py code, and we should still have a way to access the generated graphic from an interface. Nice that R has that `readPNG` function!

kcrisman gravatar imagekcrisman ( 2013-05-23 07:36:07 -0500 )edit
1

answered 2013-05-22 02:34:34 -0500

kcrisman gravatar image

updated 2013-05-22 08:12:28 -0500

This is a limitation of the interface with R that we use. See here.

              # If an input is longer than this number of characters, then
              # try to switch to outputting to a file. 
              eval_using_file_cutoff=1024)

You could certainly hack your own local copy of Sage to change this, and this is the preferred option. (You'll have to do ./sage -b before restarting the server.)

If you are using a server you don't have access to, you might be able to use a different hack. From the file sage/interfaces/expect.py:

    By default, a long command (length exceeding ``self._eval_using_file_cutoff``)
    is evaluated using :meth:`_eval_line_using_file`.

    If the optional ``wait_for_prompt`` is ``False`` then even a very
    long line will not be evaluated by :meth:`_eval_line_using_file`,
    since this does not support the ``wait_for_prompt`` option.

You might see some examples of how to use the Sage evaluation option to use the interface with that option. It might have to be a line-by-line option. Or you might be able to define a new interface MyR which is the same as the R interface by doing something like

MyR = R(eval_using_file_cutoff=10000)
MyR('thestringyouwant to evaluate')

but in this case you'd lose the drop-down menu efficiency.

Edit:

The reason things (should) work anyway when you aren't asking for output to show up is that this method of the Expect interface (what we use to talk to R) will take long commands, write them to a file, and run them. So all variables should be defined fine. However, that means that it will create any auxiliary files (like graphics) in that context, which won't show up in the notebook since the computation wasn't done "in the cell".

The auxiliary file is defined here, which for me was

$ pwd
/Users/<snip>/.sage/temp/GC04855.local/15413/interface
edit flag offensive delete link more

Comments

I can confirm in my own local Sage installation that changing `1024` to `1026` allowed two extra characters - try this!

kcrisman gravatar imagekcrisman ( 2013-05-22 02:38:46 -0500 )edit

(edited) OK. I am running `sage -b` to rebuild Sage after changing the r.py from 1024 to 2048. I'm not that familiar with R to begin with (trying to learn); could I load an R script from a file instead of changing the underlying code of Sage? If it is possible to load R code as a script from file for long commands such as this; will Sage still detect that a PNG is produced and show it in the interface? My main reason for using R through Sage is that the user interface is superior to anything else in my opinion, but if plot output doesn't show up then that advantage is nullified...

Dan-Erik gravatar imageDan-Erik ( 2013-05-22 03:55:39 -0500 )edit

After `sage -b` with the 2048 change, I was able to run the code in the example above, with a comment twice the size. But the second time I tried, it didn't work any more. I had a look at my code, and it seems like the problem is more complex than I thought. I had cells with 1580 characters total that successfully compiled BEFORE the 2048 change in the r.py code, but those cells didn't produce output, they just stored data in variables. And I am still not able to show my fancy plot with lots of options that is a cell of around 1800 characters. The 1580 character cell with no output compiles fine though, and the variables do contain what they are supposed to be.

Dan-Erik gravatar imageDan-Erik ( 2013-05-22 04:46:48 -0500 )edit

Here's the thing: changing r.py from 1024 to 2048 made it work, once. But then I installed my optional R packages with r.install_packages() in Sage, and after that it stopped working. I'm thinking the r.install_packages() function may have changed the value back to default?

Dan-Erik gravatar imageDan-Erik ( 2013-05-22 04:50:34 -0500 )edit

variable_1 <- "a" followed by lots of comments (around 2000 bytes) and then variable_2 <- 2 in the same cell assigns the correct values to both variables. The code is executed by R, and if I use a second cell to print the variables, then I get a correct output. However, anything that produces output (print("a") or plot(1:20)) just terminates without an error message.

Dan-Erik gravatar imageDan-Erik ( 2013-05-22 04:56:40 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2013-05-21 23:35:47 -0500

Seen: 488 times

Last updated: May 22 '13