Home > front end >  How to generate "pretty" output from the str() function in R
How to generate "pretty" output from the str() function in R

Time:01-27

I have a moderate-sized data set that is 1000 rows by 81 columns. I'd like to use the output from str(), but I'd like to present it in a "prettier" way. I've tried things like this:

df %>% str() %>% kableExtra::kbl() %>% kableExtra::kable_minimal()

and

tbl_summary(as.data.frame(str(df)))

but neither works. I'm not married to str() or to any specific package, but that's the kind of summary I'm going for.

In the end, this is intended to generate an HTML file, but I'd like it to work with PDF output as well.

Any ideas on how to do this?

CodePudding user response:

Update II: enter image description here This can be achieved making use of this gist devtools::source_gist('4a0a5ab9fe7e1cf3be0e')

<devtools::source_gist('4a0a5ab9fe7e1cf3be0e')>

  print(strtable(iris, factor.values=as.integer), na.print='') %>% 
    kable() %>% 
    htmlTable()

Update I: you could extend:

data.frame(variable = names(iris),
           class = sapply(iris, typeof),
           levels = sapply(iris, class),
           first_values = sapply(iris, function(x) paste0(head(x),  collapse = ", ")),
           levels_values = sapply(iris, function(x) paste0(unique(x), collapse =", ")),
           row.names = NULL) %>% 
  kable() %>% 
  htmlTable()

enter image description here

First answer: Something like this using iris dataset:

library(knitr)
library(magrittr)
library(htmlTable)

data.frame(variable = names(iris),
           classe = sapply(iris, typeof),
           first_values = sapply(iris, function(x) paste0(head(x),  collapse = ", ")),
           row.names = NULL) %>% 
  kable() %>% 
  htmlTable()

enter image description here

CodePudding user response:

skimr and gt (or kable, or flextable, or DT, or many other table packages) could also work here:

mtcars |>
  skimr::skim() |>
  gt::gt()

enter image description here

  •  Tags:  
  • Related