emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Charles C. Berry" <ccberry@ucsd.edu>
To: "Thomas S. Dye" <tsd@tsdye.com>
Cc: Org-mode <emacs-orgmode@gnu.org>
Subject: Re: ob-R, problem with try/catch
Date: Wed, 22 Apr 2015 19:23:34 -0700	[thread overview]
Message-ID: <alpine.OSX.2.00.1504221851500.2575@charles-berrys-macbook.local> (raw)
In-Reply-To: <m2fv7rstvg.fsf@tsdye.com>

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

  reply	other threads:[~2015-04-23  2:24 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-22 22:03 ob-R, problem with try/catch Thomas S. Dye
2015-04-23  2:23 ` Charles C. Berry [this message]
2015-04-23  5:40   ` Rainer M Krug
2015-04-23 15:53     ` Thomas S. Dye
2015-04-23 16:20       ` Rainer M Krug
2015-04-23 16:42         ` Aaron Ecay
2015-04-23 16:13     ` Charles C. Berry
2015-04-23 16:29       ` Rainer M Krug
2015-04-23 17:11         ` Thomas S. Dye
2015-04-23 22:39           ` Aaron Ecay
2015-04-23 23:36             ` Thomas S. Dye
2015-04-26 12:49         ` Nicolas Goaziou
2015-04-26 17:11           ` Charles C. Berry
2015-04-26 17:28             ` Nicolas Goaziou
2015-05-04  8:35             ` Rainer M Krug
2015-05-04 19:03               ` Nicolas Goaziou
2015-04-23  6:35   ` Thomas S. Dye

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.OSX.2.00.1504221851500.2575@charles-berrys-macbook.local \
    --to=ccberry@ucsd.edu \
    --cc=emacs-orgmode@gnu.org \
    --cc=tsd@tsdye.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).