From mboxrd@z Thu Jan 1 00:00:00 1970 From: "Charles C. Berry" Subject: Re: ob-R, problem with try/catch Date: Wed, 22 Apr 2015 19:23:34 -0700 Message-ID: References: Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:37996) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yl6o6-0005dF-R8 for emacs-orgmode@gnu.org; Wed, 22 Apr 2015 22:24:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Yl6o3-0004bk-Kh for emacs-orgmode@gnu.org; Wed, 22 Apr 2015 22:24:02 -0400 Received: from iport-acv4-out.ucsd.edu ([132.239.0.7]:57119) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yl6o3-0004XP-Cb for emacs-orgmode@gnu.org; Wed, 22 Apr 2015 22:23:59 -0400 In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: "Thomas S. Dye" Cc: Org-mode On Wed, 22 Apr 2015, Thomas S. Dye wrote: > Aloha all, > > Prior to eaa3a761dae, when working in a session, I was able to run this > R source code block without problems: > > ,----------------------------------------- > | #+header: :file r/adze_wt_log.pdf > | #+header: :results output graphics > | #+header: :width 4 :height 3 > | #+begin_src R > | g <- ggplot(x, aes(x = weight)) > | g + geom_histogram(aes(y=..density..)) ## Try this: print( g + geom_histogram(aes(y=..density..)) ) # before rm(g). > | rm(g) > | #+end_src > `----------------------------------------- > > After eaa3a761dae, I get an error and an empty output file. > That commit introduced a tryCatch() wrapper for graphics results. You probably know that ggplot (or ggplot2) relies on printing of objects to produce graphics (see R-FAQ 7.22). tryCatch(expr,...) evaluates expr and returns its value, which is `rm(g)' in your case. But `rm(g)' is not autoprinted, and you get an empty file. > I can work around this error by removing the line "rm(g)", Right. Then, the expression returned by tryCatch is g + geom_histogram(aes(y=..density..)) which is autoprinted giving the graph. When in doubt, there is no harm in explicitly print()ing objects that would have been autoprinted otherwise. For reference, here is what org-babel-execute:R produces for your src block (lightly formatted for readability): #+BEGIN_SRC R pdf(file=\"r/adze_wt_log.pdf\",width=4,height=3) tryCatch({ g <- ggplot(x, aes(x = weight)) g + geom_histogram(aes(y=..density..)) rm(g) }, error=function(e){ plot(x=-1:1, y=-1:1, type='n', xlab='', ylab='', axes=FALSE) text(x=0, y=0, labels=e$message, col='red') paste('ERROR', e$message, sep=' : ')}) dev.off() #+END_SRC HTH, Chuck