Skip to content

Instantly share code, notes, and snippets.

@philippbayer
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philippbayer/8837906 to your computer and use it in GitHub Desktop.
Save philippbayer/8837906 to your computer and use it in GitHub Desktop.
weirdness in rpy2
In R:
png("heatmap_before_sorting.png")
image(distance_matrix)
dev.off()
library("gclus")
ordered <- order.single(distance_matrix, clusters=NULL)
sorted_matrix <- distance_matrix[ordered, ordered]
In Python:
import rpy2
# for automatic conversion of numpy matrices
import rpy2.robjects.numpy2ri
rpy2.robjects.numpy2ri.activate()
# import the gclus R package
from rpy2.robjects.packages import importr
gclus = importr('gclus')
import rpy2.robjects as robjects
r = robjects.r
# make a heatmap out of the distance matrix and plot
grdevices = importr('grDevices')
grdevices.png(file="heatmap_before_sorting.png", width=512, height=512)
# make the heatmap
r["image"](distance_matrix) # notice the weird method call here, standard methods like image() are values in a dictionary (??)
grdevices.dev_off()
# now sort
ordered = gclus.order_single(distance_matrix, clusters=r("NULL")) # packages, on the other hand, have proper methods
# also note that R's order.single() became order_single()
# also note the weird r("NULL") instead of R's NULL
sorted_matrix = distance_matrix.rx(ordered, ordered) # notice the rx() method - R-like indexing, since ordered is a vector of indices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment