Skip to content Skip to sidebar Skip to footer

Read R Function Output As Columns

I'm trying to come up with a way to solve this question I asked yesterday: rpy2 fails to import 'rgl' R package My goal is to check if certain packages are installed inside R from

Solution 1:

rpack in your case is an rpy2.robjects.vectors.Matrix object. Therefore you can simply use rpy2 class method .rx() to extract the column:

mylist = list(rpack.rx(True, 1))

Have a try.


Solution 2:

I've not used r2py before, but it looks like it's some kind of r2py object, and that might have an option to just grab that first column.

You could hapily parse it like a text file though; when you call print XXX it grabs the string representation of the object.

Try doing something like this:

s = str(rpack)
packages = [line.split()[0] for line in s.split("\n")[1:]]

You should try both the str and repr methods to get the string representation though, some people don't use both, or use them differently.

This doesn't feel like the cleanest way to do it though, and you'll have to make sure you parse the data correctly. Try printing dir(rpack) and seeing if there are any attributes in there which sound like they'll contain what you want.

A little bit of digging, the installed_packages documentation, and a quick peek at an R tutorial suggests you can just do this:

print mpack[,"Package"]

Post a Comment for "Read R Function Output As Columns"