emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [babel] grid-based R graphical output with :results value
@ 2010-06-08 17:33 Erik Iverson
  0 siblings, 0 replies; only message in thread
From: Erik Iverson @ 2010-06-08 17:33 UTC (permalink / raw)
  To: emacs-orgmode

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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2010-06-08 17:33 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-06-08 17:33 [babel] grid-based R graphical output with :results value Erik Iverson

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).