Skip to content

Commit

Permalink
Merge pull request #196 from Crunch-io/master
Browse files Browse the repository at this point in the history
Recreating develop
  • Loading branch information
1beb authored Sep 21, 2020
2 parents 828f0bb + bc0b91a commit f0d839d
Show file tree
Hide file tree
Showing 136 changed files with 24,932 additions and 1,525 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Add kableExtra
run: Rscript -e 'install.packages("kableExtra")'
run: Rscript -e 'devtools::install_github("haozhu233/kableExtra")'
- name: CRAN crunch
run: Rscript -e 'install.packages("crunch")'
- name: Build
Expand Down
12 changes: 7 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Description: In order to generate custom survey reports, this package provides
'banners' (cross-tabulations) of datasets in the Crunch
(<https://crunch.io/>) web service. Reports can be written in 'PDF' format
using 'LaTeX' or in Microsoft Excel '.xlsx' files.
Version: 1.2.7
Version: 1.2.9
Authors@R: c(
person("Persephone", "Tsebelis", role="aut"),
person("Kamil", "Sedrowicz", role="aut"),
Expand All @@ -17,9 +17,10 @@ BugReports: https://github.com/Crunch-io/crunchtabs/issues
License: LGPL (>= 3)
Depends:
R (>= 3.5.0),
crunch,
kableExtra
crunch
Imports:
kableExtra,
rlang,
openxlsx,
digest,
methods,
Expand All @@ -31,7 +32,8 @@ Suggests:
jsonlite,
knitr,
rmarkdown,
testthat (>= 2.1.0)
RoxygenNote: 7.1.0
testthat (>= 2.1.0),
kableExtra (>= 1.1.0.9000)
RoxygenNote: 7.1.1
VignetteBuilder: knitr
Encoding: UTF-8
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export(themeHuffPoToplines)
export(themeNew)
export(themeUKPolitical)
export(with_api_fixture)
export(writeCodeBook)
export(writeCodeBookLatex)
export(writeExcel)
export(writeLatex)
Expand Down Expand Up @@ -89,6 +88,7 @@ importFrom(kableExtra,column_spec)
importFrom(kableExtra,kable_styling)
importFrom(magrittr,`%>%`)
importFrom(methods,is)
importFrom(rlang,.data)
importFrom(stats,ave)
importFrom(stats,median)
importFrom(stats,pnorm)
Expand Down
16 changes: 15 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
## crunchtabs 1.2.9

- Codebook question descriptions now appropriately escape special characters (#187)
- Added option enforce_onehundred which allows one to avoid rounding errors in totals rows (#189)
- Codebook table of contents overruns, cutting text and adding "..." (#186)
- Codebook generation now supports a filepath (#185)
- Added vertical space before append_text (#182)
- Bugfix: append_text that is multiple lines du0lpicated vertical space. Collapsing. (#191)
- Remove requirement for dev version of kableExtra (#184)

## crunchtabs 1.2.8

- Documentation for generating codebooks (#180)
- Tests and fixes for broken tests (#176)
- Spacing and font type for codebooks (#178)
- Additional refinements for codebooks such as title, subtitle, sample description (#152)
- Fixes for warnings in existing code (#158)
- Bugfix for logo assignment in excel (#155)
- Added pagebreak_in_banner which stops multi-banner crosstabs from being spread across pages using "to be continued" (#161)
- Added pagebreak_in_banner which stops multi-banner crosstabs from being spread across pages using "to be continued" (#161, #169)
- Major rework of codebooks (#165, #172, #170, #160, #159)

## crunchtabs 1.2.7

Expand Down
99 changes: 61 additions & 38 deletions R/codeBookSummary.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,28 @@ codeBookSummary.CategoricalVariable <- function(x, multiple = FALSE, ...) {
smry <- data.frame(crunch::table(x, useNA = "ifany"))
names(smry) <- c("name", "n")

res <- merge(l, smry)
res <- merge(l, smry, sort = FALSE)

if (multiple) {
res[with(res, order(id)),]
res = res[with(res, order(id)),]
} else {
res = matrix(c(
res = as.data.frame(
matrix(c(
res$id,
res$name,
res$n
), ncol = 3)
res
), ncol = 3),
stringsAsFactors = FALSE
)
names(res) <- c("id","name", "n")
}

if (getOption("crunchtabs.codebook.suppress.zeros", default = FALSE)) {
warning('Zero count categoricals are supressed by options')
res <- res[res$n != "0",]
}

res
}

#' @describeIn codeBookSummary Prepares a codeBookSummary data.frame for a MultipleResponseVariable
Expand Down Expand Up @@ -129,24 +138,27 @@ codeBookSummary.CategoricalArrayVariable <- function(x, ...) codeBookSummary.Mul
#' @describeIn codeBookSummary Prepares a codeBookSummary data.frame for a NumericVariable
#' @export
codeBookSummary.NumericVariable <- function(x, ...) {
mu <- round(mean(x, na.rm = T), 2)
std <- round(sd(x, na.rm = TRUE))
minima <- round(min(x, na.rm = T), 2)
maxima <- round(max(x, na.rm = T), 2)
missings <- sum(is.na(as.vector(x)))

type_row <- c("Type", "Numeric")
range_row <- c("Range", paste0("[", minima,", ", maxima,"]"))

if (missings > 0) {
missings_row <- c("Missing", missings)
r <- rbind(
type_row, missings_row, range_row
)
} else {
r <- rbind(
type_row, range_row
)
}

n <- length(is.na(as.vector(x)))

#type_row <- c("Type", "Numeric")
#range_row <- c("Range", paste0("[", minima,", ", maxima,"]"))

r <- as.data.frame(
matrix(c(
mu,
std,
minima,
maxima,
n - missings,
missings), nrow = 1)
)

names(r) <- c("Mean", "SD", "Min", "Max","n", "Missing")
rownames(r) <- NULL
r

Expand All @@ -156,12 +168,26 @@ codeBookSummary.NumericVariable <- function(x, ...) {
#' @export
codeBookSummary.TextVariable <- function(x, ...) {

filled <- sum(as.vector(x) != "" | !is.na(as.vector(x)), na.rm = TRUE)
type_row <- c("Type", "Text")
missing <- c("Blank", length(as.vector(x)) - filled)
filled <- c("Filled", filled)

r <- rbind(type_row, missing, filled)
filled_verbs <- as.vector(x)
filled_verbs <- filled_verbs[!is.na(filled_verbs)]
filled_verbs <- filled_verbs[!filled_verbs %in% c("", "__NA__")]
#verbsamp <- sample(filled_verbs, 5, replace = TRUE)
#verbsamp <- paste0(substr(verbsamp, start = 1, stop = 27),
# "...",
# collapse = ", "
#)
filled <- length(filled_verbs)
missing <- length(as.vector(x)) - filled

r <- as.data.frame(
matrix(c(
filled,
missing,
ifelse(filled == 0, 0, max(nchar(filled_verbs)))
), nrow = 1, ncol = 3), stringsAsFactors = FALSE
)

names(r) <- c("Filled","Missing", "Max Length")
rownames(r) <- NULL
r
}
Expand All @@ -173,19 +199,16 @@ codeBookSummary.DatetimeVariable <- function(x, ...) {
maxima <- max(x, na.rm = T)
missings <- sum(is.na(as.vector(x)))

type_row <- c("Type", "Datetime")
range_row <- c("Range", paste0("[", minima,", ", maxima,"]"))
filled <- sum(!is.na(as.vector(x)))
range_row <- c(paste0("[", minima,", ", maxima,"]"))

r = data.frame(
Filled = filled,
Missing = missings,
Range = range_row,
stringsAsFactors = FALSE
)

if (missings > 0) {
missings_row <- c("Missing", missings)
r <- rbind(
type_row, missings_row, range_row
)
} else {
r <- rbind(
type_row, range_row
)
}

rownames(r) <- NULL
r
Expand Down
Loading

0 comments on commit f0d839d

Please sign in to comment.