1

so i'm trying to display an R generated image while using wxpython and rpy2...

The full 131 line code is here... https://gist.github.com/ACollectionOfAtoms/4286c0fc32838b03f2ea

So within the program, the user comes to a point where they are a window genereated by lst_view, which has two buttons "Ok" and "Visualize". Once Visualize is pressed this code is executed..

    def graph(self,event):
    f = open('results.csv', 'wb')
    csvwriter = csv.writer(f)
    for i in self.res:
        for j in range(1,len(i)):
            row = [i[0] ,str(i[j])]
            csvwriter.writerow(row)
    f.close()


    r = robjects.r
    r('''
            source('vis.r')
    ''')
    r_main = robjects.globalenv['main']
    r_main()
    return True

Where in vis.r we have:

graph <- function() {
  res = read.csv("results.csv", header=FALSE)
  res = mutate(res, Percent = 100*(V2/(sum(res$V2))))
  ggplot(data=res, aes(x=V1, y=V2, fill=Percent)) + geom_bar(stat="identity") + coord_flip() + xlab('Facility') + ylab('Number Of Violations')
}
main <- function(){
  print(graph())
}

This doesn't immediately generate the graph, instead it causes a new menu to appear and the graphic only displays if I go to "Page Setup"....

Anyone ideas?!

  • I wouldn't mind an suggestions to alternatives as well! – ACollectionOfAtoms Apr 02 '15 at 01:07
  • Perhaps [save the plot to a file](http://stackoverflow.com/questions/7144118/how-to-save-a-plot-as-image-on-the-disk), and then [use wxpython to display the image](http://stackoverflow.com/q/1315884/190597). – unutbu Apr 02 '15 at 01:26
  • I initially wanted to do this, but I doing dev.off() within the script wouldn't work, and doing it within python also failed. I also spent some time messing around with dev.set(which=1) or 2 or 3 and I couldn't do it like that either! Perhaps I wasn't doing it correctly... – ACollectionOfAtoms Apr 02 '15 at 05:19
  • Ooooooh woowwwww. Thank you so much for that link. I'll answer my own question now. – ACollectionOfAtoms Apr 02 '15 at 05:33

1 Answers1

1

Alright, thanks to unutbu I was able to figure this out!

Basically I initially wanted to save the image and then visualize it with wx, but i had problems using dev.off() and such through rpy2!

BUT! unutbu provided a link and saved me a ton of trouble. you can use ggsave() to completely circumvent the whole jpeg('this.jpg'); dev.off(); business!

Thanks again, unutbu. Now i'll just render it with wx no problem!

Community
  • 1
  • 1
  • The documentation had an example showing how to use (and close) graphical devices though: http://rpy.sourceforge.net/rpy2/doc-2.5/html/graphics.html#graphical-devices – lgautier Apr 02 '15 at 10:54
  • Yeah, I tried the grdevices.dev_off() method mentioned in the doc in various ways but I never was able to do it correctly for some reason. More than likely my fault but like i said, ggsave is really nice so you don't have to think about which device you're on and which to deactivate etc. – ACollectionOfAtoms Apr 04 '15 at 22:09