emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Erik Iverson <eriki@ccbr.umn.edu>
To: emacs-orgmode <emacs-orgmode@gnu.org>
Subject: [babel] grid-based R graphical output with :results value
Date: Tue, 08 Jun 2010 12:33:48 -0500	[thread overview]
Message-ID: <4C0E7EFC.9000504@ccbr.umn.edu> (raw)

Hello,

This is an FYI for those using org-babel-R with grid-based graphical 
systems.

The documentation for org-babel-R says, "If a :file filename.ext header 
arg is provided to an R block, then graphical output from the source 
block is captured on disk, and the output of the source block is a link 
to the resulting file".

This is true for traditional R graphics, but needs a modifier for 
grid-based R graphics, e.g., the lattice and ggplot2 packages.

The graphics functions from lattice and ggplot2 return objects that must 
be explicitly printed to see them, using the print function.  This 
happens automatically when run interactively, e.g. :session, but when 
called inside another function, it does not.  The way :results value is 
defined to operate, my device and ggplot2 function calls are wrapped in 
a 'main' function, and unless I specifically print the object, no output 
is produced.

Another solution is to use :results output in those source headers that 
produce graphical output from these packages.

I believe the following org-mode file summarizes the different ways of 
getting this working.


============= org-mode code begins ==================

* Note that my .Rprofile loads the lattice package, i.e., library(lattice)

* does /not/ produce a file
#+begin_src R :file 1.png :results value
   xyplot(1:10 ~ 1:10)
#+end_src

* does produce a file, by printing object
#+begin_src R :file 2.png :results value
   print(xyplot(1:10 ~ 1:10))
#+end_src

* does produce a file, by using :results output
#+begin_src R :file 3.png :results output
   xyplot(1:10 ~ 1:10)
#+end_src

* does produce a file, by evaluating in :session
#+begin_src R :file 4.png :session
   xyplot(1:10 ~ 1:10)
#+end_src




========= end org-mode code ========================

The following R code shows something potentially interesting


=========== begin R code ==============================

## this produces a graphic when run with R CMD BATCH
myfun <- function() {
   xyplot(1:10 ~ 1:10)
}
png("with-function-wrapper-outside.png")
myfun()
dev.off()

## this does not produce a graphic with R CMD BATCH
myfun <- function() {
   png("with-function-wrapper-inside.png")
   xyplot(1:10 ~ 1:10)
   dev.off()
}
myfun()
============== End R code =============================

In effect, :results value is doing the latter of the two things in the R 
code above.  If the process were reversed, i.e., the 'main' function 
call was wrapped in the graphics function, as in example 1 above, I 
believe it would work.  Of course, this isn't how it is currently 
implemented, and may introduce other complexities.  There are also 
solutions as I've posted above.

--Erik

                 reply	other threads:[~2010-06-08 17:33 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=4C0E7EFC.9000504@ccbr.umn.edu \
    --to=eriki@ccbr.umn.edu \
    --cc=emacs-orgmode@gnu.org \
    /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).