From mboxrd@z Thu Jan 1 00:00:00 1970 From: tsd@tsdye.com (Thomas S. Dye) Subject: Re: Babel: communicating irregular data to R source-code block Date: Mon, 23 Apr 2012 14:23:25 -1000 Message-ID: References: <1335039472.9075.YahooMailNeo@web161901.mail.bf1.yahoo.com> <87ipgrn4by.fsf@gmx.com> <1335219898.41851.YahooMailNeo@web161901.mail.bf1.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggs.gnu.org ([208.118.235.92]:37854) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SMTXg-0004kO-9r for emacs-orgmode@gnu.org; Mon, 23 Apr 2012 20:23:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SMTXd-0006sk-Np for emacs-orgmode@gnu.org; Mon, 23 Apr 2012 20:23:39 -0400 Received: from oproxy5-pub.bluehost.com ([67.222.38.55]:40581) by eggs.gnu.org with smtp (Exim 4.71) (envelope-from ) id 1SMTXd-0006s2-Ef for emacs-orgmode@gnu.org; Mon, 23 Apr 2012 20:23:37 -0400 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)") 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-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Michael Hannon Cc: Org-Mode List , Eric Schulte Michael Hannon writes: > Greetings.=C2=A0 I'm sorry to belabor this, but I thought I had found a r= elatively > clean way to pass a "ragged" table to an R source-code block.=C2=A0 Simpl= e answer: > add the "fill=3DTRUE" option to the read.table function.=C2=A0 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: > > =C2=A0=C2=A0=C2=A0 #+RESULTS: pascals_triangle > =C2=A0=C2=A0=C2=A0 | 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=C2=A0=C2=A0 | 1 | 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 | 1 | 2 |=C2=A0 1 |=C2=A0=C2=A0=C2=A0 |=C2=A0=C2=A0 |= =C2=A0=C2=A0 | > =C2=A0=C2=A0=C2=A0 | 1 | 3 |=C2=A0 3 |=C2=A0 1 |=C2=A0=C2=A0 |=C2=A0=C2= =A0 | > =C2=A0=C2=A0=C2=A0 | 1 | 4 |=C2=A0 6 |=C2=A0 4 | 1 |=C2=A0=C2=A0 | > =C2=A0=C2=A0=C2=A0 | 1 | 5 | 10 | 10 | 5 | 1 | > =C2=A0=C2=A0 =C2=A0 > =C2=A0=C2=A0 =C2=A0 > =C2=A0=C2=A0=C2=A0 #+NAME: sanity-check(sc_input=3Dpascals_triangle) > =C2=A0=C2=A0=C2=A0 #+BEGIN_SRC R > =C2=A0=C2=A0 =C2=A0 > =C2=A0=C2=A0=C2=A0 pt <- read.table(sc_input, fill=3DTRUE) > =C2=A0=C2=A0=C2=A0 rowSums(pt) > =C2=A0=C2=A0 =C2=A0 > =C2=A0=C2=A0=C2=A0 #+END_SRC =C2=A0 > > 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.string= s, >> :=20 > =C2=A0 line 1 did not have 5 elements > > Enter a frame number, or 0 to exit=C2=A0 =C2=A0 > > 1: read.table("/tmp/babel-3780tje/R-import-37801if", header =3D FALSE, ro= w.names > =3D NULL, sep =3D " > 2: scan(file =3D file, what =3D what, sep =3D sep, quote =3D quote, dec = =3D dec, nmax =3D > nrows, skip =3D 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=3DTRUE" 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=3D%s, row.names=3D%s, sep=3D\"\\t\", as.is=3DTRUE)" If I add fill=3DTRUE 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=3Dpascals-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 > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > >> system("cat pascal.dat") > 1 > 1 1 > 1 2 1 > 1 3 3 1 > 1 4 6 4 1 >>=20 >> x <- read.table("pascal.dat", fill=3DTRUE) >>=20 >> x > =C2=A0 V1 V2 V3 V4 V5 > 1=C2=A0 1 NA NA NA NA > 2=C2=A0 1=C2=A0 1 NA NA NA >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