发送STR()输出到视图()

问题描述 投票:1回答:3

我调查与313个变量的数据集的变量。我目前印刷full list of variables使用输出画面:

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

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

是否有命令,类似于str(),但可以使输出发送到浏览器窗口(View())?

这将是非常有用的:

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

输出数据集:varnames,VARTYPE,字符串值

这是否存在?

r dataframe view metadata
3个回答
1
投票

根据您的描述,它看起来像在这里实现:https://www.r-bloggers.com/str-implementation-for-data-frames/会工作。它输出STR()的结果,以一个data.frame,你可以在查看工作,那么()。

#' 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, ...)
}

0
投票

您可以使用capture.output得到你想要的东西。但是,它不与magrittrs管打好。以下工作:

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

0
投票

也许你想要的东西,像

View(capture.output(str(x <- 1:5)))
© www.soinside.com 2019 - 2024. All rights reserved.