Does anyone have code that will methodically process R sourcecode, turning T's into TRUE and F's into FALSE? I got bored doing this by hand, after the first 30-odd functions-- there are hundreds left to do. I don't want to simply deparse everything, because that would destroy my beautiful formatting. The reason it's not trivial, is that comment lines, quotes, and split lines need to be kept track of. There are no syntax errors in the code (i.e. it all parses OK into functions). The absolute ideal would be if the code itself was in R (because I need to run this from R), but presumably if there was a Perl script, I could launch that from within R too. FWIW I'm running Windows 2000, and hovering between R1.5.1 and R1.6.0. Thanks for any help Mark ******************************* Mark Bravington CSIRO (CMIS) PO Box 1538 Castray Esplanade Hobart TAS 7001 phone (61) 3 6232 5118 fax (61) 3 6232 5012 Mark.Bravington at csiro.au -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>>>>> "Mark" == Mark Bravington <Mark.Bravington at csiro.au> >>>>> on Fri, 18 Oct 2002 14:58:04 +1100 writes:Mark> Does anyone have code that will methodically process R Mark> sourcecode, turning T's into TRUE and F's into FALSE? Mark> I got bored doing this by hand, after the first 30-odd Mark> functions-- there are hundreds left to do. I don't Mark> want to simply deparse everything, because that would Mark> destroy my beautiful formatting. Same boat ("beautiful formatting") here. Yes, ESS has had code for several year to do this {CC to ESS-help, since people there might be interested additionally} : M-x R-fix-T-F (guess who wrote it) which does it very quickly for one file. Note that the emacs-lisp code we put in ESS is somewhat smart; we only replace T/F when *not* inside a string or comment. If anybody has time & knowledge to translate the Emacs-Lisp code to perl or python or... we could provide it (from CRAN or so) : ;;;--------------------------------------------------------------------------- (defun R-fix-T-F (&optional from quietly) "Fix T/F into TRUE and FALSE --- CAUTIOUSLY" (interactive "d\nP"); point and prefix (C-u) (save-excursion (goto-char from) (ess-rep-regexp "\\(\\([][=,()]\\|<-\\|_\\) *\\)T\\>" "\\1TRUE" 'fixcase nil (not quietly)) (goto-char from) (ess-rep-regexp "\\(\\([][=,()]\\|<-\\|_\\\) *\\)F\\>" "\\1FALSE" 'fixcase nil (not quietly)))) (defun ess-rep-regexp (regexp to-string &optional fixedcase literal verbose) "Instead of (replace-regexp..) -- do NOT replace in strings or comments. If FIXEDCASE is non-nil, do *not* alter case of replacement text. If LITERAL is non-nil, do *not* treat `\\' as special. If VERBOSE is non-nil, (message ..) about replacements." (let ((case-fold-search (and case-fold-search (not fixedcase))); t <==> ignore case in search (pl) (p)) (while (setq p (re-search-forward regexp nil t)) (cond ((not (inside-string/comment-p (1- p))) (if verbose (let ((beg (match-beginning 0))) (message "(beg,p)= (%d,%d) = %s" beg p (buffer-substring beg p) ))) (replace-match to-string fixedcase literal) ;;or (if verbose (setq pl (append pl (list p)))) ))) ;;or (if (and verbose pl) ;;or (message "s/%s/%s/ at %s" regexp to-string pl)) ) ) (defun inside-string/comment-p (pos) "Return non-nil if POSition [defaults to (point) is inside string or comment (according to syntax). NOT OKAY for multi-line comments!!" ;;FIXME (defun S-calculate-indent ..) in ./essl-s.el can do that ... (interactive "d");point by default (let ((pps (save-excursion (parse-partial-sexp (save-excursion (beginning-of-line) (point)) pos)))) (or (nth 3 pps) (nth 4 pps)))); 3: string, 4: comment ;;;--------------------------------------------------------------------------- Martin Maechler <maechler at stat.math.ethz.ch> http://stat.ethz.ch/~maechler/ Seminar fuer Statistik, ETH-Zentrum LEO C16 Leonhardstr. 27 ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND phone: x-41-1-632-3408 fax: ...-1228 <>< -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
I don't think we have a complete tool. You do really need to parse the code, and relating the parsed code to your `beautiful formatting' is the problem. When I kept common S/R source code, I used a Perl script for the bulk of this. It is conservative, and does not process lines with quoted expressions at all. Basically it was s/=(\s*)F([^A-Za-z0-9\.\"])/=$1FALSE$2/go; s/=(\s*)T([^A-Za-z0-9\.\"])/=$1TRUE$2/go; s/\{T\}/{TRUE}/go; s/\{F\}/{FALSE}/go; and that correctly converted my source code and help files. On Fri, 18 Oct 2002 Mark.Bravington at csiro.au wrote:> Does anyone have code that will methodically process R sourcecode, turning > T's into TRUE and F's into FALSE? I got bored doing this by hand, after the > first 30-odd functions-- there are hundreds left to do. I don't want to > simply deparse everything, because that would destroy my beautiful > formatting. > > The reason it's not trivial, is that comment lines, quotes, and split lines > need to be kept track of. There are no syntax errors in the code (i.e. it > all parses OK into functions). > > The absolute ideal would be if the code itself was in R (because I need to > run this from R), but presumably if there was a Perl script, I could launch > that from within R too. > > FWIW I'm running Windows 2000, and hovering between R1.5.1 and R1.6.0. > > Thanks for any help > > Mark > > ******************************* > > Mark Bravington > CSIRO (CMIS) > PO Box 1538 > Castray Esplanade > Hobart > TAS 7001 > > phone (61) 3 6232 5118 > fax (61) 3 6232 5012 > Mark.Bravington at csiro.au > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Mark wrote:> Does anyone have code that will methodically process R sourcecode,turning> T's into TRUE and F's into FALSE? I got bored doing this by hand,after the> first 30-odd functions-- there are hundreds left to do. I don't wantto> simply deparse everything, because that would destroy my beautiful > formatting.Here is another solution based on an R function. You can use it to change a name of a variable: rename <- function(name,oldname="T",newname="TRUE"){ # pw 10/02 abc1<-c("",".",LETTERS,letters); abc2<-c(abc1,as.character(0:9)) dump(name); fns<-scan("dumpdata.R","",sep="\n") if(0==length(matches<-grep(oldname,fns))) return("no changes") match.lines<-fns[matches]; changes<-0 for(i in 1:length(match.lines)){ line<-strsplit(paste("",match.lines[i],""),oldname)[[1]] h1<-sapply(line,function(x)any(substring(x,1, 1) ==abc2)) h2<-sapply(line,function(x)any(substring(x,nchar(x),nchar(x))==abc1)) h <- h1[-1] | h2[-length(h2)]; changes<-changes+sum(!h) line<-paste(rbind(c("",c(newname,oldname)[1+h]),line)[-1],collapse="") match.lines[i]<-substring(line,2,nchar(line)-1) } fns[matches]<-lapply(match.lines,paste,collapse="") eval(parse(text=unlist(fns)),envir=sys.parent()) return(paste(changes,"changes")) } Example:> rename("rename","oldname","OLD")Read 18 items [1] "4 changes"> renamefunction(name,OLD="T",newname="TRUE"){ # pw 10/02 abc1<-c("",".",LETTERS,letters); abc2<-c(abc1,as.character(0:9)) dump(name); fns<-scan("dumpdata.R","",sep="\n") if(0==length(matches<-grep(OLD,fns))) return("no changes") match.lines<-fns[matches]; changes<-0 for(i in 1:length(match.lines)){ line<-strsplit(paste("",match.lines[i],""),OLD)[[1]] h1<-sapply(line,function(x)any(substring(x,1, 1) ==abc2)) h2<-sapply(line,function(x)any(substring(x,nchar(x),nchar(x))==abc1)) h <- h1[-1] | h2[-length(h2)]; changes<-changes+sum(!h) line<-paste(rbind(c("",c(newname,OLD)[1+h]),line)[-1],collapse="") match.lines[i]<-substring(line,2,nchar(line)-1) } fns[matches]<-lapply(match.lines,paste,collapse="") eval(parse(text=unlist(fns)),envir=sys.parent()) return(paste(changes,"changes")) } Peter Wolf -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 All, I think there is a small bug in sd when the argument is a dataframe and there are missing values:> x <- data.frame(matrix(rnorm(12,0,1), nrow=4, ncol=3)) > x[1,1] <- NA > sd(x)Error in var(x, na.rm = na.rm) : missing observations in cov/cor> sd(x, na.rm=TRUE)Error in var(x, na.rm = na.rm) : missing observations in cov/cor> sapply(x, sd, na.rm=TRUE)X1 X2 X3 1.0308198 0.4817945 1.5692881> version_ platform i686-pc-linux-gnu arch i686 os linux-gnu system i686, linux-gnu status major 1 minor 6.0 year 2002 month 10 day 01 language R The way to fix it should be to replace the lines: sapply(x, sd) in the definition of the sd function with sapply(x, sd, na.rm=na.rm) Best, Claudio Agostinelli -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._