【问题标题】:Send str() output to View()将 str() 输出发送到 View()
【发布时间】:2019-06-27 01:46:10
【问题描述】:

我正在研究包含 313 个变量的数据集中的变量。 我目前正在使用以下方式将full list of variables 打印到输出屏幕:

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

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

是否有类似于str() 的命令,但允许将输出发送到查看器窗口(@98​​7654325@)?

这将非常有用:

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

输出数据集:varnames、vartype、values_string

这存在吗?

【问题讨论】:

  • %>% 不是基本 R 函数。请在问题正文中列出您正在使用的任何软件包的名称。
  • 看手册,?str,答案是否定的,或者至少不是直接的。来自“值”部分:“出于效率原因,str 不返回任何内容。明显的副作用是输出到终端。”因此,无法从str 直接捕获任何内容。也许您可以将打印语句重定向到临时文件并使用read.table 将其读入...从?View,此函数需要可以强制转换为data.frame 的内容。

标签: r dataframe view metadata


【解决方案1】:

根据您的描述,这里的实现看起来像:https://www.r-bloggers.com/str-implementation-for-data-frames/ 可以工作。它将 str() 的结果输出到 data.frame,然后您可以在 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】:

    您可以使用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
    

    【讨论】:

    • 我知道这大约有 2 年的历史,但这适用于将其存储在 CSV 中(受您的解决方案启发): capture.output(obj %>% str) %>% write.csv("filename .csv")
    【解决方案3】:

    也许你想要类似的东西

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

    【讨论】:

      【解决方案4】:

      这是一个应该可以工作的简单函数:

      str_tbl <- function(x, n=5) {
        data.frame(Variable = names(x),
                   Classe = sapply(x, typeof),
                   Values = sapply(x, function(x) paste0(head(x, n = n), 
                     collapse = ", ")),
                   row.names = NULL)
      }
      

      然后只需执行View(str_tbl(df)) 其中df 是您感兴趣的数据框。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多