Skip to content

Commit

Permalink
Merge pull request #91 from RSGInc/87-change-how-checkboxes-function-…
Browse files Browse the repository at this point in the history
…in-hts_summaryhts_summary_cat

Setting defaults for checkbox_valname and checkbox_yesval
  • Loading branch information
erika-redding authored Jan 30, 2024
2 parents 5addc29 + d4d92bd commit 2cad429
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 31 deletions.
21 changes: 10 additions & 11 deletions R/hts_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
#' @param wtname Name of the weight column to use. Default is NULL. Must be specified
#' when weighted = TRUE.
#' @param strataname Name of strata name to bring in. Default is NULL.
#' @param checkbox_valname Name of the column with the checkbox value. Default is NULL.
#' @param checkbox_valname Name of the column with the checkbox value. Default is 'value'.
#' Must be provided if summarize_var is a checkbox variable.
#' @param checkbox_yesval Value of checkbox_valname that indicates it was selected.
#' Default is NULL. Must be provided if summarize_var is a checkbox variable.
#' Default is 1. Must be provided if summarize_var is a checkbox variable.
#'
#' @return A list containing (if applicable) categorical and numeric summaries of the
#' specified variable(s), as well as sample sizes and whether or not the summarized
Expand Down Expand Up @@ -79,10 +79,8 @@
#' output = hts_summary(prepped_dt = DT,
#' summarize_var = 'race',
#' summarize_by = 'age',
#' summarize_vartype = 'categorical',
#' wtname = 'person_weight',
#' checkbox_valname = 'value',
#' checkbox_yesval = 1)
#' summarize_vartype = 'checkbox',
#' wtname = 'person_weight')


hts_summary = function(
Expand All @@ -95,17 +93,17 @@ hts_summary = function(
se = FALSE,
wtname = NULL,
strataname = NULL,
checkbox_valname = NULL,
checkbox_yesval = NULL) {
checkbox_valname = 'value',
checkbox_yesval = 1) {

# FIXME consider a labels = T/F argument here

# For instances where num obs is singular inside a sub-strata, adjust:
options(survey.lonely.psu = "adjust")

if ( summarize_vartype == 'checkbox' &
if ( summarize_vartype == 'checkbox' &
(is.null(checkbox_valname) | is.null(checkbox_yesval))) {

stop("Must provide checkbox_valname and checkbox_yesval if summarize_vartype is checkbox.")
}

Expand Down Expand Up @@ -149,7 +147,8 @@ hts_summary = function(
wtname = wtname,
strataname = strataname,
checkbox_valname = checkbox_valname,
checkbox_yesval = checkbox_yesval
checkbox_yesval = checkbox_yesval,
summarize_vartype = summarize_vartype
)
}

Expand Down
22 changes: 16 additions & 6 deletions R/hts_summary_cat.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
#' @param se Whether to calculate standard error. Default is FALSE.
#' @param wtname Name of the weight column to use. Default is NULL.
#' @param strataname Name of strata name to bring in. Default is NULL.
#' @param checkbox_valname Name of the column with the checkbox value. Default is NULL.
#' @param checkbox_valname Name of the column with the checkbox value. Default is 'value'.
#' Must be provided if summarize_var is a checkbox variable.
#' @param checkbox_yesval Value of checkbox_valname that indicates it was selected.
#' Default is NULL. Must be provided if summarize_var is a checkbox variable.
#' Default is 1. Must be provided if summarize_var is a checkbox variable.
#' @param summarize_vartype String; one of either 'categorical' (when the
#' variable being summarized is categorical) or 'checkbox' (when the variable being
#' summarized is derived from a multiple response, aka select-all-that-apply question).
#'
#'
#' @importFrom srvyr survey_prop
Expand Down Expand Up @@ -53,8 +56,7 @@
#' summarize_var = 'race',
#' summarize_by = 'employment',
#' wtname = 'person_weight',
#' checkbox_valname = 'value',
#' checkbox_yesval = 1)
#' summarize_vartype = 'checkbox')
#'
#' DT = hts_prep_data(summarize_var = 'employment',
#' summarize_by = c('race', 'income_detailed', 'gender'),
Expand All @@ -77,8 +79,9 @@ hts_summary_cat = function(prepped_dt,
se = FALSE,
wtname = NULL,
strataname = NULL,
checkbox_valname = NULL,
checkbox_yesval = NULL) {
checkbox_valname = 'value',
checkbox_yesval = 1,
summarize_vartype = 'categorical') {

if ( !weighted & se ){

Expand All @@ -100,6 +103,13 @@ hts_summary_cat = function(prepped_dt,

}

if ( summarize_vartype != 'checkbox'){

checkbox_valname = NULL
checkbox_yesval = NULL

}

groupbyvars = c(
summarize_by,
summarize_var,
Expand Down
14 changes: 6 additions & 8 deletions man/hts_summary.Rd

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

16 changes: 10 additions & 6 deletions man/hts_summary_cat.Rd

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

0 comments on commit 2cad429

Please sign in to comment.