4

On Debian Stretch, I'm trying to export this file called test.org

# -*- org-confirm-babel-evaluate: nil -*-
#+OPTIONS: ':nil *:t -:t ::t <:t H:3 \n:nil ^:t arch:headline
#+OPTIONS: author:t c:nil creator:nil d:(not "LOGBOOK") date:t e:t
#+OPTIONS: email:nil f:t inline:t num:t p:nil pri:nil prop:nil stat:t
#+OPTIONS: tags:t tasks:t tex:t timestamp:t title:t toc:t todo:t |:t
#+TITLE: Emacs Batch Test
#+DATE: 
#+EMAIL: 
#+LANGUAGE: en
#+SELECT_TAGS: export
#+EXCLUDE_TAGS: noexport
#+CREATOR: Emacs 25.2.1 (Org mode 8.3.4)
#+LATEX_CLASS: article
#+LATEX_CLASS_OPTIONS:
#+LATEX_HEADER:
#+LATEX_HEADER_EXTRA:
#+DESCRIPTION:
#+KEYWORDS:
#+SUBTITLE:

* Section 1

This is some text.

* Section 2

#+NAME: testcode
#+HEADER: :session R:emacsbatchtest
#+BEGIN_SRC R :exports both :results output 
library(arm)
example(display)
#+END_SRC

using this elisp script called test.el

(progn
  (require 'cl-lib)
  (require 'org)
  (require 'ox)
  (require 'ox-latex)
  (require 'ox-publish)
  (defun bh-export-batch-dot-org ()
    (find-file "test.org")
    (org-latex-export-to-pdf nil nil nil nil nil)
    )
  (bh-export-batch-dot-org)
)

using this command line:

$ emacs -batch -l test.el 

Emacs produces the PDF just fine, but it fails to execute the R source code. I can try to set org-confirm-babel-evaluate in my .emacs or in the file; it makes no difference in the results (and I'm not sure that in-file setting is syntactically correct; it doesn't seem to do anything). I've looked, but I haven't found a solution on stackexchange or elsewhere that seems to work.

If I export the .org file manually, it executes the R source code and produces the complete PDF.

Here are a few possibly pertinent snippets from the custom-set-variables section of my .emacs:

 '(org-babel-load-languages
   (quote
    ((emacs-lisp . t)
     (J . t)
     (calc . t)
     (shell . t)
     (ditaa . t)
     (dot . t)
     (plantuml . t)
     (python . t)
     (octave . t)
     (ledger . t)
     (sqlite . t)
     (stan . t)
     (R . t))))
 '(org-confirm-babel-evaluate t)
 '(org-export-backends (quote (ascii beamer html icalendar latex md odt)))

I've also tried this on Windows Server 2016 with cygwin bash, and it fails to produce anything. I'd appreciate insights into how to make this work on both platforms. While it would be nice, the solution doesn't need to be the same on both.

EDIT: I have tried exporting this file two ways: by setting org-confirm-babel-evaluate to nil in my .emacs (not shown in this example) or by setting it to nil in a file variable in the first line of test.org (shown here). The syntax shown here was incorrect, missing a ; after nil. I fixed that, and now it no longer queries about R execution.

It does query for two other reasons. First, it asks if it can apply the local variables. I've now responded !, and it no longer asks.

Second, it asks for the R starting project directory if there's not a live ESS session by the name R:emacsbatchtest. I fixed that by adding

#+HEADER: :dir FQPN

to the source code block headers (where FQPN is the fully-qualified path name of the current directory).

Now I have a file that exports interactively with no interaction after the C-c C-e l o.

Batch mode still fails to run the source or produce the pdf, presumably because '(safe-local-variable-values (quote ((org-confirm-babel-evaluate)))) is now in my init.el, which is not called in batch. As soon as I determine how to set that, I will try it.

EDIT 2:

As I noted in a comment, this works well on Linux, but it fails on W7.

By "this" I mean running emacs --batch --eval '(setq org-confirm-babel-evaluate nil)' -l test.el using the previously provided test.el file and a slightly modified test.org file:

#+OPTIONS: ':nil *:t -:t ::t <:t H:3 \n:nil ^:t arch:headline
#+OPTIONS: author:t c:nil creator:nil d:(not "LOGBOOK") date:t e:t
#+OPTIONS: email:nil f:t inline:t num:t p:nil pri:nil prop:nil stat:t
#+OPTIONS: tags:t tasks:t tex:t timestamp:t title:t toc:t todo:t |:t
#+TITLE: Emacs Batch Test
#+DATE: 
#+EMAIL: 
#+LANGUAGE: en
#+SELECT_TAGS: export
#+EXCLUDE_TAGS: noexport
#+CREATOR: Emacs 25.2.1 (Org mode 8.3.4)
#+LATEX_CLASS: article
#+LATEX_CLASS_OPTIONS:
#+LATEX_HEADER:
#+LATEX_HEADER_EXTRA:
#+DESCRIPTION:
#+KEYWORDS:
#+SUBTITLE:

* Section 1

This is some text.

* Section 2

#+NAME: testcode1
#+HEADER: :session R:emacsbatchtest
#+HEADER: :dir FQDN
#+BEGIN_SRC R :exports both :results output 
library(arm)
example(display)
#+END_SRC
* Section 3
#+NAME: testcode2
#+HEADER: :session R:emacsbatchtest
#+BEGIN_SRC R :exports both :results output 
example(sim)
#+END_SRC

* Section 4

#+NAME: elispexample
#+HEADER: :exports both :results value
#+BEGIN_SRC emacs-lisp
(setq mylist '(a b c))
(cadr mylist)
#+END_SRC

I dropped the file local variable, thanks to setting it in the --eval section, I added the :dir header in the first code block, and I added a second R code block and an emacs-lisp code block.

On GNU/Linux, it executes each code block and publishes the result in a PDF.

On W7, it produces test.tex, but it fails to produce the PDF.

In addition, neither of the two R code blocks are executed, but the emacs-lisp code block is executed.

I think that means there's something wrong with executing R as a function call from inside org-mode and there's something wrong with finding pdflatex.

Perhaps it can't start R successfully; Cygwin has its challenges:

$ R
Fatal error: you must specify '--save', '--no-save' or '--vanilla'

Stealing from my init.el, perhaps this works:

$ /cygdrive/c/emacs/emacs-25.2-x86_64/bin/runemacs.exe --batch --eval "(setq org-confirm-babel-evaluate nil)"  --eval '(setq ess-directory-containing-R "c:")' --eval '(setq ess-rterm-version-paths "C:/R/R-3.4.3/bin/x64/Rterm.exe")' --eval '(setq org-babel-R-command "c:/R/R-3.4.3/bin/R --slave --no-save")'  -l test.el

Nope.

Does anyone see the problem? I think I tried everything pertinent I see in my init.el.

  • 1
    org-confirm-babel-evaluate t will require a confirmation before executing code. Try setting it to nil – Tyler Apr 10 '18 at 15:52
  • maybe the session header argument is causing you problems. When you run this interactively, does emacs prompt you for anything when you start the R session? Try running your sample without this argument and see if it works better. – Tyler Apr 10 '18 at 17:52
1

As you found out, the problem is that --batch prevents Emacs from loading your init file. You can work around that by using something like

emacs --batch -l ~/.emacs ...

or

emacs --batch -l ~/.emacs.d/init.el ...

or

emacs --batch --eval '(setq org-confirm-babel-evaluate nil)' ...
  • Thanks, Stefan. I had found indications that I needed something like (put 'org-confirm-babel-evaluate 'safe-local-variable (lambda (xx) t)), and I wasn't sure that was correct. I also wasn't sure if that was a permanent setting or a per-invocation setting; I think I'd prefer the latter. – Bill Apr 10 '18 at 20:52
  • Such a put statement is only needed in order to set org-confirm-babel-evaluate as a file-local variable but it seems simpler in this case to set it directly on the command line. – Stefan Apr 10 '18 at 21:01
  • I just tested it on Linux, and it seems to work as promised. The keys were your (setq ...), Stefan, setting adding the :dir argument to the first session code block to avoid a query (I think Tyler and I were on that at about the same time). As you said, the first line that sets the file-local variable is no longer necessary. I'll try it on Windows tomorrow. – Bill Apr 11 '18 at 2:12
  • Stefan, your solution worked on Linux, as I said. I never made it work on Windows, and I decided to tangle the code and run the R instead of trying to export the entire report automatically: the marginal benefit wasn't worth the cost of further investigation. swsnr.de/posts/emacs-script-pitfalls seemed to suggest the answer wasn't necessarily trivial. – Bill May 12 '18 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.