cwind
cwind

Reputation: 432

how to pass a function or functions between src blocks in org-mode (do not use :session, use :var or others instead)

I want to share a function between different src blocks in org-mode. Certainly, we could use :session in org-mode. However, it may cause errors if the variable names are the same.

I would like to use the function repeatedly in different sessions. How can I do it? I try :var, but I failed to pass functions, such as :var theme_ls1=theme; :var theme_ls1()=theme.

It would be better if the function keeps the form theme_ls() in the second src block. Maybe can we include src blocks in a single org file.

I show a correct example by using :session.

* block 1
  #+NAME: theme
  #+BEGIN_SRC R :session info :exports none
    ## Theme for plot
    theme_ls1 <- function(..., bg='white'){
    require(grid)
    theme_classic(...) +
        theme(
        panel.background=element_rect(fill='transparent', color='blue'),
        panel.border=element_rect(fill='transparent', color='transparent'),
        panel.grid=element_blank(),
        plot.margin=unit(c(0.4,0.4,0.4,0.4),"mm")
        )
    }
  #+END_SRC

* block 2
  #+BEGIN_SRC R :exports results :results output graphics :session info :file exp1.1.png :width 400 :height 300
    #!/usr/bin/env Rscript
    ## library
    library(ggplot2)

    ## data
    dt <- data.frame(x=-6:6, y=-6:6)

    ## pic
    pp1 <- ggplot(data=dt, aes(x=x, y=y))+
    geom_point()+
    scale_x_continuous(limits=c(-6,6), breaks=-6:6)+
    scale_y_continuous(limits=c(-6,6), breaks=-6:6)+
    theme_ls1()
    pp1
  #+END_SRC

Ref: https://emacs.stackexchange.com/questions/19073/org-mode-passing-variables-between-code-blocks-without-sessions , but different

Upvotes: 0

Views: 111

Answers (1)

Rorschach
Rorschach

Reputation: 32446

I don't really understand why you wouldn't want to use a session here -- having variable conflicts should be a problem -- otherwise I think a proper design would put your reusable code in a proper namespace.

But, it seems an ugly workaround could be to use eval(parse(...)), which is generally considered bad practice.

* block 1
#+NAME: theme
#+BEGIN_SRC R :exports none :results output
## Theme for plot
theme_ls1 <- function(..., bg='white'){
    require(grid)
    theme_classic(...) +
        theme(
            panel.background=element_rect(fill='transparent', color='blue'),
            panel.border=element_rect(fill='transparent', color='transparent'),
            panel.grid=element_blank(),
            plot.margin=unit(c(0.4,0.4,0.4,0.4),"mm")
        )
}
theme_ls1
#+END_SRC


* block 2
#+BEGIN_SRC R :exports results :results output graphics :var theme_ls1=theme :file exp1.1.png :width 400 :height 300

library(ggplot2)
theme_ls1 <- eval(parse(text=theme_ls1))

## data
dt <- data.frame(x=-6:6, y=-6:6)

## pic
pp1 <- ggplot(data=dt, aes(x=x, y=y)) +
    geom_point() +
    scale_x_continuous(limits=c(-6,6), breaks=-6:6) +
    scale_y_continuous(limits=c(-6,6), breaks=-6:6) +
    theme_ls1()
pp1
#+END_SRC

Upvotes: 0

Related Questions