From mboxrd@z Thu Jan 1 00:00:00 1970 From: Rainer M Krug Subject: Re: [babel] suggestion: wrap creation of graphics into try() block Date: Mon, 20 Jan 2014 16:13:56 +0100 Message-ID: <52DD3D34.7090101@krugs.de> References: <52DD2660.3090403@krugs.de> <878uua3g91.fsf@med.uni-goettingen.de> Reply-To: Rainer@krugs.de Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:36618) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W5GY8-0000EN-Rp for emacs-orgmode@gnu.org; Mon, 20 Jan 2014 10:14:10 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1W5GY3-00077V-EB for emacs-orgmode@gnu.org; Mon, 20 Jan 2014 10:14:04 -0500 Received: from mail-we0-f180.google.com ([74.125.82.180]:43940) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W5GY3-00077R-4t for emacs-orgmode@gnu.org; Mon, 20 Jan 2014 10:13:59 -0500 Received: by mail-we0-f180.google.com with SMTP id q59so7085917wes.11 for ; Mon, 20 Jan 2014 07:13:58 -0800 (PST) In-Reply-To: <878uua3g91.fsf@med.uni-goettingen.de> 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: Andreas Leha , emacs-orgmode@gnu.org -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 01/20/14, 15:00 , Andreas Leha wrote: > Rainer M Krug writes: > >> Hi >> >> I have two suggestions which are liked to each other. They are >> based on R code blocks, but the should be useful for other >> languages as well. >> >> I have a document in which I use :session for creating R graphs. >> These look like: >> >> #+begin_src R :file Correlation_1.pdf :results graphics IFN.mean >> <- load.IFN.mean() grid <- load.grid.CASTANEA.average() >> image(IFN.mean) #+end_src >> >> Everything works fine, unless there is an error in one graph. >> >> In this case, the device remains open, which leaves open devices >> at the end of the export of the document. > > I experience the same problem. And would love to see that > addressed. Just checked in the code from ob-R.el, and it should not be to difficult: The functions org-babel-R-construct-graphics-device-call and org-babel-expand-body:R would be affected. I changed : ... Trying it out ... This seems to be working - I just changed it and it closes the device. But further tests are needed. I will leave it to see if anything unexpected happens. If somebody could look if this makes sense? (defun org-babel-expand-body:R (body params &optional graphics-file) "Expand BODY according to PARAMS, return the expanded body." (let ((graphics-file (or graphics-file (org-babel-R-graphical-output-file params)))) (mapconcat #'identity (let ((inside (append (when (cdr (assoc :prologue params)) (list (cdr (assoc :prologue params)))) (org-babel-variable-assignments:R params) (list body) (when (cdr (assoc :epilogue params)) (list (cdr (assoc :epilogue params))))))) (if graphics-file (append (list (org-babel-R-construct-graphics-device-call graphics-file params)) inside ;; my edits (list "},error=function(e){plot(-1:1, -1:1, type='n'); text(0,0,'DUMMY')}); dev.off()")) inside)) ;; end "\n"))) and (format "%s(%s=\"%s\"%s%s%s); tryCatch({" device filearg out-file args (if extra-args "," "") (or extra-args "")))) in org-babel-R-construct-graphics-device-call It is working with the Dummy. Could somebody please check if this is working? I am leaving my changes and will see during my work if it is fine. Cheers, Rainer > >> >> If the code block which should create the graph would be wrapped >> into a try() block, so that it would look like the following: >> >> try( { pdf("./Correlation_1.pdf") IFN.mean <- load.IFN.mean() >> grid <- load.grid.CASTANEA.average() image(IFN.mean) } ) >> dev.off() >> >> The device would be closed even if an error occurred during the >> execution of the code. >> >> But still, when exporting to pdf, the call to pdflatex fails as >> only a empty pdf is available. So it becomes necessary to go >> through the pdf log to identify the graphs which failed. >> >> In this case it would be useful, to have a placeholder grah in >> the final pdf, so that one can see which graphs did not work. >> >> In addition, this could be used as placeholders (well - they are >> placeholders) for to be created graphs, while the text has >> already been written. >> >> So my second suggestion would be to include a placeholder image, >> which would be used if the generation of the actual graph fails. >> It would be great (but not necessary) if the actual error >> message would be in the image. > > Such an place holder image would be great, indeed. > > > Thanks for bringing this up! > > Regards, Andreas > > - -- Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany) Centre of Excellence for Invasion Biology Stellenbosch University South Africa Tel : +33 - (0)9 53 10 27 44 Cell: +33 - (0)6 85 62 59 98 Fax : +33 - (0)9 58 10 27 44 Fax (D): +49 - (0)3 21 21 25 22 44 email: Rainer@krugs.de Skype: RMkrug -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.22 (Darwin) Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBAgAGBQJS3T00AAoJENvXNx4PUvmCZt0H/2bfO6rKpB3i5R8RiVMk0Vjc f6N6gnE/kZJNvOj+xtDpesrX2XtAnPzhkeB36LN+uFp2gjObtutwcnsPzOXsUI5g Tamgajk7igafGVJAMWFPDPLXlBu7ayo0AC/NGKrxzjhXIpAGFBi50J4w8nhLXL6l e9k7ovt8yp+uhBLs6X/VI5M3Cph9RU9bcBxkDYWceXz2k4SXesWZZpmJW57okRge yC72SHF2Depkg8PaBHLk75FeAQaZYn1bhnjEwlLKYR7XWGLrIuVCgGuN4ttC0y+F by9t9wvs+uqnP7rG/kYDnkcxHxyVaVJEcUYaELeWiOnrd9R+GZlWY6JADax/BcQ= =x+U0 -----END PGP SIGNATURE-----