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

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.

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

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
>

  parent reply	other threads:[~2012-04-23 22:25 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 [this message]
2012-04-23 21:05       ` Eric Schulte
2012-04-24  0:23       ` Thomas S. Dye
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=1335219898.41851.YahooMailNeo@web161901.mail.bf1.yahoo.com \
    --to=jm_hannon@yahoo.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=eric.schulte@gmx.com \
    --cc=tsd@tsdye.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).