Re: [Orgmode] [PATCH] babel: add a :rownames argument to R code blocks
"Eric Schulte" writes: > Hi Julien, > > Thanks for the patch, however it looks like the attached patch breaks > columnname support for R source code blocks. I'm pasting in the > relevant portion of our test suite, from the development.org file in our > development repository [1]. Could you please sort this issue out before > we apply the patch? Hi Julien, Thanks for the patch and for raising this issue. It's been something I've wanted to sort out for a while. I'm attaching a rushed-together org file containing some test examples for colnames and rownames. I think it shows a couple of problems with your patch, but I had to do this quickly so sorry if I'm mistaken. Before we finalise the behaviour, I'd like us to be clear about the following issues: 1. When we say ':rownames yes', are we 1. Declaring that the input has rownames? 2. Declaring that we want the output to have rownames? 3. Both? 2. Same as (1) but for colnames I've added the test file to our devel repo at http://github.com/eschulte/babel-dev/ please contact either of us offline for access to the repo. Could we use this org file as the basis for settling on final colnames/rownames behaviour in org-babel-R? Dan #+title: Column and row names * Tables #+tblname: A | row1 | 11 | 12 | | row2 | 21 | 22 | #+tblname: B | | col1 | col2 | |--+--+--| | row1 | 11 | 12 | | row2 | 21 | 22 | * Current behaviour *** OK Simple identity #+begin_src R :var tab=A tab #+end_src #+results: | row1 | 11 | 12 | | row2 | 21 | 22 | *** OK Use org header line #+begin_src R :var tab=B :colnames yes tab #+end_src The X comes from R providing a default name for a missing column name #+results: | X| col1 | col2 | |--+--+--| | row1 | 11 | 12 | | row2 | 21 | 22 | *** Create rownames in R * Simple #+begin_src R :var tab=B array(1:9, dim=c(3,3), dimnames=list(letters[1:3], letters[1:3])) #+end_src #+results: | 1 | 4 | 7 | | 2 | 5 | 8 | | 3 | 6 | 9 | * OK With colnames #+begin_src R :var tab=B :colnames yes array(1:9, dim=c(3,3), dimnames=list(letters[1:3], letters[1:3])) #+end_src #+results: | a | b | c | |---+---+---| | 1 | 4 | 7 | | 2 | 5 | 8 | | 3 | 6 | 9 | * New behaviour *** OK Simple identity #+begin_src R :var tab=A tab #+end_src #+results: | row1 | 11 | 12 | | row2 | 21 | 22 | *** TODO Use org header line Header line from org table is not used. #+begin_src R :var tab=B :colnames yes tab #+end_src #+results: | row1 | 11 | 12 | |--++| | row2 | 21 | 22 | *** OK Use org header line with 'rownames yes' #+begin_src R :var tab=B :colnames yes :rownames yes tab #+end_src #+results: | X| col1 | col2 | |--+--+--| | row1 | 11 | 12 | | row2 | 21 | 22 | *** TODO Create rownames in R * Simple #+begin_src R :var tab=B array(1:9, dim=c(3,3), dimnames=list(letters[1:3], letters[1:3])) #+end_src #+results: | 1 | 4 | 7 | | 2 | 5 | 8 | | 3 | 6 | 9 | * TODO With colnames Inappropriate colnames #+begin_src R :var tab=B :colnames yes array(1:9, dim=c(3,3), dimnames=list(letters[1:3], letters[1:3])) #+end_src #+results: | 1 | 4 | 7 | |---+---+---| | 2 | 5 | 8 | | 3 | 6 | 9 | * TODO With rownames Gets colnames but not rownames #+begin_src R :var tab=B :rownames yes array(1:9, dim=c(3,3), dimnames=list(letters[1:3], letters[1:3])) #+end_src #+results: | a | b | c | | 1 | 4 | 7 | | 2 | 5 | 8 | | 3 | 6 | 9 | * TODO With colnames and rownames Doesn't get rownames (?) #+begin_src R :var tab=B :colnames yes :rownames yes array(1:9, dim=c(3,3), dimnames=list(letters[1:3], letters[1:3])) #+end_src #+results: | a | b | c | |---+---+---| | 1 | 4 | 7 | | 2 | 5 | 8 | | 3 | 6 | 9 | * Org config #+TODO: TODO | OK > > Thanks -- Eric > > relevant test... the final source block should return 169 > > #+tblname: test-table-colnames > | var1 | var2 | var3 | > |--+--+--| > |1 | 22 | 13 | > | 41 | 55 | 67 | > > #+srcname: R-square(x=default-name-doesnt-exist) > #+begin_src R :colnames yes > x^2 > #+end_src > > This should return 169. The fact that R is able to use the column name > to index the data frame (x$var3) proves that a table with column names > (a header row) has been recognised as input for the R-square function > block, and that the R-square block has output an elisp table with > column names, and that the colnames have again been recognised when > creating the R variables in this block. > #+srcname: table-R-colnames-org(x = R-square(x=test-table-colnames)) > #+begin_src R > x$var3[1] > #+end_src > > > Julien Barnier writes: > >> 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 | >> |---+---
Re: [Orgmode] [PATCH] babel: add a :rownames argument to R code blocks
Hi Julien, Thanks for the patch, however it looks like the attached patch breaks columnname support for R source code blocks. I'm pasting in the relevant portion of our test suite, from the development.org file in our development repository [1]. Could you please sort this issue out before we apply the patch? Thanks -- Eric relevant test... the final source block should return 169 #+tblname: test-table-colnames | var1 | var2 | var3 | |--+--+--| |1 | 22 | 13 | | 41 | 55 | 67 | #+srcname: R-square(x=default-name-doesnt-exist) #+begin_src R :colnames yes x^2 #+end_src This should return 169. The fact that R is able to use the column name to index the data frame (x$var3) proves that a table with column names (a header row) has been recognised as input for the R-square function block, and that the R-square block has output an elisp table with column names, and that the colnames have again been recognised when creating the R variables in this block. #+srcname: table-R-colnames-org(x = R-square(x=test-table-colnames)) #+begin_src R x$var3[1] #+end_src Julien Barnier writes: > 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)"
[Orgmode] [PATCH] babel: add a :rownames argument to R code blocks
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 ___ Emacs-orgmode mailing list Please use `Reply All' to send replies to the list. Emacs-orgmode@gnu.org http://lists.gnu.org/mailman/listinfo/emacs-orgmode