From mboxrd@z Thu Jan 1 00:00:00 1970 From: Erik Iverson Subject: [babel] grid-based R graphical output with :results value Date: Tue, 08 Jun 2010 12:33:48 -0500 Message-ID: <4C0E7EFC.9000504@ccbr.umn.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Return-path: Received: from [140.186.70.92] (port=38459 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OM2gX-0004wG-1t for emacs-orgmode@gnu.org; Tue, 08 Jun 2010 13:33:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OM2gV-00031o-T6 for emacs-orgmode@gnu.org; Tue, 08 Jun 2010 13:33:56 -0400 Received: from walleye.ccbr.umn.edu ([128.101.116.11]:2542) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OM2gV-00031X-Lo for emacs-orgmode@gnu.org; Tue, 08 Jun 2010 13:33:55 -0400 Received: from tadpole.ccbr.umn.edu (tadpole.ccbr.umn.edu [128.101.116.26]) by walleye.ccbr.umn.edu (8.9.3p2/8.9.3) with ESMTP id MAA11982 for ; Tue, 8 Jun 2010 12:33:51 -0500 (CDT) Received: from iron.ccbr.umn.edu (iron.ccbr.umn.edu [128.101.116.194]) by tadpole.ccbr.umn.edu (8.9.3/8.9.3) with ESMTP id MAA21457 for ; Tue, 8 Jun 2010 12:33:50 -0500 (CDT) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org 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