From ae44b185e62ab7d712e92841aae740f6fba2a50c Mon Sep 17 00:00:00 2001 From: "Matthijs S. Berends" Date: Wed, 13 Feb 2019 17:14:59 +0100 Subject: [PATCH] fix freq length calculation --- DESCRIPTION | 2 +- R/freq.R | 38 ++++++++++++++++++++------------------ docs/extra.js | 6 +++--- docs/index.html | 2 +- index.md | 2 +- pkgdown/extra.js | 6 +++--- 6 files changed, 29 insertions(+), 27 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 5a6953db..8dbd4116 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: AMR Version: 0.5.0.9017 -Date: 2019-02-12 +Date: 2019-02-13 Title: Antimicrobial Resistance Analysis Authors@R: c( person( diff --git a/R/freq.R b/R/freq.R index 109ddffe..b21ba2ec 100755 --- a/R/freq.R +++ b/R/freq.R @@ -352,6 +352,14 @@ frequency_tbl <- function(x, NAs <- x[is.na(x)] } + if (mult.columns > 0) { + header_list <- list(columns = mult.columns) + } else { + header_list <- list(class = class(x), + mode = mode(x)) + } + header_list$length <- length(x) + if (na.rm == TRUE) { x_class <- class(x) x <- x[!x %in% NAs] @@ -364,13 +372,6 @@ frequency_tbl <- function(x, } x_align <- "l" - if (mult.columns > 0) { - header_list <- list(columns = mult.columns) - } else { - header_list <- list(class = class(x), - mode = mode(x)) - } - if (!is.null(levels(x))) { header_list$levels <- levels(x) header_list$ordered <- is.ordered(x) @@ -381,7 +382,6 @@ frequency_tbl <- function(x, } } - header_list$length <- length(x) header_list$na_length <- length(NAs) header_list$unique <- n_distinct(x) @@ -559,7 +559,7 @@ format_header <- function(x, markdown = FALSE, decimal.mark = ".", big.mark = ", header <- header(x) x_class <- header$class - has_length <- header$length + header$na_length > 0 + has_length <- header$length > 0 # FORMATTING # rsi @@ -633,23 +633,25 @@ format_header <- function(x, markdown = FALSE, decimal.mark = ".", big.mark = ", } # levels if (!is.null(header$levels)) { - n_levels <- header$levels %>% length() - n_levels_list <- header$levels - if (n_levels > 5) { - n_levels_list <- c(n_levels_list[1:5], "...") + if (markdown == TRUE) { + header$levels <- paste0("`", header$levels, "`") } if (header$ordered == TRUE) { - n_levels_list <- paste0(n_levels_list, collapse = " < ") + levels_text <- paste0(header$levels, collapse = " < ") } else { - n_levels_list <- paste0(n_levels_list, collapse = ", ") + levels_text <- paste0(header$levels, collapse = ", ") } - header$levels <- n_levels_list + if (nchar(levels_text) > 70) { + # levels text wider than half the console + levels_text <- paste0(substr(levels_text, 1, 70 - 3), "...") + } + header$levels <- paste0(length(header$levels), ": ", levels_text) header <- header[names(header) != "ordered"] } # length and NAs if (has_length == TRUE) { na_txt <- paste0(header$na_length %>% format(decimal.mark = decimal.mark, big.mark = big.mark), " = ", - (header$na_length / (header$na_length + header$length)) %>% percent(force_zero = TRUE, round = digits, decimal.mark = decimal.mark) %>% + (header$na_length / header$length) %>% percent(force_zero = TRUE, round = digits, decimal.mark = decimal.mark) %>% sub("NaN", "0", ., fixed = TRUE)) if (!na_txt %like% "^0 =") { na_txt <- red(na_txt) @@ -660,7 +662,7 @@ format_header <- function(x, markdown = FALSE, decimal.mark = ".", big.mark = ", } else { na_txt <- "" } - header$length <- paste((header$na_length + header$length) %>% format(decimal.mark = decimal.mark, big.mark = big.mark), + header$length <- paste(format(header$length, decimal.mark = decimal.mark, big.mark = big.mark), na_txt) header <- header[names(header) != "na_length"] diff --git a/docs/extra.js b/docs/extra.js index 10c05985..f8e2d62c 100644 --- a/docs/extra.js +++ b/docs/extra.js @@ -38,9 +38,9 @@ $( document ).ready(function() { if ($(".template-article").length > 0) { $('#sidebar').prepend( '
' + - ' Learn R reading this great book: R for Data Science.' + - '
' + - '
' + + '
' + + ' Learn R reading this great book: R for Data Science.' + + '

' + ' Click to read it online - it was published for free.' + ' ' + '
' + diff --git a/docs/index.html b/docs/index.html index 31493b09..f6aadcd1 100644 --- a/docs/index.html +++ b/docs/index.html @@ -261,7 +261,7 @@

WHONET / EARS-Net

-

We support WHONET and EARS-Net data. Exported files from WHONET can be imported into R and can be analysed easily using this package. For education purposes, we created an example data set WHONET with the exact same structure and a WHONET export file. Furthermore, this package also contains a data set antibiotics with all EARS-Net antibiotic abbreviations, and knows almost all WHONET abbreviations for microorganisms. When using WHONET data as input for analysis, all input parameters will be set automatically.

+

We support WHONET and EARS-Net data. Exported files from WHONET can be imported into R and can be analysed easily using this package. For education purposes, we created an example data set WHONET with the exact same structure as a WHONET export file. Furthermore, this package also contains a data set antibiotics with all EARS-Net antibiotic abbreviations, and knows almost all WHONET abbreviations for microorganisms. When using WHONET data as input for analysis, all input parameters will be set automatically.

Read our tutorial about how to work with WHONET data here.

diff --git a/index.md b/index.md index 85ed77ac..913c1759 100644 --- a/index.md +++ b/index.md @@ -81,7 +81,7 @@ To find out how to conduct AMR analysis, please [continue reading here to get st -We support WHONET and EARS-Net data. Exported files from WHONET can be imported into R and can be analysed easily using this package. For education purposes, we created an [example data set `WHONET`](./reference/WHONET.html) with the exact same structure and a WHONET export file. Furthermore, this package also contains a [data set `antibiotics`](./reference/antibiotics.html) with all EARS-Net antibiotic abbreviations, and knows almost all WHONET abbreviations for microorganisms. When using WHONET data as input for analysis, all input parameters will be set automatically. +We support WHONET and EARS-Net data. Exported files from WHONET can be imported into R and can be analysed easily using this package. For education purposes, we created an [example data set `WHONET`](./reference/WHONET.html) with the exact same structure as a WHONET export file. Furthermore, this package also contains a [data set `antibiotics`](./reference/antibiotics.html) with all EARS-Net antibiotic abbreviations, and knows almost all WHONET abbreviations for microorganisms. When using WHONET data as input for analysis, all input parameters will be set automatically. Read our tutorial about [how to work with WHONET data here](./articles/WHONET.html). diff --git a/pkgdown/extra.js b/pkgdown/extra.js index 10c05985..f8e2d62c 100644 --- a/pkgdown/extra.js +++ b/pkgdown/extra.js @@ -38,9 +38,9 @@ $( document ).ready(function() { if ($(".template-article").length > 0) { $('#sidebar').prepend( '