[Rd] Tab formatting in dummy.coef.R

Martin Maechler maechler at stat.math.ethz.ch
Fri Jan 3 12:00:47 CET 2014


>>>>> Paul Johnson <pauljohn32 at gmail.com>
>>>>>     on Thu, 2 Jan 2014 22:29:29 -0600 writes:

    > Happy New Year I recognize this is a low priority issue,
    > but... I'll fix it if you let me.

    > There are some TABs where R style calls for 4 spaces. 

This is your not quite correct interpretation. 
The R internal manual you mention below talks about TABs in
specific places in *.Rd files only.

*.R (and *.c even more) use TABs in many places, and all of R core have
their Emacs / vi / ... settings accordingly.

Happy New Year,  indeed, to all contributors and readers of 'R-devel'!
Martin

--
Martin Maechler, ETH Zurich  &  R core team


    > For example R-3.0.2/src/library/stats/R/dummy.coef.R.

    > I never noticed this until today, when I was stranded on a
    > deserted island with only the R source code and a Swiss
    > Army knife (vi). Now I realize my ~.vimrc has tabstop set
    > at 2, and it makes tab indentations in R source "go
    > backwards".  if you are strictly following R Internals 9
    > "R Coding Standards", the TABs should be replaced with
    > spaces, I shouldn't have to reconfigure the editor.

    > Here's the way it looks in my vi

    > dummy.coef.aovlist <- function(object, use.na = FALSE,
    > ...)  { Terms <- terms(object, specials="Error") err <-
    > attr(Terms,"specials")$Error - 1 tl <- attr(Terms,
    > "term.labels")[-err] int <- attr(Terms, "intercept") facs
    > <- attr(Terms, "factors")[-c(1,1+err), -err, drop=FALSE]
    > vars <- rownames(facs) xl <- attr(object, "xlevels")
    > if(!length(xl)) { # no factors in model
    > return(as.list(coef(object))) } nxl <- setNames(rep.int(1,
    > length(vars)), vars) tmp <- unlist(lapply(xl, length))
    > nxl[names(tmp)] <- tmp lterms <- apply(facs, 2L,
    > function(x) prod(nxl[x > 0])) nl <- sum(lterms) args <-
    > setNames(vector("list", length(vars)), vars) for(i in
    > vars) args[[i]] <- if(nxl[[i]] == 1) rep.int(1, nl) else
    > factor(rep.int(xl[[i]][1L], nl), levels = xl[[i]]) dummy
    > <- do.call("data.frame", args) pos <- 0 rn <- rep.int(tl,
    > lterms) rnn <- rep.int("", nl) for(j in tl) { i <-
    > vars[facs[, j] > 0] ifac <- i[nxl[i] > 1] if(length(ifac)
    > == 0L) { # quantitative factor rnn[pos + 1] <- j } else
    > if(length(ifac) == 1L) { # main effect dummy[
    > pos+1L:lterms[j], ifac ] <- xl[[ifac]] rnn[
    > pos+1L:lterms[j] ] <- as.character(xl[[ifac]]) [... snip
    > ....]

    > The patch looks like this in my vi:

    > *** dummy.coef.R 2013-03-05 17:02:23.000000000 -0600 ---
    > dummy.coef-indent.R 2014-01-02 21:49:25.828163544 -0600
    > ***************
    > *** 28,35 **** Terms <- delete.response(Terms) vars <-
    > all.vars(Terms) xl <- object$xlevels !  if(!length(xl)) {
    > # no factors in model !  return(as.list(coef(object))) }
    > nxl <- setNames(rep.int(1, length(vars)), vars) tmp <-
    > unlist(lapply(xl, length)) ## ?? vapply(xl, length, 1L)
    > --- 28,35 ---- Terms <- delete.response(Terms) vars <-
    > all.vars(Terms) xl <- object$xlevels !  if(!length(xl)) {
    > # no factors in model !  return(as.list(coef(object))) }
    > nxl <- setNames(rep.int(1, length(vars)), vars) tmp <-
    > unlist(lapply(xl, length)) ## ?? vapply(xl, length, 1L)
    > ***************
    > *** 38,64 **** nl <- sum(lterms) args <-
    > setNames(vector("list", length(vars)), vars) for(i in
    > vars) !  args[[i]] <- if(nxl[[i]] == 1) rep.int(1, nl) !
    > else factor(rep.int(xl[[i]][1L], nl), levels = xl[[i]])
    > dummy <- do.call("data.frame", args) pos <- 0 rn <-
    > rep.int(tl, lterms) rnn <- rep.int("", nl) for(j in tl) {
    > !  i <- vars[facs[, j] > 0] !  ifac <- i[nxl[i] > 1] !
    > if(length(ifac) == 0L) { # quantitative factor !
    > rnn[pos+1] <- j !  } else if(length(ifac) == 1L) { # main
    > effect !  dummy[ pos+1L:lterms[j], ifac ] <- xl[[ifac]] !
    > rnn[ pos+1L:lterms[j] ] <- as.character(xl[[ifac]]) !  }
    > else { # interaction !  tmp <- expand.grid(xl[ifac]) !
    > dummy[ pos+1L:lterms[j], ifac ] <- tmp !  rnn[
    > pos+1L:lterms[j] ] <- !  apply(as.matrix(tmp), 1L,
    > function(x) paste(x, collapse=":")) !  } !  pos <- pos +
    > lterms[j] } ## some terms like poly(x,1) will give
    > problems here, so allow ## NaNs and set to NA afterwards.
    > --- 38,64 ---- nl <- sum(lterms) args <-
    > setNames(vector("list", length(vars)), vars) for(i in
    > vars) !  args[[i]] <- if(nxl[[i]] == 1) rep.int(1, nl) !
    > else factor(rep.int(xl[[i]][1L], nl), levels = xl[[i]])
    > dummy <- do.call("data.frame", args) pos <- 0 rn <-
    > rep.int(tl, lterms) rnn <- rep.int("", nl) for(j in tl) {
    > !  i <- vars[facs[, j] > 0] !  ifac <- i[nxl[i] > 1] !
    > if(length(ifac) == 0L) { # quantitative factor !
    > rnn[pos+1] <- j !  } else if(length(ifac) == 1L) { # main
    > effect !  dummy[ pos+1L:lterms[j], ifac ] <- xl[[ifac]] !
    > rnn[ pos+1L:lterms[j] ] <- as.character(xl[[ifac]]) !  }
    > else { # interaction !  tmp <- expand.grid(xl[ifac]) !
    > dummy[ pos+1L:lterms[j], ifac ] <- tmp !  rnn[
    > pos+1L:lterms[j] ] <- !  apply(as.matrix(tmp), 1L,
    > function(x) paste(x, collapse=":")) !  } !  pos <- pos +
    > lterms[j] } [...snip...]

    > I could make a patch that applies at the top level, maybe
    > one for each library. If one of you will consider using
    > it.

    > Again, I recognize this is a small thing, the editor can
    > be adjusted to avoid it. But it shouldn't be necessary.

    > PJ
    > -- 
    > Paul E. Johnson Professor, Political Science
    > Assoc. Director 1541 Lilac Lane, Room 504 Center for
    > Research Methods University of Kansas University of Kansas
    > http://pj.freefaculty.org http://quant.ku.edu

    > 	[[alternative HTML version deleted]]

    > ______________________________________________
    > R-devel at r-project.org mailing list
    > https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list