Hi,

A substantial fraction of times when exporting my org-babel document
(with many R session code blocks), I get "code block produced no
value".  I think this is because of a race condition between
waiting for the transfer.file to exist and actually populating it with
output; the scenario is: 1. write.table() creates its output file
2. emacs notices its existence, and reads it 3. write.table() gets round
to writing .Last.value to the file.

The attached patch has made exporting much more reliable for me.

>From 62aa24a7979d687dd5136775004ff7ff5584564a Mon Sep 17 00:00:00 2001
From: Christophe Rhodes <cs...@cantab.net>
Date: Fri, 20 May 2011 12:12:45 +0100
Subject: [PATCH] fix race condition in ob-R write-object-command

* lisp/ob-R.el (org-babel-R-write-object-command): write the object to a
temporary file, then move that temporary file in place to transfer.file
---
 lisp/ob-R.el |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/lisp/ob-R.el b/lisp/ob-R.el
index 713d59f..8421c69 100644
--- a/lisp/ob-R.el
+++ b/lisp/ob-R.el
@@ -227,7 +227,7 @@ current code buffer."
 (defvar org-babel-R-eoe-indicator "'org_babel_R_eoe'")
 (defvar org-babel-R-eoe-output "[1] \"org_babel_R_eoe\"")
 
-(defvar org-babel-R-write-object-command "{function(object, transfer.file) {object;invisible(if(inherits(try(write.table(object, file=transfer.file, sep=\"\\t\", na=\"nil\",row.names=%s, col.names=%s, quote=FALSE), silent=TRUE),\"try-error\")) {if(!file.exists(transfer.file)) file.create(transfer.file)})}}(object=%s, transfer.file=\"%s\")")
+(defvar org-babel-R-write-object-command "{function(object, transfer.file) {tfile <- tempfile();invisible(if(inherits(try({write.table(object, file=tfile, sep=\"\\t\", na=\"nil\",row.names=%s, col.names=%s, quote=FALSE); file.rename(tfile,transfer.file)}, silent=TRUE),\"try-error\")){if(!file.exists(transfer.file)) file.create(transfer.file)})}}(object=%s, transfer.file=\"%s\")")
 
 (defun org-babel-R-evaluate
   (session body result-type column-names-p row-names-p)
-- 
1.7.2.5

Best,

Christophe

Reply via email to