From mboxrd@z Thu Jan 1 00:00:00 1970 From: Julien Barnier Subject: [PATCH] babel: add a :rownames argument to R code blocks Date: Thu, 25 Mar 2010 00:11:57 +0100 Message-ID: <87eij95n7m.fsf@z.nozav.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Return-path: Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NuZkJ-0002Sw-N7 for emacs-orgmode@gnu.org; Wed, 24 Mar 2010 19:12:19 -0400 Received: from [140.186.70.92] (port=47837 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NuZkI-0002R2-5y for emacs-orgmode@gnu.org; Wed, 24 Mar 2010 19:12:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1NuZkG-0001ou-FU for emacs-orgmode@gnu.org; Wed, 24 Mar 2010 19:12:18 -0400 Received: from lo.gmane.org ([80.91.229.12]:59498) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1NuZkG-0001on-30 for emacs-orgmode@gnu.org; Wed, 24 Mar 2010 19:12:16 -0400 Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1NuZkD-00039U-7D for emacs-orgmode@gnu.org; Thu, 25 Mar 2010 00:12:13 +0100 Received: from z.nozav.org ([91.121.121.141]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 25 Mar 2010 00:12:13 +0100 Received: from julien by z.nozav.org with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 25 Mar 2010 00:12:13 +0100 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@gnu.org Hi, The following simple patch add a :rownames argument to R source code blocks in org-babel. With :rownames yes it allows to export the row names when the result is a table. For example : #+BEGIN_SRC R :session :colnames yes :rownames yes table(d$sexe,d$cuisine) #+END_SRC #+results: | | Non | Oui | |-------+-----+-----| | Homme | 2 | 2 | | Femme | 4 | 2 | Thanks a lot for all your work ! Julien --- contrib/babel/lisp/langs/org-babel-R.el | 12 +++++++----- 1 files changed, 7 insertions(+), 5 deletions(-) diff --git a/contrib/babel/lisp/langs/org-babel-R.el b/contrib/babel/lisp/langs/org-babel-R.el index a8071b2..f0d79b9 100644 --- a/contrib/babel/lisp/langs/org-babel-R.el +++ b/contrib/babel/lisp/langs/org-babel-R.el @@ -46,6 +46,8 @@ called by `org-babel-execute-src-block'." (vars (second processed-params)) (column-names-p (and (cdr (assoc :colnames params)) (string= "yes" (cdr (assoc :colnames params))))) + (row-names-p (and (cdr (assoc :rownames params)) + (string= "yes" (cdr (assoc :rownames params))))) (out-file (cdr (assoc :file params))) (augmented-body (concat @@ -53,7 +55,7 @@ called by `org-babel-execute-src-block'." (mapconcat ;; define any variables (lambda (pair) (org-babel-R-assign-elisp (car pair) (cdr pair))) vars "\n") "\n" body "\n" (if out-file "dev.off()\n" ""))) - (result (org-babel-R-evaluate session augmented-body result-type column-names-p))) + (result (org-babel-R-evaluate session augmented-body result-type column-names-p row-names-p))) (or out-file result)))) (defun org-babel-prep-session:R (session params) @@ -133,9 +135,9 @@ called by `org-babel-execute-src-block'." (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-wrapper-method "main <- function ()\n{\n%s\n} -write.table(main(), file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.names=%s, quote=FALSE)") +write.table(main(), file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=%s, col.names=%s, quote=FALSE)") -(defun org-babel-R-evaluate (session body result-type column-names-p) +(defun org-babel-R-evaluate (session body result-type column-names-p row-names-p) "Pass BODY to the R process in SESSION. If RESULT-TYPE equals 'output then return a list of the outputs of the statements in BODY, if RESULT-TYPE equals 'value then return the value of the @@ -153,7 +155,7 @@ last statement in BODY, as elisp." (stderr (with-temp-buffer (insert (format org-babel-R-wrapper-method - body tmp-file (if column-names-p "TRUE" "FALSE"))) + body tmp-file (if row-names-p "TRUE" "FALSE") (if column-names-p (if row-names-p "NA" "TRUE") "FALSE"))) (setq exit-code (org-babel-shell-command-on-region (point-min) (point-max) "R --no-save" nil 'replace (current-buffer))) (buffer-string)))) @@ -168,7 +170,7 @@ last statement in BODY, as elisp." (case result-type (value (mapconcat #'org-babel-chomp (list body - (format "write.table(.Last.value, file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.names=%s, quote=FALSE)" tmp-file (if column-names-p "TRUE" "FALSE")) + (format "write.table(.Last.value, file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=%s, col.names=%s, quote=FALSE)" tmp-file (if row-names-p "TRUE" "FALSE") (if column-names-p (if row-names-p "NA" "TRUE") "FALSE")) org-babel-R-eoe-indicator) "\n")) (output (mapconcat #'org-babel-chomp (list body org-babel-R-eoe-indicator) "\n")))) -- 1.7.0.3