Plotting with R in A Sage Cell

28 views
Skip to first unread message

Thomas Judson

unread,
Jul 19, 2017, 7:23:00 AM7/19/17
to sage...@googlegroups.com, sage-s...@googlegroups.com
The following code works in CoCalc

cases <- c(36, 531, 4233, 8682, 7164, 2229, 600, 164, 57, 722, 1517, 1828, 1539, 2416, 3148, 3465, 1440)
deaths <- c(0, 0, 130, 552, 738, 414, 198, 90, 56, 50, 71, 137, 178, 194, 290, 310, 149) 
plot(ts(cases), col="red", main="Influenza Epidemic")
lines(ts(10*deaths),col="blue”)

However, I get the following error when I try to plot this in a Sage cell

Error in plot.new() : cairo error 'error while writing to output stream'
Error in plot.xy(xy.coords(x, y), type = type, ...) : 
  plot.new has not been called yet

I may or may not get the plot.

Other R plots seem to work in the Sage cells.  Any ideas?

Tom

Andrey Novoseltsev

unread,
Jul 19, 2017, 6:53:35 PM7/19/17
to sage-cell, sage-s...@googlegroups.com

I've tried it ten times or so and got the plot with no errors every time. Can you reproduce it in your setup reliably? I don't have any clue what can be wrong here that it works sometimes but not always...

Thomas Judson

unread,
Jul 20, 2017, 6:24:50 AM7/20/17
to sage-cell, sage-s...@googlegroups.com
I am pretty sure that this is an R problem that occurs because of linked cells.  Look at http://faculty.sfasu.edu/judsontw/math-s304/section-16.html.  The first plot works fine, but somehow I need to clear the previous graphics when I create a new plot.  I have tried dev.off() and graphics.off(), but neither works.  Any ideas?

Andrey Novoseltsev

unread,
Jul 20, 2017, 7:51:12 PM7/20/17
to sage-cell, sage-s...@googlegroups.com
On Thursday, 20 July 2017 04:24:50 UTC-6, Thomas Judson wrote:
I am pretty sure that this is an R problem that occurs because of linked cells.  Look at http://faculty.sfasu.edu/judsontw/math-s304/section-16.html.  The first plot works fine, but somehow I need to clear the previous graphics when I create a new plot.  I have tried dev.off() and graphics.off(), but neither works.  Any ideas?

OK, thanks a lot for the full example, I think this is the reason:
https://github.com/sagemath/sagecell/blob/master/js/session.js#L327
R-cells have device.off() added automatically so that plotting commands that were given by users will all flush to a file that will be automatically displayed. Because R has these "live plots" that you can add stuff to, dealing with them in SageMathCell is a bit tricky. Not sure what is the best solution - perhaps in addition to "closing" commands I should add commands to initialize a new plot so that it is ready for the next cell if it exists?
 

Thomas Judson

unread,
Jul 21, 2017, 5:47:16 AM7/21/17
to sage-cell, sage-s...@googlegroups.com
Yes, I am sure that you have hit on the problem.  Let me know what you come up with.

Andrey Novoseltsev

unread,
Aug 6, 2017, 11:11:05 PM8/6/17
to sage-cell, sage-s...@googlegroups.com
There is one problem after another... The line I've mentioned earlier
https://github.com/sagemath/sagecell/blob/master/js/session.js#L327
saves and shows the default R plots whose names start with Rplot. This causes read only permissions on files via
https://github.com/sagemath/sagecell/blob/master/backend_cell.py#L65
so when R tries to create a new plotting device with the same name it fails. I don't recall reasons for read only but I am pretty sure that's how Volker's display backend system is supposed to function. I have tried to make the file writeable, in which case errors go away, but the same plot gets displayed again and again because it has the same name. Recently user files were prevented from caching, but it does not help here - displaying the same image on the same page does not generate a network request at all, rather the old copy is shown again.

Any ideas on how to fix it? Or how to describe the situation for a search engine?

Andrey Novoseltsev

unread,
Aug 7, 2017, 12:58:20 AM8/7/17
to sage-cell, sage-s...@googlegroups.com
Now http://faculty.sfasu.edu/judsontw/math-s304/section-16.html works fine for me - I don't enforce permission on user files except for group readable (otherwise the web server will not be able to serve them) and there is a timestamp added to the name to force browser to load a new version.

The next problem is that if a linked cell does not create a new plot, then the old one will be shown again - since the file is still there.

Andrey Novoseltsev

unread,
Aug 20, 2017, 8:09:31 PM8/20/17
to sage-cell, sage-s...@googlegroups.com
On Sunday, 6 August 2017 22:58:20 UTC-6, Andrey Novoseltsev wrote:
The next problem is that if a linked cell does not create a new plot, then the old one will be shown again - since the file is still there.

This problem also seems to be solved, please report if you discover any new issues!

Thomas Judson

unread,
Aug 21, 2017, 8:48:16 PM8/21/17
to sage-cell, sage-s...@googlegroups.com
Looks okay to me.

Thanks,
Tom
Reply all
Reply to author
Forward
0 new messages