From 551d003d15c5cc9d3792909a12247fca58ae8d84 Mon Sep 17 00:00:00 2001 From: "Rainer M. Krug" Date: Mon, 23 Jun 2014 12:11:59 +0200 Subject: [PATCH 2/2] Make transfer of values from R type aware * lisp/ob-R.el (org-babel-R-assign-elisp): Added different cases for transfer of integer, float, string and other variables to R so that now integer values are transferred as integers (L) and stored in R as such. This change is backward compatible as integer values are numerical values in R. Moved definition of R functions for transfer of tables into defconst ob-R-transfer-variable-table-with-header and ob-R-transfer-variable-table-without-header. --- lisp/ob-R.el | 78 ++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 31 deletions(-) diff --git a/lisp/ob-R.el b/lisp/ob-R.el index c77a103..c9f04ea 100644 --- a/lisp/ob-R.el +++ b/lisp/ob-R.el @@ -93,6 +93,44 @@ this variable.") (when (and session (string-match "^\\*\\(.+?\\)\\*$" session)) (save-match-data (org-babel-R-initiate-session session nil))))) +(defconst ob-R-transfer-variable-table-with-header + "%s <- local({ + con <- textConnection( + %S + ) + res <- read.table( + con, + header = %s, + row.names = %s, + sep = \"\\t\", + as.is = TRUE + ) + close(con) + res + })" + "R code used to transfer a table defined as a variable from org to R. +This function is used when the table contains a header.") + +(defconst ob-R-transfer-variable-table-without-header + "%s <- local({ + con <- textConnection( + %S + ) + res <- read.table( + con, + header = %s, + row.names = %s, + sep = \"\\t\", + as.is = TRUE, + fill = TRUE, + col.names = paste(\"V\", seq_len(%d), sep =\"\") + ) + close(con) + res + })" + "R code used to transfer a table defined as a variable from org to R. +This function is used when the table does not contain a header.") + (defun org-babel-expand-body:R (body params &optional graphics-file) "Expand BODY according to PARAMS, return the expanded body." (mapconcat 'identity @@ -198,37 +236,15 @@ This function is called by `org-babel-execute-src-block'." "TRUE" "FALSE")) (row-names (if rownames-p "1" "NULL"))) (if (= max min) - (format "%s <- local({ - con <- textConnection( - %S - ) - res <- read.table( - con, - header = %s, - row.names = %s, - sep = \"\\t\", - as.is = TRUE - ) - close(con) - res - })" name file header row-names) - (format "%s <- local({ - con <- textConnection( - %S - ) - res <- read.table( - con, - header = %s, - row.names = %s, - sep = \"\\t\", - as.is = TRUE, - fill = TRUE, - col.names = paste(\"V\", seq_len(%d), sep =\"\") - ) - close(con) - res - })" name file header row-names max)))) - (format "%s <- %s" name (org-babel-R-quote-tsv-field value)))) + (format ob-R-transfer-variable-table-with-header + name file header row-names) + (format ob-R-transfer-variable-table-without-header + name file header row-names max)))) + (cond ((integerp value) (format "%s <- %s" name (concat (number-to-string value) "L"))) + ((floatp value) (format "%s <- %s" name value)) + ((stringp value) (format "%s <- %S" name value)) + (t (format "%s <- %S" name (prin1-to-string value)))))) + (defvar ess-ask-for-ess-directory) ; dynamically scoped (defun org-babel-R-initiate-session (session params) -- 2.0.0