From mboxrd@z Thu Jan 1 00:00:00 1970 From: Rainer M Krug Subject: Re: ob-R, problem with try/catch Date: Thu, 23 Apr 2015 07:40:25 +0200 Message-ID: <83477C11-94B6-467B-8CD5-77976FE83C31@gmail.com> References: Mime-Version: 1.0 (1.0) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:46589) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yl9sK-0002Ia-35 for emacs-orgmode@gnu.org; Thu, 23 Apr 2015 01:40:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Yl9sF-0005OL-1t for emacs-orgmode@gnu.org; Thu, 23 Apr 2015 01:40:35 -0400 Received: from mail-wi0-x231.google.com ([2a00:1450:400c:c05::231]:35228) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yl9sE-0005Ny-SE for emacs-orgmode@gnu.org; Thu, 23 Apr 2015 01:40:31 -0400 Received: by widdi4 with SMTP id di4so202371568wid.0 for ; Wed, 22 Apr 2015 22:40:29 -0700 (PDT) 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: "Charles C. Berry" Cc: Org-mode , "Thomas S. Dye" Envoy=C3=A9 de mon iPhone > Le 23 avr. 2015 =C3=A0 04:23, Charles C. Berry a =C3=A9= crit : >=20 >> On Wed, 22 Apr 2015, Thomas S. Dye wrote: >>=20 >> Aloha all, >>=20 >> Prior to eaa3a761dae, when working in a session, I was able to run this >> R source code block without problems: >>=20 >> ,----------------------------------------- >> | #+header: :file r/adze_wt_log.pdf >> | #+header: :results output graphics >> | #+header: :width 4 :height 3 >> | #+begin_src R >> | g <- ggplot(x, aes(x =3D weight)) >> | g + geom_histogram(aes(y=3D..density..)) >=20 > ## Try this: >=20 > print( g + geom_histogram(aes(y=3D..density..)) ) # before rm(g). >=20 >=20 >> | rm(g) >> | #+end_src >> `----------------------------------------- >>=20 >> After eaa3a761dae, I get an error and an empty output file. >=20 > That commit introduced a tryCatch() wrapper for graphics results. >=20 > You probably know that ggplot (or ggplot2) relies on printing of objects t= o produce graphics (see R-FAQ 7.22). >=20 > tryCatch(expr,...) evaluates expr and returns its value, which is `rm(g)' i= n your case. But `rm(g)' is not autoprinted, and you get an empty file. I am not in front of my computer but there must be more, as even before the c= ommit there should have been empty file for exactly the same reason. Also, t= he error is strange. Could you send a small reproducable example, so that we= can see which error you get? Because if you get an error and an empty file,= an error must be in the tryCatcb block.=20 Thanks, Rainer >=20 >> I can work around this error by removing the line "rm(g)", >=20 > Right. Then, the expression returned by tryCatch is >=20 > g + geom_histogram(aes(y=3D..density..)) >=20 > which is autoprinted giving the graph. >=20 > When in doubt, there is no harm in explicitly print()ing objects that woul= d have been autoprinted otherwise. >=20 > For reference, here is what org-babel-execute:R produces for your src bloc= k (lightly formatted for readability): >=20 > #+BEGIN_SRC R > pdf(file=3D\"r/adze_wt_log.pdf\",width=3D4,height=3D3) > tryCatch({ > g <- ggplot(x, aes(x =3D weight)) > g + geom_histogram(aes(y=3D..density..)) > rm(g) > }, > error=3Dfunction(e){ > plot(x=3D-1:1, y=3D-1:1, type=3D'n', xlab=3D'', ylab=3D'', > axes=3DFALSE) > text(x=3D0, y=3D0, labels=3De$message, col=3D'red') > paste('ERROR', e$message, sep=3D' : ')}) > dev.off() > #+END_SRC >=20 > HTH, >=20 > Chuck >=20