emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Vicente Vera <vicentemvp@gmail.com>
To: emacs-orgmode <emacs-orgmode@gnu.org>
Subject: Re: Strings converted to numbers in Org table?
Date: Mon, 27 Feb 2017 15:31:58 +0000	[thread overview]
Message-ID: <CAMfbzvD806CQrFXq=qkKLqXt6xDpTrtiBZqRdEy94frCS-fkww@mail.gmail.com> (raw)
In-Reply-To: <CAMfbzvCw-ysekmBhuUrgNZbS+ji4X-Xg+1pXPKB-4+SvRX5UGg@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2922 bytes --]

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 <vicentemvp@gmail.com>:

> 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 <vicentemvp@gmail.com>:
>
>> 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.
>>
>>
>

[-- Attachment #2: Type: text/html, Size: 4343 bytes --]

  reply	other threads:[~2017-02-27 15:32 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-24  1:40 Strings converted to numbers in Org table? Vicente Vera
2017-02-27 13:49 ` Vicente Vera
2017-02-27 15:31   ` Vicente Vera [this message]
2017-02-27 17:33     ` Nicolas Goaziou
2017-02-27 19:20       ` Vicente Vera
2017-02-27 18:17     ` Charles C. Berry
2017-02-28  7:43       ` Alan Schmitt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAMfbzvD806CQrFXq=qkKLqXt6xDpTrtiBZqRdEy94frCS-fkww@mail.gmail.com' \
    --to=vicentemvp@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).