From mboxrd@z Thu Jan 1 00:00:00 1970 From: Michael Hannon Subject: Babel: communicating irregular data to R source-code block Date: Sat, 21 Apr 2012 13:17:52 -0700 (PDT) Message-ID: <1335039472.9075.YahooMailNeo@web161901.mail.bf1.yahoo.com> Reply-To: Michael Hannon Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggs.gnu.org ([208.118.235.92]:40516) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SLgkq-0004Tr-KZ for emacs-orgmode@gnu.org; Sat, 21 Apr 2012 16:18:04 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SLgkm-0003Im-2x for emacs-orgmode@gnu.org; Sat, 21 Apr 2012 16:18:00 -0400 Received: from nm20.bullet.mail.bf1.yahoo.com ([98.139.212.179]:45661) by eggs.gnu.org with smtp (Exim 4.71) (envelope-from ) id 1SLgkl-0003Ia-Pz for emacs-orgmode@gnu.org; Sat, 21 Apr 2012 16:17:56 -0400 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: Org-Mode List =0A=0AGreetings.=A0 I'm sitting in on a weekly, informal, "brown-bag" semin= ar on data=0Atechnologies in statistics.=A0 There are more people attending= the seminar than=0Athere are weeks in which to give talks, so I may get by= with being my usual,=0Apassive-slug self.=0A=0ABut I thought it might be u= seful to have a contingency plan and decided that=0Agiving a brief talk abo= ut Babel might be useful/instructive.=A0 I thought (and=0Athink) that mushi= ng together (with attribution) some of the content of the=0Apaper [1] by Th= e Gang of Four and the content of Eric's talk [2] might be a=0Agood approac= h.=A0 (BTW, if this isn't legal, desirable, permissible, etc., this=0Awould= be a good time to tell me.)=0A=0AI liked the Pascal's Triangle example (wh= ich morphed from elisp to Python, or=0Avice versa, in the two references), = but I was afraid that the elisp routine=0A"pst-check", used as a check on t= he correctness of the previously-generated=0APascal's triangle, might be to= o esoteric for this audience, not to mention me.=0A(The recursive Fibonacci= function is virtually identical in all languages,=0Abut the second part is= more obscure.)=0A=0AI thought it should be possible to use R to do the sam= e sanity check, as R=0Awould be much more-familiar to this audience (and it= s use would still=0Ademonstrate the meta-language feature of Babel).=0A=0AU= nfortunately, I haven't been able to find a way to communicate the output o= f=0Athe Pascal's Triangle example to an R source-code block.=A0 The gist of= the=0Aproblem seems to be that regardless of how I try to grab the data (s= can,=0AreadLines, etc.) Babel always ends up trying to read a data frame (t= able) and=0AI get an error similar to:=0A=0A<<<<<<=0A> Error in scan(file, = what, nmax, sep, dec, quote, skip, nlines, na.strings,=0A> : line 1 did not= have 5 elements=0A=0AEnter a frame number, or 0 to exit=A0=A0 =0A=0A1: rea= d.table("/tmp/babel-3780tje/R-import-3780Akj", header =3D FALSE, row.names= =0A=3D NULL, sep =3D "=0A>>>>>>=0A=0AIf I construct a table "by hand" with = all of the cells occupied, everything=0Agoes OK.=A0 For instance:=0A=0A<<<<= <<=0A#+TBLNAME: some-junk=0A| 1 | 0 | 0 | 0 |=0A| 1 | 1 | 0 | 0 |=0A| 1 | 2= | 1 | 0 |=0A| 1 | 3 | 3 | 1 | =0A=0A#+NAME: read-some-junk(sj_input=3Dsome= -junk)=0A#+BEGIN_SRC R=0A=0ArowSums(sj_input)=0A=0A#+END_SRC=A0 =0A=0A#+RES= ULTS: read-some-junk=0A| 1 |=0A| 2 |=0A| 4 |=0A| 8 |=0A>>>>>>=0A=0ABut the = following gives the kind of error I described above:=0A=0A<<<<<<=0A#+name: = pascals_triangle=0A#+begin_src python :var n=3D5 :exports none :return pasc= als_triangle(5)=0Adef pascals_triangle(n):=0A=A0=A0=A0 if n =3D=3D 0:=0A=A0= =A0=A0=A0=A0=A0=A0 return [[1]]=0A=A0=A0=A0 prev_triangle =3D pascals_trian= gle(n-1)=0A=A0=A0=A0 prev_row =3D prev_triangle[n-1]=0A=A0=A0=A0 this_row = =3D map(sum, zip([0] + prev_row, prev_row + [0]))=0A=A0=A0=A0 return prev_t= riangle + [this_row]=0A=0Apascals_triangle(n)=0A#+end_src=0A=0A#+RESULTS: p= ascals_triangle=0A| 1 |=A0=A0 |=A0=A0=A0 |=A0=A0=A0 |=A0=A0 |=A0=A0 |=0A| 1= | 1 |=A0=A0=A0 |=A0=A0=A0 |=A0=A0 |=A0=A0 |=0A| 1 | 2 |=A0 1 |=A0=A0=A0 |= =A0=A0 |=A0=A0 |=0A| 1 | 3 |=A0 3 |=A0 1 |=A0=A0 |=A0=A0 |=0A| 1 | 4 |=A0 6= |=A0 4 | 1 |=A0=A0 |=0A| 1 | 5 | 10 | 10 | 5 | 1 |=0A=0A#+name: pst-checkR= (pas_inputs=3Dpascals_triangle)=0A#+BEGIN_SRC R=0A=0ArowSums(pas_inputs)=0A= =0A#+END_SRC=0A>>>>>>=0A=0ANote that I don't really want to do rowSums in t= his case.=A0 I'm just trying to=0Ademonstrate the error.=0A=0AOf course, it= 's clear that the first line does NOT contain five elements, nor=0Adoes the= second, etc., as all of the above-diagonal elements are blanks.=0A=0ABut I= 've been unable to find an R input function that doesn't end up treating=0A= the source data as a table, i.e., in the context of Babel source blocks -- = R=0Ais "happy" to read a lower-diagonal structure.=A0 See the appendix for = an=0Aexample.=0A=0AAny suggestions?=A0 Note that I'm happy to acknowledge t= hat my own ignorance of=0AR and/or Babel might be the source of the problem= .=A0 If so, please enlighten=0Ame.=0A=0AThanks.=0A=0A-- Mike=0A=0A[1] http:= //www.jstatsoft.org/v46/i03=0A[2] https://github.com/eschulte/babel-present= ation=0A=0A<<<<<<=0Aappendix=0A--------=0A=0A=0A$ cat pascal.dat=0A1=0A1 1= =0A1 2 1=0A1 3 3 1=0A1 4 6 4 1=0A=0A$ R --vanilla < pascal.R=0A=0AR version= 2.15.0 (2012-03-30)=0ACopyright (C) 2012 The R Foundation for Statistical = Computing=0AISBN 3-900051-07-0=0APlatform: x86_64-redhat-linux-gnu (64-bit)= =0A.=0A.=0A.=0A=0A> x <- readLines("pascal.dat")=0A> x=0A[1] "1"=A0=A0=A0= =A0=A0=A0=A0=A0 "1 1"=A0=A0=A0=A0=A0=A0 "1 2 1"=A0=A0=A0=A0 "1 3 3 1"=A0=A0= "1 4 6 4 1"=0A> str(x)=0A=A0chr [1:5] "1" "1 1" "1 2 1" "1 3 3 1" "1 4 6 4= 1"=0A> =0A> y <- scan("pascal.dat")=0ARead 15 items=0A> y=0A=A0[1] 1 1 1 1= 2 1 1 3 3 1 1 4 6 4 1=0A> str(y)=0A=A0num [1:15] 1 1 1 1 2 1 1 3 3 1 ...= =0A> =0A> z <- read.table("pascal.dat", header=3DFALSE)=0AError in scan(fil= e, what, nmax, sep, dec, quote, skip, nlines, na.strings,=A0 : =0A=A0 line = 1 did not have 5 elements=0ACalls: read.table -> scan=0AExecution halted