From 5c55508ed329fae4f4301da19f69f012cebbb6b1 Mon Sep 17 00:00:00 2001 From: Daniel Sjoberg Date: Tue, 14 May 2024 15:03:24 -0700 Subject: [PATCH] added atributes ard --- DESCRIPTION | 6 ++--- NAMESPACE | 1 + NEWS.md | 1 + R/ard_attributes.survey.design.R | 37 +++++++++++++++++++++++++ man/ard_attributes.Rd | 46 ++++++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 R/ard_attributes.survey.design.R create mode 100644 man/ard_attributes.Rd diff --git a/DESCRIPTION b/DESCRIPTION index b6f9ce3f8..b30785e72 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -18,7 +18,7 @@ BugReports: https://github.com/insightsengineering/cardx/issues Depends: R (>= 4.1) Imports: - cards (>= 0.1.0.9025), + cards (>= 0.1.0.9026), cli (>= 3.6.1), dplyr (>= 1.1.2), glue (>= 1.6.2), @@ -33,13 +33,13 @@ Suggests: effectsize (>= 0.6.0), emmeans (>= 1.7.3), geepack (>= 1.3.2), - ggsurvfit (>= 1.0.0), + ggsurvfit (>= 1.1.0), lme4 (>= 1.1-31), parameters (>= 0.20.2), smd (>= 0.6.6), spelling, survey (>= 4.1), - survival (>= 3.2-11), + survival (>= 3.6-4), testthat (>= 3.2.0), withr (>= 2.5.0) Remotes: diff --git a/NAMESPACE b/NAMESPACE index 338a9539a..43f5d3d5c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +S3method(ard_attributes,survey.design) S3method(ard_continuous,survey.design) S3method(ard_regression,default) S3method(ard_stats_anova,anova) diff --git a/NEWS.md b/NEWS.md index f25281d28..94c94cfe0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -31,6 +31,7 @@ ard_moodtest() -> ard_stats_mood_test() - `ard_smd_smd()` for calculating standardized mean differences using `smd::smd()`. (#4) - `ard_survival_survfit()` for survival analyses using `survival::survfit()`. (#43) - `ard_continuous.survey.design()` for calculating univariate summary statistics from weighted/survey data using many functions from the {survey} package. (#68) + - `ard_attributes.survey.design()` for summarizing labels and attributes from weighted/survey data using many functions from the {survey} package. - `ard_survey_svychisq()` for weighted/survey chi-squared test using `survey::svychisq()`. (#72) - `ard_survey_svyttest()` for weighted/survey t-tests using `survey::svyttest()`. (#70) - `ard_survey_svyranktest()` for weighted/survey rank tests using `survey::svyranktest()`. (#71) diff --git a/R/ard_attributes.survey.design.R b/R/ard_attributes.survey.design.R new file mode 100644 index 000000000..b3f069768 --- /dev/null +++ b/R/ard_attributes.survey.design.R @@ -0,0 +1,37 @@ +#' ARD Attributes +#' +#' @description +#' Add variable attributes to an ARD data frame. +#' - The `label` attribute will be added for all columns, and when no label +#' is specified and no label has been set for a column using the `label=` argument, +#' the column name will be placed in the label statistic. +#' - The `class` attribute will also be returned for all columns. +#' - Any other attribute returned by `attributes()` will also be added, e.g. factor levels. +#' +#' @rdname ard_attributes +#' @param data (`survey.design`)\cr +#' a design object often created with [`survey::svydesign()`]. +#' @param variables ([`tidy-select`][dplyr::dplyr_tidy_select])\cr +#' variables to include +#' @param label (named `list`)\cr +#' named list of variable labels, e.g. `list(cyl = "No. Cylinders")`. +#' Default is `NULL` +#' @inheritParams rlang::args_dots_empty +#' +#' @return an ARD data frame of class 'card' +#' @export +#' +#' @examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = "survey", reference_pkg = "cardx")) +#' data(api, package = "survey") +#' dclus1 <- survey::svydesign(id = ~dnum, weights = ~pw, data = apiclus1, fpc = ~fpc) +#' +#' ard_attributes( +#' data = dclus1, +#' variables = c(sname, dname), +#' label = list(sname = "School Name", dname = "District Name") +#' ) +ard_attributes.survey.design <- function(data, variables = everything(), label = NULL, ...) { + set_cli_abort_call() + + cards::ard_attributes(data = data[["variables"]], variables = {{ variables }}, label = label, ...) +} diff --git a/man/ard_attributes.Rd b/man/ard_attributes.Rd new file mode 100644 index 000000000..6069aae4d --- /dev/null +++ b/man/ard_attributes.Rd @@ -0,0 +1,46 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ard_attributes.survey.design.R +\name{ard_attributes.survey.design} +\alias{ard_attributes.survey.design} +\title{ARD Attributes} +\usage{ +\method{ard_attributes}{survey.design}(data, variables = everything(), label = NULL, ...) +} +\arguments{ +\item{data}{(\code{survey.design})\cr +a design object often created with \code{\link[survey:svydesign]{survey::svydesign()}}.} + +\item{variables}{(\code{\link[dplyr:dplyr_tidy_select]{tidy-select}})\cr +variables to include} + +\item{label}{(named \code{list})\cr +named list of variable labels, e.g. \code{list(cyl = "No. Cylinders")}. +Default is \code{NULL}} + +\item{...}{These dots are for future extensions and must be empty.} +} +\value{ +an ARD data frame of class 'card' +} +\description{ +Add variable attributes to an ARD data frame. +\itemize{ +\item The \code{label} attribute will be added for all columns, and when no label +is specified and no label has been set for a column using the \verb{label=} argument, +the column name will be placed in the label statistic. +\item The \code{class} attribute will also be returned for all columns. +\item Any other attribute returned by \code{attributes()} will also be added, e.g. factor levels. +} +} +\examples{ +\dontshow{if (do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = "survey", reference_pkg = "cardx"))) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +data(api, package = "survey") +dclus1 <- survey::svydesign(id = ~dnum, weights = ~pw, data = apiclus1, fpc = ~fpc) + +ard_attributes( + data = dclus1, + variables = c(sname, dname), + label = list(sname = "School Name", dname = "District Name") +) +\dontshow{\}) # examplesIf} +}