[BioC] Error in calculating P-values with Genefilter function

Steve Lianoglou lianoglou.steve at gene.com
Tue Jun 11 20:33:53 CEST 2013


More baR tRivia!

On Tue, Jun 11, 2013 at 7:27 AM, James W. MacDonald <jmacdon at uw.edu> wrote:
> And this brings me back to the admonition that you should always do
> something like
>
> class(X)
>
> or
>
> dim(X)
>
> first. Depending on how you are running R, if X really were a 35555 x 7
> matrix or data.frame and you just typed X at the prompt, you will get the
> whole thing output to your screen (or up to the row limit set in options). I
> run R under emacs, and sometimes it isn't possible to get R to stop that
> nonsense without doing a kill command at a terminal prompt.

I've co-opted the code from the data.table package to change how
data.frames are printed to stdout for this very reason. I've
accidentally dumped huge data.frames to the screen often enough to
make me wonder if programming with punch cards wouldn't be a better
alternative than staring at emacs trying to plow through the output
while I furiously tap ctrl-g or ctrl-c or something.

Anyway, the end result is to have R print out data.frames in a very
IRanges/DataFrame like way, actually. Monster data.frames now don't
bother me at all:

R> df <- data.frame(name=sample(letters, 10000, replace=TRUE),
score=rnorm(10000))
R> df ## "NOO!", you say ..
       name      score
1    |    b  1.1804078
2    |    j  0.8143630
3    |    t -0.1430033
4    |    n -1.3588291
5    |    o  2.7989686
   ---
9996 |    h -0.1317207
9997 |    c  0.1645823
9998 |    w  0.5061355
9999 |    a -1.6761684
10000|    c  0.3653244

Below is the code I just have pasted into my ~/.Rprofile to make this
happen. I'm sure there's a better place for it, though, but this works
for 99% of the time that I shoot myself in the foot (except for when
I'm in the `browser()` (debugger), actually -- if someone wants to
offer a fix for that, I'd be greatful ;-)

Without further ado:

## -----------------------------------------------------------------------------
## A saner default to print data.frames from data.table
###############################################################################
## Change print data.frame to be data.table like
format.data.frame <- function (x, ..., justify = "none") {
  format.item <- function(x) {
    if (is.atomic(x))
      paste(c(head(x,6),if(length(x)>6)""),collapse=",")
    else
      paste("<",class(x)[1L],">",sep="")
  }
  do.call("cbind", lapply(x, function(col, ...){
    if (is.list(col))
      col <- sapply(col, format.item)
    format(col, justify=justify, ...)
  }))
}

print.data.frame <- function (x, nrows=100L, digits=NULL, ...) {
  if (nrow(x) == 0L) {
    if (length(x)==0L)
      cat("NULL data.frame\n")
    else
      cat("Empty data.frame (0 rows) of ", length(x),
          " col", if(length(x)>1L) "s", ": ",
          paste(head(names(x),6),collapse=","),
          if (ncol(x)>6) "...", "\n", sep="")
    return()
  }
  printdots<-FALSE
  n <- 5
  if (nrow(x)>nrows) {
    if (missing(nrows)) {
      ##msg<-paste("First",nrows,"rows of",nrow(x),"printed.")
      toprint <- rbind(head(x,n),tail(x,n))
      rn <- c(seq_len(n),seq.int(to=nrow(x),length.out=n))
      printdots <- TRUE
    } else {
      toprint <- head(x,nrows)
      rn <- seq_len(nrows)
    }
  } else {
    toprint <- x
    rn <- seq_len(nrow(x))
  }

  ## Replace idx with rownames
  rn <- rownames(x)[rn]

  toprint<-format.data.frame(toprint, digits=digits, na.encode = FALSE)
  rownames(toprint)<-paste(format(rn,right=TRUE),"|",sep="")

  if (printdots) {
    toprint <- rbind(head(toprint,n),"---"="",tail(toprint,n))
    rownames(toprint) <- format(rownames(toprint),justify="right")
    print(toprint,right=TRUE,quote=FALSE)
    return(invisible())
  }
  if (nrow(toprint)>20L)
    ## repeat colnames at the bottom if over 20 rows so you don't have
to scroll up to see them
    toprint<-rbind(toprint,matrix(names(x),nrow=1))
  print(toprint,right=TRUE,quote=FALSE)
  invisible()
}

############### End code ################

-steve

--
Steve Lianoglou
Computational Biologist
Bioinformatics and Computational Biology
Genentech



More information about the Bioconductor mailing list