From mboxrd@z Thu Jan 1 00:00:00 1970 From: Vicente Vera Subject: Re: Strings converted to numbers in Org table? Date: Mon, 27 Feb 2017 15:31:58 +0000 Message-ID: References: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=001a1134e9e4b8c039054984c70d Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:55424) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ciNHN-0004SN-50 for emacs-orgmode@gnu.org; Mon, 27 Feb 2017 10:32:02 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ciNHM-0003Zd-1q for emacs-orgmode@gnu.org; Mon, 27 Feb 2017 10:32:01 -0500 Received: from mail-oi0-x231.google.com ([2607:f8b0:4003:c06::231]:33994) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ciNHL-0003ZT-QY for emacs-orgmode@gnu.org; Mon, 27 Feb 2017 10:31:59 -0500 Received: by mail-oi0-x231.google.com with SMTP id m124so13786430oig.1 for ; Mon, 27 Feb 2017 07:31:59 -0800 (PST) In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-orgmode --001a1134e9e4b8c039054984c70d Content-Type: text/plain; charset=UTF-8 Hello again, I'm sorry for being noisy. OK yes, `org-babel-read' is indeed converting "number strings" to numbers. Basically this is what happens: : (string-to-number "3.350") => 3.35 To leave cell values unchanged I did this clumsy hack: #+BEGIN_SRC emacs-lisp (defun test-other-org-babel-read (cell &optional inhibit-lisp-eval) (if (and (stringp cell) (not (equal cell ""))) ;; ;; (or (org-babel--string-to-number cell) (if (and (not inhibit-lisp-eval) (or (member (substring cell 0 1) '("(" "'" "`" "[")) (string= cell "*this*"))) (eval (read cell) t) (if (string= (substring cell 0 1) "\"") (read cell) (progn (set-text-properties 0 (length cell) nil cell) cell))) ;; ;; ) cell)) ;; Override default behavior (fset 'org-babel-read 'test-other-org-babel-read) #+END_SRC It would be useful to have a header argument to prevent this conversion. Probably somebody else has had the same issue? 2017-02-27 13:49 GMT+00:00 Vicente Vera : > Probably the issue is related to this function in `ob-core.el'? > > org-babel-read: "Convert the string value of CELL to a number if > appropriate." > > Behind the curtains lies the built-in function `string-to-number'. > > Maybe that conversion should be made optional to leave the strings > untouched. > > 2017-02-24 1:40 GMT+00:00 Vicente Vera : > >> Hello. I'm trying to get an Org table from an R data frame but data is >> lost in the process. >> >> Here is a MWE. Note that: >> >> - In R every value is a string. "var2" contains no numbers (is a >> character vector). >> >> - Upon conversion to a table Org removes the zero from "var2" last >> value. >> >> ------------------------------ >> >> #+BEGIN_SRC R :session *mwe* :results value table :colnames yes >> tst <- data.frame(var1 = c("a", "b", "c", "d", "e", "f", "g"), >> var2 = c("150", "210", "140", "150", "192", "497", >> "3.350"), >> stringsAsFactors = FALSE) >> tst >> #+END_SRC >> >> #+RESULTS: >> | var1 | var2 | >> |------+------| >> | a | 150 | >> | b | 210 | >> | c | 140 | >> | d | 150 | >> | e | 192 | >> | f | 497 | >> | g | 3.35 | >> >> ------------------------------ >> >> Here's the output as seen in R: >> >> : > tst >> : var1 var2 >> : 1 a 150 >> : 2 b 210 >> : 3 c 140 >> : 4 d 150 >> : 5 e 192 >> : 6 f 497 >> : 7 g 3.350 >> >> Details on the data frame: >> >> : > str(tst) >> : 'data.frame': 7 obs. of 2 variables: >> : $ var1: chr "a" "b" "c" "d" ... >> : $ var2: chr "150" "210" "140" "150" ... >> >> It seems Org knows that the values on column "var2" are numbers and >> converts the strings to numbers, applying some obscure trimming on the >> digits. The "3.350" value needs to be left as is. >> >> > --001a1134e9e4b8c039054984c70d Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hello again, I'm sorry for being noisy.

OK yes,= `org-babel-read' is indeed converting "number strings" tonumbers.

Basically this is what happens:

: (string-to-number= "3.350") =3D> 3.35

To leave cell values unchanged I di= d this clumsy hack:

#+BEGIN_SRC emacs-lisp
=C2=A0 (defun test-oth= er-org-babel-read (cell &optional inhibit-lisp-eval)
=C2=A0=C2=A0=C2= =A0 (if (and (stringp cell) (not (equal cell "")))
=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ;; ;; (or (org-babel--string-to-number ce= ll)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (if (and (not inhibit-lis= p-eval)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (or (member (substring cell 0 1) '(&q= uot;(" "'" "`" "["))
=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (string=3D cell "*this*")))
= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (eval (r= ead cell) t)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (if = (string=3D (substring cell 0 1) "\"")
=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (read cell)=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (progn = (set-text-properties 0 (length cell) nil cell) cell)))
=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 ;; ;; )
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 cell))

=C2= =A0 ;; Override default behavior
=C2=A0 (fset 'org-babel-read 't= est-other-org-babel-read)
#+END_SRC

It would be useful to have a = header argument to prevent this
conversion. Probably somebody else has h= ad the same issue?

2017-02-27 13:49 GMT+00:00 Vicente Vera <vicentemvp@gmail.co= m>:
Probab= ly the issue is related to this function in `ob-core.el'?

org-ba= bel-read: "Convert the string value of CELL to a number if
appropri= ate."

Behind the curtains lies the built-in function `string-to= -number'.

Maybe that conversion should be made optional to leave= the strings
untouched.

2017-02-24 1:40 = GMT+00:00 Vicente Vera <vicentemvp@gmail.com>:
Hello. I'm trying to get an Or= g table from an R data frame but data is
lost in the process.

Her= e is a MWE. Note that:

- In R every value is a string. "var2&qu= ot; contains no numbers (is a
=C2=A0 character vector).

- Upon co= nversion to a table Org removes the zero from "var2" last
=C2= =A0 value.

------------------------------

#+BEGIN_SRC R :sess= ion *mwe* :results value table :colnames yes
=C2=A0 tst <- data.frame= (var1 =3D c("a", "b", "c", "d", &qu= ot;e", "f", "g"),
=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 var2 =3D c("150", "210", "140", = "150", "192", "497", "3.350"),
= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 stringsAsFactors =3D FALSE)
=C2= =A0 tst
#+END_SRC

#+RESULTS:
| var1 | var2 |
|------+------= |
| a=C2=A0=C2=A0=C2=A0 |=C2=A0 150 |
| b=C2=A0=C2=A0=C2=A0 |=C2=A0 2= 10 |
| c=C2=A0=C2=A0=C2=A0 |=C2=A0 140 |
| d=C2=A0=C2=A0=C2=A0 |=C2= =A0 150 |
| e=C2=A0=C2=A0=C2=A0 |=C2=A0 192 |
| f=C2=A0=C2=A0=C2=A0 |= =C2=A0 497 |
| g=C2=A0=C2=A0=C2=A0 | 3.35 |

---------------------= ---------

Here's the output as seen in R:

: > tst
:= =C2=A0=C2=A0 var1=C2=A0 var2
: 1=C2=A0=C2=A0=C2=A0 a=C2=A0=C2=A0 150
= : 2=C2=A0=C2=A0=C2=A0 b=C2=A0=C2=A0 210
: 3=C2=A0=C2=A0=C2=A0 c=C2=A0=C2= =A0 140
: 4=C2=A0=C2=A0=C2=A0 d=C2=A0=C2=A0 150
: 5=C2=A0=C2=A0=C2=A0= e=C2=A0=C2=A0 192
: 6=C2=A0=C2=A0=C2=A0 f=C2=A0=C2=A0 497
: 7=C2=A0= =C2=A0=C2=A0 g 3.350

Details on the data frame:

: > str(ts= t)
: 'data.frame':=C2=A0=C2=A0=C2=A0 7 obs. of=C2=A0 2 variables= :
:=C2=A0 $ var1: chr=C2=A0 "a" "b" "c" &q= uot;d" ...
:=C2=A0 $ var2: chr=C2=A0 "150" "210"= ; "140" "150" ...

It seems Org knows that the va= lues on column "var2" are numbers and
converts the strings to = numbers, applying some obscure trimming on the
digits. The "3.350&q= uot; value needs to be left as is.



--001a1134e9e4b8c039054984c70d--