1

I am investigating the variables in a dataset with 313 variables. I am currently printing the full list of variables to the output screen using:

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

str() output is very useful but difficult to read at the output when there window with so many variables.

Is there command, similar to str(), but that allows sending the output to the viewer window (View())?

This would be very useful:

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

Outputing a dataset with: varnames, vartype, values_string

Does this exist?

Emily Kothe
  • 842
  • 1
  • 6
  • 17
LucasMation
  • 2,408
  • 2
  • 22
  • 45
  • `%>%` is not a base R function. Please list the names of any packages you are using in the body of your question. – lmo Feb 02 '19 at 20:28
  • Looking at the manual, `?str`, the answer is no, or at least not directly. From the "value" section: "str does not return anything, for efficiency reasons. The obvious side effect is output to the terminal." Thus, nothing can directly be captured from `str`. Perhaps you can redirect the print statement to a temp file and read it in with `read.table`... From `?View`, this function requires something that can be coerced into a data.frame. – lmo Feb 02 '19 at 20:34

4 Answers4

1

Based on your description it looks like the implementation here: https://www.r-bloggers.com/str-implementation-for-data-frames/ would work. It outputs the results of str() to a data.frame which you can then work with in 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, ...)
}
Emily Kothe
  • 842
  • 1
  • 6
  • 17
1

You might use capture.output to get what you want. However, it does not play well with magrittrs pipe. The following works:

library("magrittr") # The package and some toy data
obj <- list(a = list(b = list()), c = list(), d = numeric(10))

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

or alternatively:

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

The following 'naive' chaining does not work:

obj %>% str %>% capture.output %>% View
Anders Ellern Bilgrau
  • 9,928
  • 1
  • 30
  • 37
  • I know this is about 2 years old but this works for storing it in CSV (inspired by your soluton): capture.output(obj %>% str) %>% write.csv("filename.csv") – JeniFav Jan 30 '21 at 19:44
1

Here's a simple function that should work:

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)
}

Then just do View(str_tbl(df)) where df is your dataframe of interest.

MH765
  • 390
  • 3
  • 11
0

Perhaps you want something like

View(capture.output(str(x <- 1:5)))
dww
  • 30,425
  • 5
  • 68
  • 111