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

将str()输出发送到View()

  •  0
  • LucasMation  · 技术社区  · 7 年前

    我正在调查一个有313个变量的数据集中的变量。 我正在打印 full list of variables 到输出屏幕,使用:

    str(df, list.len=ncol(df))
    

    str() 输出非常有用,但是当窗口中有这么多变量时,很难在输出处读取。

    是否有类似的命令 str() ,但这允许将输出发送到查看器窗口( View()

    这将非常有用:

    library(dplyr)    
    d %>% str() %>% View()
    

    输出数据集:varnames,vartype,values\u string

    这个存在吗?

    3 回复  |  直到 7 年前
        1
  •  1
  •   Emily Kothe    7 年前

    根据您的描述,它的实现如下所示: https://www.r-bloggers.com/str-implementation-for-data-frames/ 会有用的。它将str()的结果输出到数据帧然后可以在View()中使用。

    #' Creates a \code{data.frame} version of the str function for data.frames.
    #' 
    #' Note that this function only works with \code{data.frames}. The function
    #' will throw an error for any other object types.
    #' 
    #' @param n the first n element to show
    #' @param width maximum width in characters for the examples to show
    #' @param n.levels the first n levels of a factor to show.
    #' @param width.levels maximum width in characters for the number of levels to show.
    #' @param factor.values function defining how factor examples should be printed.
    #'        Possible values are \code{as.character} or \code{as.integer}.
    #' @export
    #' @examples
    #' data(iris)
    #' str(iris)
    #' strtable(iris)
    #' strtable(iris, factor.values=as.integer)
    strtable <- function(df, n=4, width=60, 
                         n.levels=n, width.levels=width, 
                         factor.values=as.character) {
        stopifnot(is.data.frame(df))
        tab <- data.frame(variable=names(df),
                          class=rep(as.character(NA), ncol(df)),
                          levels=rep(as.character(NA), ncol(df)),
                          examples=rep(as.character(NA), ncol(df)),
                          stringsAsFactors=FALSE)
        collapse.values <- function(col, n, width) {
            result <- NA
            for(j in 1:min(n, length(col))) {
                el <- ifelse(is.numeric(col),
                             paste0(col[1:j], collapse=', '),
                             paste0('"', col[1:j], '"', collapse=', '))
                if(nchar(el) <= width) {
                    result <- el
                } else {
                    break
                }
            }
            if(length(col) > n) {
                return(paste0(result, ', ...'))
            } else {
                return(result)
            }
        }
    
        for(i in seq_along(df)) {
            if(is.factor(df[,i])) {
                tab[i,]$class <- paste0('Factor w/ ', nlevels(df[,i]), ' levels')
                tab[i,]$levels <- collapse.values(levels(df[,i]), n=n.levels, width=width.levels)
                tab[i,]$examples <- collapse.values(factor.values(df[,i]), n=n, width=width)
            } else {
                tab[i,]$class <- class(df[,i])[1]
                tab[i,]$examples <- collapse.values(df[,i], n=n, width=width)
            }
    
        }
    
        class(tab) <- c('strtable', 'data.frame')
        return(tab)
    }
    
    #' Prints the results of \code{\link{strtable}}.
    #' @param x result of code \code{\link{strtable}}.
    #' @param ... other parameters passed to \code{\link{print.data.frame}}.
    #' @export
    print.strtable <- function(x, ...) {
        NextMethod(x, row.names=FALSE, ...)
    }
    
        2
  •  0
  •   Anders Ellern Bilgrau    7 年前

    你可以用 capture.output 为了得到你想要的。但是,它不能很好地与 magrittr s管。以下工作:

    library("magrittr") # The package and some toy data
    obj <- list(a = list(b = list()), c = list(), d = numeric(10))
    
    capture.output(obj %>% str) %>% View
    

    或者选择:

    obj %>% capture.output(str(.)) %>% View
    

    下面的“天真”链接 工作:

    obj %>% str %>% capture.output %>% View
    
        3
  •  0
  •   dww Jarretinha    7 年前

    View(capture.output(str(x <- 1:5)))