Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R + Org-mode: how to avoid naming plot files?

Tags:

When I use Sweave with R, I can avoid explicitly naming the plot files by simply enclosing the plot commands within a code-chunk like <<fig=TRUE>> ... @. The Sweave driver automatically generates numbered plot files like fig1.pdf, fig2.pdf, etc.

However in org-mode, it seems like I need to explicitly name the figure file using a :file [...].pdf in the header, e.g.

#+attr_latex: width=8cm placement=[htbp]
#+begin_src R :results output graphics :exports results :file fig.pdf                                                                                                                                                                                                 
    require(ggplot2)                                                                                                                           
    a <- rnorm(100)                                                                                                                            
    b <- 2*a + rnorm(100)                                                                                                                      
    d <- data.frame(a,b)                                                                                                                       
    ggplot(d,aes(a,b)) + geom_point()                                                                                                          
#+end_src                                     

Is there some way to avoid explicitly naming the plot file, and have the org-mode latex export engine generate these file-names?

Update: I'm including the solution that G. Jay Kerns pointed to here for easy reference: all you need to do is include a temp-file-generating emacs-lisp function in the header, like :file (org-babel-temp-file "./figure-" ".pdf"). This creates a temp figure-file in the current directory (because of the ./). If you want the temp figure-file in a global temp directory (defined by the variable org-babel-temporary-directory), then just say ".figure":

#+attr_latex: width=8cm placement=[htbp]
#+begin_src R :results output graphics :exports results :file (org-babel-temp-file "./figure-" ".pdf")                                                                                                                                                                                                 
    require(ggplot2)                                                                                                                           
    a <- rnorm(100)                                                                                                                            
    b <- 2*a + rnorm(100)                                                                                                                      
    d <- data.frame(a,b)                                                                                                                       
    ggplot(d,aes(a,b)) + geom_point()                                                                                                          
#+end_src                                     
like image 807
Prasad Chalasani Avatar asked Nov 30 '11 14:11

Prasad Chalasani


1 Answers

Great question, and a similar one (plus some extra stuff) came up on the Org-mode mailing list back in September. The original question is here, and a sketch of a possible solution lies in the final message of the thread here (see #1, the other items are about other things).

like image 146
G. Jay Kerns Avatar answered Oct 12 '22 00:10

G. Jay Kerns