Importing delimited data

18 views
Skip to first unread message

Pizzalova

unread,
Feb 5, 2008, 5:52:03 PM2/5/08
to sage-newbie
Hi all,

I searched but I couldn't find a solution. When using R, I can import
a delimited file into an R data frame using read.table() function. I
uploaded the data using the notebook function, and I checked the
~/.sage/sage_notebook/../../data directory and the data appears to be
there. However, trying rpy through the notebook interface on Mac OSX I
get the following:

from rpy import *

r.read.table('webref.txt')

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/joshchang/.sage/sage_notebook/worksheets/admin/1/code/
6.py", line 4, in <module>
r.read.table(\u0027webref.txt\u0027)
File "/Applications/sage/local/lib/python2.5/site-packages/sympy/
plotting/", line 1, in <module>

File "/Applications/sage/local/lib/python2.5/site-packages/rpy.py",
line 302, in __getattr__
return self.__getitem__(name)
File "/Applications/sage/local/lib/python2.5/site-packages/rpy.py",
line 306, in __getitem__
obj = self.__dict__[name] = self.__dict__.get(name,
self.get(name))
rpy.RPy_RException: Error in function (x, pos = -1, envir =
as.environment(pos), mode = "any", :
variable "read" was not found

or using scan

r.scan('webref.txt',sep=" ")

Warning message:
In file(file, "r") :
cannot open file 'webref.txt', reason 'No such file or directory'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File
"/Users/joshchang/.sage/sage_notebook/worksheets/admin/1/code/7.py",
line 4, in <module>
exec compile(ur'r.scan(\u0027webref.txt\u0027,sep=" ")' + '\n',
'',
'single')
File
"/Applications/sage/local/lib/python2.5/site-packages/sympy/
plotting/",
line 1, in <module>

rpy.RPy_RException: Error in file(file, "r") : unable to open
connection


More generally, how can I read data from a file into the sage
interface itself and have the data stored as a matrix?

Thanks in advance.

Pizzalova

unread,
Feb 6, 2008, 4:23:29 PM2/6/08
to sage-newbie
Ok I figured out why R wasn't finding the data, I had to add ../../ to
the beginning of the file name since each cell is a directory it
seems. Will the R interpreter ever work by selecting the interpreter
without having to append r. to the beginning of each command? Any word
on getting plots to display inline?

Jacob Hicks

unread,
Feb 6, 2008, 5:01:02 PM2/6/08
to sage-...@googlegroups.com
Plots can be displayed inline in the notebook. Any image sent to
os.curdir gets displayed in the notebook. So all you have to do is
tell r to send its images there.
This is an example plotting with r to the notebook.

from rpy import r
r.png(os.curdir+'/sage.png')

r.par(ann=0)
values = [x for x in srange(0,float(pi),.1)]
r.plot(values, [cos(x^2) for x in values], type='l')
_ = r.dev_off()

Jacob

William Stein

unread,
Feb 6, 2008, 6:10:21 PM2/6/08
to sage-...@googlegroups.com

To avoid potential frustration, I just want to note that I tried
this on my OS X laptop and it does _not_ work:


{{{id=141|


from rpy import r
r.png(os.curdir+'/sage.png')

r.par(ann=0)
values = [x for x in srange(0,float(pi),.1)]
r.plot(values, [cos(x^2) for x in values], type='l')
_ = r.dev_off()

///
print "\x01r\x01e327"
Warning message:
In function (filename = "Rplot%03d.png", width = 480, height = 480, :
unable to open connection to X11 display ''


Traceback (most recent call last):

rpy.RPy_RException: Error in X11(paste("png::", filename, sep = ""),
width, height, pointsize, :
unable to start device PNG
}}}

Jacob Hicks

unread,
Feb 7, 2008, 7:27:32 AM2/7/08
to sage-...@googlegroups.com
Does this means that the only output of plotting R supports in sage
instances that aren't compiled from source on a Linux machine with X
development libraries installed is to .ps files? I knew that was true
for source installs of sage. I haven't ever used the binaries of
sage, but it seems they could have the potential to support png output
from R if they were compiled on a machine with the needed libraries.

--
Jacob Hicks
Mathematics Teacher
Trinity Collegiate School

William Stein

unread,
Feb 7, 2008, 1:54:05 PM2/7/08
to sage-...@googlegroups.com, sage-...@googlegroups.com
On Feb 7, 2008 4:27 AM, Jacob Hicks <jacob....@gmail.com> wrote:
>
> Does this means that the only output of plotting R supports in sage
> instances that aren't compiled from source on a Linux machine with X
> development libraries installed is to .ps files?

Yes, that's true.

> I knew that was true
> for source installs of sage. I haven't ever used the binaries of
> sage, but it seems they could have the potential to support png output
> from R if they were compiled on a machine with the needed libraries.

They are built on fairly minimal machines, so that they will actually work.

Anyway, I hope an R expert such as you will improve matters regarding
R graphics in Sage. Send us patches, etc.!

--
William Stein
Associate Professor of Mathematics
University of Washington
http://wstein.org

Pizzalova

unread,
Feb 8, 2008, 6:12:52 PM2/8/08
to sage-newbie
Compiling sage from source and then invoking sage within xterm seem to
make R detect libpng and X11. It seems that the binaries that are
provided were compiled on a mac without the X development headers in
place.

Back to the original topic, I found that I could get to the data using
the built in DATA variable open(DATA+'source.txt') to open source.txt.

source.txt is a 2 column tab delimited file, how can I load it into a
matrix or whatever python object and do a quick and dirty scatter plot
of it without invoking R? Sorry, I'm a complete python newb, and I'm
googling for a solution right now.

Thanks in advance.

On Feb 7, 10:54 am, "William Stein" <wst...@gmail.com> wrote:
> On Feb 7, 2008 4:27 AM, Jacob Hicks <jacob.m.hi...@gmail.com> wrote:
>
>
>
> > Does this means that the only output of plotting R supports in sage
> > instances that aren't compiled from source on a Linux machine with X
> > development libraries installed is to .ps files?
>
> Yes, that's true.
>
> >  I knew that was true
> > for source installs of sage.  I haven't ever used the binaries of
> > sage, but it seems they could have the potential to support png output
> > from R if they were compiled on a machine with the needed libraries.
>
> They are built on fairly minimal machines, so that they will actually work.
>
> Anyway, I hope an R expert such as you will improve matters regarding
> R graphics in Sage.  Send us patches, etc.!
>
>
>
>
>
> > On Feb 6, 2008 6:10 PM, William Stein <wst...@gmail.com> wrote:
>

shahab

unread,
Feb 10, 2008, 9:53:54 PM2/10/08
to sage-newbie
Many of the SAGE binaries are built without X11 option turned on (b/c
of the missing development headers).
For a quick fix without rebuilding SAGE, one can use the following:

Another option for having R display its graphics in the notebook is to
use an X11 independent rendering of jpeg/png/gif images.

1) Install the sage experimental package 'gd-2.0.34'

install_package('gd-2.0.34')

2) Install the R package GDD

From the notebook session:
from rpy import *
r("install.packages(('GDD'),,'http://www.rosuda.org/R')")

3) That's it.
Run the following example:

r.library("GDD")
r.GDD("demo", type = "png", width=800, height=600)
r.par(ann=0)
values = [x for x in srange(0,float(pi),.01)]
r.plot(values, [cos(x^2) for x in values], type='l')
r.dev_off()

Here is the link to the GDD package:
http://cran.r-project.org/doc/packages/GDD.pdf

Shahab

Michael Abshoff

unread,
Feb 11, 2008, 8:56:02 AM2/11/08
to sage-...@googlegroups.com
shahab wrote:
>
> Many of the SAGE binaries are built without X11 option turned on (b/c
> of the missing development headers).
> For a quick fix without rebuilding SAGE, one can use the following:
>
> Another option for having R display its graphics in the notebook is to
> use an X11 independent rendering of jpeg/png/gif images.


That sounds definitely like something that should be done once we do more
work on a tighter integration between Sage and R.

> 1) Install the sage experimental package 'gd-2.0.34'

Any reason the current gd-2.0.35.p2 doesn't work? We are planning to
update to 2.0.36 anyway, I am just curious.

> install_package('gd-2.0.34')
>
> 2) Install the R package GDD
>
> From the notebook session:
> from rpy import *
> r("install.packages(('GDD'),,'http://www.rosuda.org/R')")
>
> 3) That's it.
> Run the following example:
>
> r.library("GDD")
> r.GDD("demo", type = "png", width=800, height=600)
> r.par(ann=0)
> values = [x for x in srange(0,float(pi),.01)]
> r.plot(values, [cos(x^2) for x in values], type='l')
> r.dev_off()
>
> Here is the link to the GDD package:
> http://cran.r-project.org/doc/packages/GDD.pdf
>
> Shahab

I think we should at least open a wishlist ticket for the above solution.
Depending on how large the GDD package for R is we might even do that per
default.

Cheers,

Michael

shahab

unread,
Feb 11, 2008, 6:50:05 PM2/11/08
to sage-newbie

I think I missed the fact that gd-2.0.35.p2 was part of the standard
packages in SAGE. It should work just fine.

Shahab

On Feb 11, 5:56 am, "Michael Abshoff"
<Michael.Absh...@fsmath.mathematik.uni-dortmund.de> wrote:
> shahabwrote:
Reply all
Reply to author
Forward
0 new messages