Skip to content

Commit

Permalink
Develop to master
Browse files Browse the repository at this point in the history
Develop to master
  • Loading branch information
elinw authored Nov 6, 2019
2 parents 032839b + 12662aa commit 722dfa7
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 164 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ warnings_are_errors: true
env:
global:
- NOT_CRAN=false
addons:
apt:
packages:
- libudunits2-dev
- gdal-bin
- libgdal1-dev
- libproj-dev
before_install:
- echo "options(repos = c(CRAN='http://cran.rstudio.com'))" > ~/.Rprofile
r_github_packages:
Expand Down
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ Imports:
tidyr (>= 1.0),
tidyselect (>= 0.2.5)
Suggests:
covr,
extrafont,
rmarkdown,
sf,
testthat (>= 2.0.0),
withr,
covr
withr
License: GPL-3
Encoding: UTF-8
LazyData: true
Expand Down
59 changes: 37 additions & 22 deletions R/skim_with.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#' Set or add the summary functions for a particular type of data
#'
#' While skim is designed around having an opinionated set of defaults, you
#' can use these functions to change the summary statistics that it returns.
#' To do that, provide type you wish to change as an argument to this function,
#' along with a list of named functions that you want to use instead of the
#' defaults.
#' can use this function to change the summary statistics that it returns.
#'
#' `skim_with()` is a closure: a function that returns a new function. This
#' lets you have several skimming functions in a single R session, but it
Expand All @@ -14,7 +11,11 @@
#' You assign values within `skim_with` by using the [sfl()] helper (`skimr`
#' function list). This helper behaves mostly like [dplyr::funs()], but lets
#' you also identify which skimming functions you want to remove, by setting
#' them to `NULL`.
#' them to `NULL`. Assign an `sfl` to each column type that you wish to modify.
#' Functions that summarize all data types, and always return the same type
#' of value, can be assigned to the `base` argument. The default base skimmers
#' compute the number of missing values `n_missing` and the rate of values being
#' complete, i.e. not missing.
#'
#' When `append = TRUE` and local skimmers have names matching the names of
#' entries in the default `skim_function_list`, the values in the default list
Expand Down Expand Up @@ -68,6 +69,7 @@ skim_with <- function(...,
local_skimmers <- validate_assignment(...)

function(data, ...) {
data_name <- rlang::expr_label(substitute(data))
if (!is.data.frame(data)) {
data <- as.data.frame(data)
}
Expand Down Expand Up @@ -110,7 +112,7 @@ skim_with <- function(...,
class = c("skim_df", "tbl_df", "tbl", "data.frame"),
data_rows = nrow(data),
data_cols = ncol(data),
df_name = rlang::expr_label(substitute(data)),
df_name = data_name,
groups = dplyr::groups(data),
base_skimmers = names(base$funs),
skimmers_used = get_skimmers_used(unique_skimmers)
Expand Down Expand Up @@ -193,8 +195,12 @@ get_final_skimmers <- function(column, data, local_skimmers, append) {

if (is.null(locals$funs)) {
if (defaults$skim_type == "default") {
warning("Couldn't find skimmers for class: %s; No user-defined `sfl` ",
"provided. Falling back to `character`.",
msg <- sprintf(
"Couldn't find skimmers for class: %s;",
paste(all_classes, collapse = ", ")
)
warning(msg,
" No user-defined `sfl` provided. Falling back to `character`.",
call. = FALSE
)
data[[column]] <- as.character(data[[column]])
Expand Down Expand Up @@ -284,47 +290,56 @@ mangle_names <- function(skimmers, base_names) {
#' mangle the function names. That way, each set of relevant columns begin
#' with the column name + `_` + our internal delimiter.
#'
#' @param mangled_skimmers The `sfl`'s whose function names have been mangled.
#' @param variable_names The names of columns in the original data, matching a
#' data type, that will be summarized.
#' @param data The original data.
#' @keywords internal
#' @noRd
skim_by_type <- function(mangled, columns, data) {
skim_by_type <- function(mangled_skimmers, variable_names, data) {
UseMethod("skim_by_type", data)
}

#' @export
skim_by_type.grouped_df <- function(mangled, columns, data) {
skim_by_type.grouped_df <- function(mangled_skimmers, variable_names, data) {
group_columns <- dplyr::groups(data)
grouped <- dplyr::group_by(data, !!!group_columns)
skimmed <- dplyr::summarize_at(grouped, columns, mangled$funs)
build_results(skimmed, columns, group_columns)
skimmed <- dplyr::summarize_at(grouped, variable_names, mangled_skimmers$funs)
build_results(skimmed, variable_names, group_columns)
}

#' @export
skim_by_type.data.frame <- function(mangled, columns, data) {
skimmed <- dplyr::summarize_at(data, columns, mangled$funs)
build_results(skimmed, columns, NULL)
skim_by_type.data.frame <- function(mangled_skimmers, variable_names, data) {
skimmed <- dplyr::summarize_at(data, variable_names, mangled_skimmers$funs)
build_results(skimmed, variable_names, NULL)
}

#' Summarize returns a single row data frame, make it tall.
#' @noRd
build_results <- function(skimmed, data_cols, groups) {
if (length(data_cols) > 1) {
build_results <- function(skimmed, variable_names, groups) {
if (length(variable_names) > 1) {
out <- tibble::tibble(
skim_variable = data_cols,
by_variable = purrr::map(data_cols, reshape_skimmed, skimmed, groups)
skim_variable = variable_names,
by_variable = purrr::map(variable_names, reshape_skimmed, skimmed, groups)
)
tidyr::unnest(out, .data$by_variable)
} else {
out <- dplyr::select(
as.data.frame(skimmed),
!!!groups,
tidyselect::contains(NAME_DELIMETER)
)
tibble::tibble(
skim_variable = data_cols,
!!!set_clean_names(skimmed)
skim_variable = variable_names,
!!!set_clean_names(out)
)
}
}

reshape_skimmed <- function(column, skimmed, groups) {
delim_name <- paste0(column, "_", NAME_DELIMETER)
out <- dplyr::select(
skimmed,
as.data.frame(skimmed),
!!!groups,
tidyselect::starts_with(delim_name)
)
Expand Down
14 changes: 7 additions & 7 deletions R/summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ summary.skim_df <- function(object, ...) {
if (is.null(object)) {
stop("dataframe is null.")
}
df_name <- df_name(object)
df_name <- ifelse(df_name %in% c("`.`", ".data"), "Piped data", df_name)
df_name <- gsub("`", "", df_name)
df_name <- ifelse(nchar(df_name) > 25,
paste0(substring(df_name, 1, 25), "..."),
df_name
data_name <- df_name(object)
data_name <- ifelse(data_name %in% c("`.`", ".data"), "Piped data", data_name)
data_name <- gsub("`", "", data_name)
data_name <- ifelse(nchar(data_name) > 25,
paste0(substring(data_name, 1, 25), "..."),
data_name
)

duplicated <- duplicated(object$skim_variable)
Expand All @@ -32,7 +32,7 @@ summary.skim_df <- function(object, ...) {
)

summary_object <- c(
df_name,
data_name,
data_rows(object),
data_cols(object),
" ",
Expand Down
34 changes: 23 additions & 11 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@
}
],
"softwareSuggestions": [
{
"@type": "SoftwareApplication",
"identifier": "covr",
"name": "covr",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=covr"
},
{
"@type": "SoftwareApplication",
"identifier": "extrafont",
Expand All @@ -188,40 +200,40 @@
},
{
"@type": "SoftwareApplication",
"identifier": "testthat",
"name": "testthat",
"version": ">= 2.0.0",
"identifier": "sf",
"name": "sf",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=testthat"
"sameAs": "https://CRAN.R-project.org/package=sf"
},
{
"@type": "SoftwareApplication",
"identifier": "withr",
"name": "withr",
"identifier": "testthat",
"name": "testthat",
"version": ">= 2.0.0",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=withr"
"sameAs": "https://CRAN.R-project.org/package=testthat"
},
{
"@type": "SoftwareApplication",
"identifier": "covr",
"name": "covr",
"identifier": "withr",
"name": "withr",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=covr"
"sameAs": "https://CRAN.R-project.org/package=withr"
}
],
"softwareRequirements": [
Expand Down Expand Up @@ -391,7 +403,7 @@
],
"releaseNotes": "https://github.com/ropensci/skimr/blob/master/NEWS.md",
"readme": "https://github.com/ropensci/skimr/blob/master/README.md",
"fileSize": "4120.816KB",
"fileSize": "3788.726KB",
"contIntegration": [
"https://travis-ci.org/ropenscilabs/skimr",
"https://codecov.io/gh/ropenscilabs/skimr"
Expand Down
11 changes: 6 additions & 5 deletions man/skim_with.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions tests/testthat/helper-expectations.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ expect_NA <- function(object) {

expect_print_matches_file <- function(object,
filename,
skip_on_windows = TRUE) {
skip_on_windows = TRUE,
width = 100) {
if (skip_on_windows) testthat::skip_on_os("windows")
withr::with_options(list(crayon.enabled = FALSE), {
withr::with_options(list(crayon.enabled = FALSE, width = width), {
testthat::expect_known_output(
print(object),
filename,
update = FALSE,
width = 100
width = width
)
})
}
Expand Down
25 changes: 16 additions & 9 deletions tests/testthat/print/smaller.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ Column type frequency:
________________________
Group variables None

── Variable type: factor ───────────────────────────────────────────────────────────────────────────
skim_variable n_missing complete_rate ordered n_unique top_counts
1 Species 0 1 FALSE 3 set: 50, ver: 50, vir: 50
── Variable type: factor ─────────────────────────
skim_variable n_missing complete_rate ordered
1 Species 0 1 FALSE
n_unique top_counts
1 3 set: 50, ver: 50, vir: 50

── Variable type: numeric ──────────────────────────────────────────────────────────────────────────
skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
1 Sepal.Length 0 1 5.84 0.828 4.3 5.1 5.8 6.4 7.9 ▆▇▇▅▂
2 Sepal.Width 0 1 3.06 0.436 2 2.8 3 3.3 4.4 ▁▆▇▂▁
3 Petal.Length 0 1 3.76 1.77 1 1.6 4.35 5.1 6.9 ▇▁▆▇▂
4 Petal.Width 0 1 1.20 0.762 0.1 0.3 1.3 1.8 2.5 ▇▁▇▅▃
── Variable type: numeric ────────────────────────
skim_variable n_missing complete_rate mean
1 Sepal.Length 0 1 5.84
2 Sepal.Width 0 1 3.06
3 Petal.Length 0 1 3.76
4 Petal.Width 0 1 1.20
sd p0 p25 p50 p75 p100 hist
1 0.828 4.3 5.1 5.8 6.4 7.9 ▆▇▇▅▂
2 0.436 2 2.8 3 3.3 4.4 ▁▆▇▂▁
3 1.77 1 1.6 4.35 5.1 6.9 ▇▁▆▇▂
4 0.762 0.1 0.3 1.3 1.8 2.5 ▇▁▇▅▃
6 changes: 2 additions & 4 deletions tests/testthat/test-skim_print.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,8 @@ test_that("Print focused objects appropriately", {
})

test_that("Metadata is stripped from smaller consoles", {
withr::with_options(list(width = 50), {
skimmed <- skim(iris)
expect_print_matches_file(skimmed, "print/smaller.txt")
})
skimmed <- skim(iris)
expect_print_matches_file(skimmed, "print/smaller.txt", width = 50)
})

test_that("Crayon is supported", {
Expand Down
Loading

0 comments on commit 722dfa7

Please sign in to comment.