代码之家  ›  专栏  ›  技术社区  ›  dnagirl

阻止print()在R中输出列表索引

  •  5
  • dnagirl  · 技术社区  · 15 年前

    我有一个 list 包含六个情节,制作如下:

    voi=c('inadist','smldist','lardist')
    
    plist <- llply(voi, 
        function(v,df,s) {
            list(   
                assign(
                    paste(v,'.violin'), 
                    bwplot(groupname~df[,which(colnames(df)==v)]|fCycle*fPhase, 
                        data=df, 
                        groups=groupname, col=rainbow(1), box.ratio=3,
                        main=paste('Distribution of ', v, ' by Treatment and Cycle'),
                        sub=s, xlab=v, panel=panel.violin)),
                assign(
                    paste(v,'.hexbin'),
                    hexbinplot(df[,which(colnames(df)==v)]~starttime|groupname, 
                        data=df, xlab='Time(s)',main= paste('Distribution of ',v,' by Treatment'),
                        sub=s,ylab=v, aspect=0.5, colramp=redgrad.pal, layout=c(2,4)))
    
                )
        },data,meta$exp_name)
    

    如果我把名单打印出来, print(plist)

    [[1]]
    [[1]][[1]]
    
    [[1]][[2]]
    
    
    [[2]]
    [[2]][[1]]
    
    [[2]][[2]]
    
    
    [[3]]
    [[3]][[1]]
    
    [[3]][[2]]
    

    因为我正在编写一个webapp,所以我需要非常严格地控制控制台输出。到目前为止,在不输出索引的情况下输出绘图的唯一方法如下:

    for(p in plist) 
        for(i in p) 
            print(i)
    

    有没有更有效的方法得到我需要的?

    1 回复  |  直到 12 年前
        1
  •  5
  •   Marek    15 年前

    你可以作弊 capture.output

    dummy <- capture.output(print(plist))
    

    或者不创建新变量

    invisible(capture.output(print(plist)))
    

    require(lattice)
    plist <- list(
        list(bwplot(rnorm(10)),bwplot(rnorm(10))),
        list(bwplot(rnorm(10)),bwplot(rnorm(10))),
        list(bwplot(rnorm(10)),bwplot(rnorm(10)))
    )