emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: tsd@tsdye.com (Thomas S. Dye)
To: Michael Hannon <jm_hannon@yahoo.com>
Cc: Org-Mode List <emacs-orgmode@gnu.org>,
	Eric Schulte <eric.schulte@gmx.com>
Subject: Re: Babel: communicating irregular data to R source-code block
Date: Mon, 23 Apr 2012 14:23:25 -1000	[thread overview]
Message-ID: <m1vckqx9j6.fsf@tsdye.com> (raw)
In-Reply-To: <1335219898.41851.YahooMailNeo@web161901.mail.bf1.yahoo.com> (Michael Hannon's message of "Mon, 23 Apr 2012 15:24:58 -0700 (PDT)")

Michael Hannon <jm_hannon@yahoo.com> writes:

> Greetings.  I'm sorry to belabor this, but I thought I had found a relatively
> clean way to pass a "ragged" table to an R source-code block.  Simple answer:
> add the "fill=TRUE" option to the read.table function.  Please see the
> appended for the log of an R session that does what I want.
>
> I then tried to do the same thing in an R source-code block:
>
>     #+RESULTS: pascals_triangle
>     | 1 |   |    |    |   |   |
>     | 1 | 1 |    |    |   |   |
>     | 1 | 2 |  1 |    |   |   |
>     | 1 | 3 |  3 |  1 |   |   |
>     | 1 | 4 |  6 |  4 | 1 |   |
>     | 1 | 5 | 10 | 10 | 5 | 1 |
>     
>     
>     #+NAME: sanity-check(sc_input=pascals_triangle)
>     #+BEGIN_SRC R
>     
>     pt <- read.table(sc_input, fill=TRUE)
>     rowSums(pt)
>     
>     #+END_SRC  
>
> Unfortunately, this still results in the "error" that the first line did not
> contain five elements:
>
> <<<<<<<<<<
>> Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,
>> : 
>   line 1 did not have 5 elements
>
> Enter a frame number, or 0 to exit   
>
> 1: read.table("/tmp/babel-3780tje/R-import-37801if", header = FALSE, row.names
> = NULL, sep = "
> 2: scan(file = file, what = what, sep = sep, quote = quote, dec = dec, nmax =
> nrows, skip = 0,
>>>>>>>>>>>
>
> I.e.,it seems that Org is going to do its own "read.table" before even
> looking at the code in the source block.

Yes, I believe this happens when Org assigns values to R variables.

>
> Is there some way to get Org to use the "fill=TRUE" option on a case-by-case
> basis?

I don't think so.  The call to read.table in org-babel-R-assign-elisp
doesn't use the fill option:

        (format "%s <- read.table(\"%s\", header=%s, row.names=%s,
        sep=\"\\t\", as.is=TRUE)"

If I add fill=TRUE to that (on a git branch), then I get this:

#+RESULTS: pascals-triangle
| 1 |   |    |    |   |   |
| 1 | 1 |    |    |   |   |
| 1 | 2 |  1 |    |   |   |
| 1 | 3 |  3 |  1 |   |   |
| 1 | 4 |  6 |  4 | 1 |   |
| 1 | 5 | 10 | 10 | 5 | 1 |

#+NAME: sanity-check
#+HEADER: :var sc_input=pascals-triangle
#+BEGIN_SRC R
sc_input
#+END_SRC

#+RESULTS: sanity-check

| 1 | nil | nil | nil | nil |
| 1 |   1 | nil | nil | nil |
| 1 |   2 |   1 | nil | nil |
| 1 |   3 |   3 | 1   | nil |
| 1 |   4 |   6 | 4   | 1   |
| 1 |   5 |  10 | 10  | 5   |
| 1 | nil | nil | nil | nil |

which isn't correct, but gets past the scan error.

I'm in over my head here, but hope that my curiosity hasn't been too noisy.

All the best,
Tom
>
> Thanks.
>
> -- Mike
>
>
> Appendix: R code that correctly reads and processes a Pascal's triangle
> =======================================================================
>
>
>> system("cat pascal.dat")
> 1
> 1 1
> 1 2 1
> 1 3 3 1
> 1 4 6 4 1
>> 
>> x <- read.table("pascal.dat", fill=TRUE)
>> 
>> x
>   V1 V2 V3 V4 V5
> 1  1 NA NA NA NA
> 2  1  1 NA NA NA
> 3  1  2  1 NA NA
> 4  1  3  3  1 NA
> 5  1  4  6  4  1
>> 
>> y <- as.matrix(x)
>> 
>> y
>      V1 V2 V3 V4 V5
> [1,]  1 NA NA NA NA
> [2,]  1  1 NA NA NA
> [3,]  1  2  1 NA NA
> [4,]  1  3  3  1 NA
> [5,]  1  4  6  4  1
>> 
>> y[is.na(y)] <- 0
>> 
>> y
>      V1 V2 V3 V4 V5
> [1,]  1  0  0  0  0
> [2,]  1  1  0  0  0
> [3,]  1  2  1  0  0
> [4,]  1  3  3  1  0
> [5,]  1  4  6  4  1
>> 
>> dimnames(y)[[2]]=NULL  #### cosmetic change
>> 
>> y
>      [,1] [,2] [,3] [,4] [,5]
> [1,]    1    0    0    0    0
> [2,]    1    1    0    0    0
> [3,]    1    2    1    0    0
> [4,]    1    3    3    1    0
> [5,]    1    4    6    4    1
>> 
>
>

-- 
Thomas S. Dye
http://www.tsdye.com

  parent reply	other threads:[~2012-04-24  0:23 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-21 20:17 Babel: communicating irregular data to R source-code block Michael Hannon
2012-04-22  0:44 ` Thomas S. Dye
2012-04-22 15:58   ` Eric Schulte
2012-04-23 16:46     ` Thomas S. Dye
2012-04-23 15:41       ` Eric Schulte
2012-04-23 19:17         ` Thomas S. Dye
2012-04-23 22:24     ` Michael Hannon
2012-04-23 21:05       ` Eric Schulte
2012-04-24  0:23       ` Thomas S. Dye [this message]
2012-04-23 22:55         ` Eric Schulte
2012-04-24  6:44           ` Thomas S. Dye
2012-04-24  7:07             ` Michael Hannon
2012-04-24 17:18               ` Thomas S. Dye
2012-04-24 19:23                 ` Thomas S. Dye
2012-04-25 23:52               ` Thomas S. Dye
2012-04-26  2:06                 ` Michael Hannon
2012-04-26  6:34                   ` Thomas S. Dye

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=m1vckqx9j6.fsf@tsdye.com \
    --to=tsd@tsdye.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=eric.schulte@gmx.com \
    --cc=jm_hannon@yahoo.com \
    /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).