added option to export "both" raw and label by labelling raw data to preserve as much information as possible

This commit is contained in:
Andreas Gammelgaard Damsbo 2024-11-27 09:51:51 +01:00
commit 2ba46e8e7a
No known key found for this signature in database
11 changed files with 177 additions and 230 deletions

View file

@ -8,6 +8,8 @@
\alias{as_factor.character}
\alias{as_factor.haven_labelled}
\alias{as_factor.labelled}
\alias{as_factor.redcapcast_labelled}
\alias{as_factor.data.frame}
\title{Convert labelled vectors to factors while preserving attributes}
\usage{
as_factor(x, ...)
@ -33,6 +35,15 @@ as_factor(x, ...)
ordered = FALSE,
...
)
\method{as_factor}{redcapcast_labelled}(
x,
levels = c("default", "labels", "values", "both"),
ordered = FALSE,
...
)
\method{as_factor}{data.frame}(x, ..., only_labelled = TRUE)
}
\arguments{
\item{x}{Object to coerce to a factor.}
@ -49,6 +60,8 @@ as_factor(x, ...)
\item{ordered}{If `TRUE` create an ordered (ordinal) factor, if
`FALSE` (the default) create a regular (nominal) factor.}
\item{only_labelled}{Only apply to labelled columns?}
}
\description{
This extends [forcats::as_factor()] as well as [haven::as_factor()], by appending

View file

@ -1,5 +1,5 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/easy_redcap.R
% Please edit documentation in R/read_redcap_tables.R
\name{clean_field_label}
\alias{clean_field_label}
\title{Very simple function to remove rich text formatting from field label

View file

@ -11,7 +11,7 @@ ds2dd(
field.type = "text",
field.label = NULL,
include.column.names = FALSE,
metadata = REDCapCAST::redcap_meta_default()
metadata = names(REDCapCAST::redcapcast_meta)
)
}
\arguments{
@ -34,7 +34,7 @@ names.}
column names for original data set for upload.}
\item{metadata}{Metadata column names. Default is the included
REDCapCAST::redcap_meta_default.}
names(REDCapCAST::redcapcast_meta).}
}
\value{
data.frame or list of data.frame and vector

18
man/fct_drop.Rd Normal file
View file

@ -0,0 +1,18 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/fct_drop.R
\name{fct_drop}
\alias{fct_drop}
\alias{fct_drop.data.frame}
\title{Drop unused levels preserving label data}
\usage{
fct_drop.data.frame(x, ...)
}
\arguments{
\item{x}{Factor to drop unused levels}
\item{...}{Other arguments passed down to method.}
}
\description{
This extends [forcats::fct_drop()] to natively work across a data.frame and
replace [base::droplevels()].
}

28
man/is.labelled.Rd Normal file
View file

@ -0,0 +1,28 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/as_factor.R
\name{is.labelled}
\alias{is.labelled}
\title{Tests for multiple label classes}
\usage{
is.labelled(
x,
classes = c("redcapcast_labelled", "haven_labelled", "labelled")
)
}
\arguments{
\item{x}{data}
\item{classes}{classes to test}
}
\value{
logical
}
\description{
Tests for multiple label classes
}
\examples{
structure(c(1, 2, 3, 2, 10, 9),
labels = c(Unknown = 9, Refused = 10),
class = "haven_labelled"
) |> is.labelled()
}

View file

@ -1,38 +0,0 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/easy_redcap.R
\name{read_redcap_labelled}
\alias{read_redcap_labelled}
\title{REDCap read function to preserve field labels and all factor levels}
\usage{
read_redcap_labelled(
uri,
token,
records = NULL,
fields = NULL,
events = NULL,
forms = NULL,
split_forms = "all"
)
}
\arguments{
\item{uri}{REDCap database API uri}
\item{token}{API token}
\item{records}{records to download}
\item{fields}{fields to download}
\item{events}{events to download}
\item{forms}{forms to download}
\item{split_forms}{Whether to split "repeating" or "all" forms, default is
"all".}
}
\value{
data.frame or list
}
\description{
This works very much as `read_redcap_tables()` and might end up there
}

View file

@ -28,7 +28,14 @@ read_redcap_tables(
\item{forms}{forms to download}
\item{raw_or_label}{raw or label tags}
\item{raw_or_label}{raw or label tags. Can be
* "raw": Standard [REDCapR] method to get raw values.
* "label": Standard [REDCapR] method to get label values.
* "both": Get raw values with REDCap labels applied as labels. Use
[as_factor()] to format factors with original labels and use the
[gtsummary] package to easily get beautiful tables with original labels
from REDCap. Use [fct_drop()] to drop empty levels.}
\item{split_forms}{Whether to split "repeating" or "all" forms, default is
all.}

View file

@ -1,20 +0,0 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/ds2dd_detailed.R
\name{redcap_meta_default}
\alias{redcap_meta_default}
\title{Default column names of a REDCap data dictionary}
\usage{
redcap_meta_default(...)
}
\arguments{
\item{...}{ignored for now}
}
\value{
character vector
}
\description{
Default column names of a REDCap data dictionary
}
\examples{
dput(redcap_meta_default())
}