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: Tue, 21 Jan 2014 10:52:50 +0100 Message-ID: <52DE4372.5090800@krugs.de> References: <52DD2660.3090403@krugs.de> <878uua3g91.fsf@med.uni-goettingen.de> <52DD3D34.7090101@krugs.de> <87txcyy8dm.fsf@gmail.com> <52DD56A5.4080901@krugs.de> <87ob36wlbp.fsf@gmail.com> Reply-To: Rainer@krugs.de Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------020000040905000306080707" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:58190) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W5Y0x-0000wS-EK for emacs-orgmode@gnu.org; Tue, 21 Jan 2014 04:53:04 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1W5Y0r-0003pi-7p for emacs-orgmode@gnu.org; Tue, 21 Jan 2014 04:52:59 -0500 Received: from mail-wg0-f42.google.com ([74.125.82.42]:61461) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W5Y0q-0003pL-VN for emacs-orgmode@gnu.org; Tue, 21 Jan 2014 04:52:53 -0500 Received: by mail-wg0-f42.google.com with SMTP id l18so4777460wgh.1 for ; Tue, 21 Jan 2014 01:52:52 -0800 (PST) In-Reply-To: <87ob36wlbp.fsf@gmail.com> 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: Eric Schulte Cc: Andreas Leha , emacs-orgmode@gnu.org This is a multi-part message in MIME format. --------------020000040905000306080707 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 01/20/14, 19:38 , Eric Schulte wrote: >> >> I will check it a little bit longer and see that I can display >> the error message in the dummy graphic. Should have it by >> tomorrow. >> > > Sounds good. Alternately, maybe you could get the R process to > fail and print an error to STDERR so that the export will stop > immediately and inform the user of the error. I think it is better to catch the error and return with the computations in the next block, because 1) a code block which produces a graph is most often an end point, i.e. further blocks should (as I see it) not rely on the output of this block 2) if the export continues, one can fix more then one error at a time, so being more efficient. 3) If the export continues, it makes it possible to use this to create placeholder graphs: #+begin_src R :file TheFantasticGraph.pdf :results graphics stop("This is a placeholder for a new fantastic graph" #+end_src Where TheFantasticGraph will display the message "This is a placeholder for a new fantastic graph". I have now added the error to the normal R output in addition to a graph, but the export is not canceled. > >> >> I'll send you the patch then. >> > > Sounds great, thanks. OK - here it is attached (my first patch to org :-) ) - let me know if it is OK. > >> >> This is the first time I really work with patches - I assume >> "git diff" will give me the patch you need? >> > > The best would be to create the patch with git format-patch, see > http://orgmode.org/worg/org-contribute.html#sec-4-2. > > If the patch changes more than 10 lines, you'll need to fill out > the FSF copyright assignment paperwork. It is two lines, but it might be worth considering to d=o the paperwork, as I am thinking about doing some other things on ob-R.el > > Thanks, My pleasure, Rainer > >> >> Cheers, >> >> Rainer >> >>> >>> Thanks, >>> >>>> >>>> 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 >>> >> >> -- 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 > - -- 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/ iQEcBAEBAgAGBQJS3kNyAAoJENvXNx4PUvmCZXkIAMLm2LTuB8/FFZidI12yRhuF N90DVeqYYcHpsJYTIk74W3EX2Wj6p4zjH84TXsVRUc3CRnh2EploU7MTYbdxtshC VLTLsKxgfX8FKXz78ADeUqFmOL6KkJihS+P09mrkqwzq652YvGKbw3H0NLwQDfnR Dp4K3wwYfArFtteCPNgZjBcUVgKF5guyfpEvdYEroR7bJ2wHp4FCaQuaKGXxIPfk oXDDwiIjM7+cboZ4Nub6JnSu48GjTKbP0IkP81ejGpgl4JdE447Z13ikvRAeWfL1 dbWbL0aQSmdpeg/3M21Os9XtOcSesD21ueHUW5sblfAIAjIZRDpW++2jfCA6hS0= =Hro3 -----END PGP SIGNATURE----- --------------020000040905000306080707 Content-Type: text/plain; charset=UTF-8; x-mac-type="0"; x-mac-creator="0"; name="0001-Graphic-Catch-errors-and-return-error-message.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-Graphic-Catch-errors-and-return-error-message.patch" >From 3805d053a912ede4ae377b22f10c3562ebd4b967 Mon Sep 17 00:00:00 2001 From: "Rainer M. Krug" Date: Tue, 21 Jan 2014 10:35:52 +0100 Subject: [PATCH] Graphic: Catch errors and return error message * lisp/ob-R.el (org-babel-expand-body:R): Added the opening of the tryCatch() wrapper * lisp/ob-R.el (org-babel-R-construct-graphics-device-call): Added closing of the wrapper containing the error function. Added tryCatch() wrapper around the execution of the source block so that if an error occurs - the R graphic device is closed even when an error occurs - a graph containing the error message is created - the error message is printed in the R session in the form ERROR : the error message TINYCHANGE --- lisp/ob-R.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lisp/ob-R.el b/lisp/ob-R.el index c907d18..62aa7f2 100644 --- a/lisp/ob-R.el +++ b/lisp/ob-R.el @@ -112,7 +112,7 @@ this variable.") (list (org-babel-R-construct-graphics-device-call graphics-file params)) inside - (list "dev.off()")) + (list "},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()")) inside)) "\n"))) @@ -295,7 +295,7 @@ Each member of this list is a list with three members: (substring (symbol-name (car pair)) 1) (cdr pair)) "")) params "")) - (format "%s(%s=\"%s\"%s%s%s)" + (format "%s(%s=\"%s\"%s%s%s); tryCatch({" device filearg out-file args (if extra-args "," "") (or extra-args "")))) -- 1.8.3.4 (Apple Git-47) --------------020000040905000306080707--