Hi to all R/S gurus, I am developping a library to export R/S objects to HTML (there was developments of this library past year, by myself and Mathieu Ros, but we never finished it). For that, I wrote HTML.* functions handled by the generic method HTML. At this step, allmost all objects exportations are possible: matrix, atomic, data.frame, functions and even plots... but I still don't succeed to export generic lists. Does it exist a function to recursively run through a list and apply a function only to terminal nodes? I tried to sapply my HTML.list function to a list and expected that it recursively worked but it's not the case. Thank you very much! Eric Lecoutre PS: I am ready to send my actual code to any interested person. At this step of development, the main function does create a new prompt, so that each command entered is evaluated both in standard output and in HTML. ?------------------------?------------------------------------------------? | Eric Lecoutre | Statistics | | Voie du Roman Pays, 20 | Teaching assistant / Consultant | | 1348 Louvain-La-Neuve | Universit? de Louvain-la-Neuve | | Belgique | lecoutre at stat.ucl.ac.be | | (+32) (0)10 47 30 50 | http://www.stat.ucl.ac.be/ISpersonnel/lecoutre/| ?------------------------?------------------------------------------------? | We need statistical thinking, not rituals - Gigerenzer | ?-------------------------------------------------------------------------? -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Dear Eric, At 01:23 PM 6/10/2002 +0100, Eric Lecoutre wrote:>Does it exist a function to recursively run through a list and apply a >function only to terminal nodes? >I tried to sapply my HTML.list function to a list and expected that it >recursively worked but it's not the case.I have the feeling that I'm reinventing the wheel -- I think that I recall a simple way to flatten a list but I can't find it (or I may just be having a Lisp flashback), and I don't think that anyone else has posted an answer to your question. In any event, here's a simple-minded function that flattens a list, along with an example of its use. > flatten <- function(x){ + result <- NULL + for(i in seq(along=x)) { + if (any(sapply(x[[i]], is.list))) Recall(x[[i]]) + else result <- c(result, if (is.list(x[[i]])) x[[i]] else list(x[[i]])) + } + return(result) + } > > lst <- list(lst1=list(a=1:3, b=4:6), lst2=list(c=7:9, d=10:12), e=13:15) > flat <- flatten(lst) > flat $a [1] 1 2 3 $b [1] 4 5 6 $c [1] 7 8 9 $d [1] 10 11 12 [[5]] [1] 13 14 15 > sapply(flat, sum) a b c d 6 15 24 33 42 Maybe this will help. John ----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox ----------------------------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._