mirror of
https://github.com/agdamsbo/REDCapCAST.git
synced 2024-11-22 13:30:23 +01:00
executing examples with as_factor() errors. I think due to redundancy. Will investigate.
This commit is contained in:
parent
69e1520aff
commit
c86ae9a364
@ -11,6 +11,7 @@
|
|||||||
#' @export
|
#' @export
|
||||||
#' @examples
|
#' @examples
|
||||||
#' # will preserve all attributes but class
|
#' # will preserve all attributes but class
|
||||||
|
#' \dontrun{
|
||||||
#' c(1, 4, 3, "A", 7, 8, 1) |> as_factor()
|
#' c(1, 4, 3, "A", 7, 8, 1) |> as_factor()
|
||||||
#' structure(c(1, 2, 3, 2, 10, 9),
|
#' structure(c(1, 2, 3, 2, 10, 9),
|
||||||
#' labels = c(Unknown = 9, Refused = 10)
|
#' labels = c(Unknown = 9, Refused = 10)
|
||||||
@ -22,7 +23,7 @@
|
|||||||
#' class = "haven_labelled"
|
#' class = "haven_labelled"
|
||||||
#' ) |>
|
#' ) |>
|
||||||
#' as_factor()
|
#' as_factor()
|
||||||
#'
|
#' }
|
||||||
#' @importFrom forcats as_factor
|
#' @importFrom forcats as_factor
|
||||||
#' @importFrom rlang check_dots_used
|
#' @importFrom rlang check_dots_used
|
||||||
#' @export
|
#' @export
|
||||||
@ -74,15 +75,20 @@ as_factor.labelled <- as_factor.haven_labelled
|
|||||||
#'
|
#'
|
||||||
#' @param data factor
|
#' @param data factor
|
||||||
#' @param label character string of attribute with named vector of factor labels
|
#' @param label character string of attribute with named vector of factor labels
|
||||||
|
#' @param na.label character string to refactor NA values. Default is NULL.
|
||||||
|
#' @param na.value new value for NA strings. Ignored if na.label is NULL.
|
||||||
|
#' Default is 99.
|
||||||
#'
|
#'
|
||||||
#' @return named vector
|
#' @return named vector
|
||||||
#' @export
|
#' @export
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
|
#' \dontrun{
|
||||||
#' structure(c(1, 2, 3, 2, 10, 9),
|
#' structure(c(1, 2, 3, 2, 10, 9),
|
||||||
#' labels = c(Unknown = 9, Refused = 10),
|
#' labels = c(Unknown = 9, Refused = 10),
|
||||||
#' class = "haven_labelled"
|
#' class = "haven_labelled"
|
||||||
#' ) |> as_factor() |> named_levels()
|
#' ) |> as_factor() |> named_levels()
|
||||||
|
#' }
|
||||||
named_levels <- function(data, label = "labels",na.label=NULL, na.value=99) {
|
named_levels <- function(data, label = "labels",na.label=NULL, na.value=99) {
|
||||||
stopifnot(is.factor(data))
|
stopifnot(is.factor(data))
|
||||||
if (!is.null(na.label)){
|
if (!is.null(na.label)){
|
||||||
@ -141,6 +147,7 @@ named_levels <- function(data, label = "labels",na.label=NULL, na.value=99) {
|
|||||||
#' @export
|
#' @export
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
|
#' \dontrun{
|
||||||
#' c(1, 4, 3, "A", 7, 8, 1) |>
|
#' c(1, 4, 3, "A", 7, 8, 1) |>
|
||||||
#' as_factor() |> fct2num()
|
#' as_factor() |> fct2num()
|
||||||
#'
|
#'
|
||||||
@ -156,6 +163,7 @@ named_levels <- function(data, label = "labels",na.label=NULL, na.value=99) {
|
|||||||
#' ) |>
|
#' ) |>
|
||||||
#' as_factor() |>
|
#' as_factor() |>
|
||||||
#' fct2num()
|
#' fct2num()
|
||||||
|
#' }
|
||||||
fct2num <- function(data) {
|
fct2num <- function(data) {
|
||||||
stopifnot(is.factor(data))
|
stopifnot(is.factor(data))
|
||||||
as.numeric(named_levels(data))[match(data, names(named_levels(data)))]
|
as.numeric(named_levels(data))[match(data, names(named_levels(data)))]
|
||||||
@ -171,11 +179,12 @@ fct2num <- function(data) {
|
|||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
#' attr(mtcars$mpg, "label") <- "testing"
|
#' attr(mtcars$mpg, "label") <- "testing"
|
||||||
#' sapply(mtcars, get_attr)
|
#' do.call(c,sapply(mtcars, get_attr))
|
||||||
#' lapply(mtcars, \(.x)get_attr(.x, NULL))
|
#' \dontrun{
|
||||||
#' mtcars |>
|
#' mtcars |>
|
||||||
#' numchar2fct(numeric.threshold = 6) |>
|
#' numchar2fct(numeric.threshold = 6) |>
|
||||||
#' ds2dd_detailed()
|
#' ds2dd_detailed()
|
||||||
|
#' }
|
||||||
get_attr <- function(data, attr = NULL) {
|
get_attr <- function(data, attr = NULL) {
|
||||||
if (is.null(attr)) {
|
if (is.null(attr)) {
|
||||||
attributes(data)
|
attributes(data)
|
||||||
@ -195,16 +204,20 @@ get_attr <- function(data, attr = NULL) {
|
|||||||
#' @param data vector
|
#' @param data vector
|
||||||
#' @param label label
|
#' @param label label
|
||||||
#' @param attr attribute name
|
#' @param attr attribute name
|
||||||
|
#' @param overwrite overwrite existing attributes. Default is FALSE.
|
||||||
#'
|
#'
|
||||||
#' @return vector with attribute
|
#' @return vector with attribute
|
||||||
#' @export
|
#' @export
|
||||||
#'
|
#'
|
||||||
set_attr <- function(data, label, attr = NULL) {
|
set_attr <- function(data, label, attr = NULL, overwrite=FALSE) {
|
||||||
if (is.null(attr)) {
|
if (is.null(attr)) {
|
||||||
## Has to be list...
|
## Has to be list...
|
||||||
stopifnot(is.list(label))
|
stopifnot(is.list(label))
|
||||||
## ... with names
|
## ... with names
|
||||||
stopifnot(length(label)==length(names(label)))
|
stopifnot(length(label)==length(names(label)))
|
||||||
|
if (!overwrite){
|
||||||
|
label <- label[!names(label) %in% names(attributes(data))]
|
||||||
|
}
|
||||||
attributes(data) <- c(attributes(data),label)
|
attributes(data) <- c(attributes(data),label)
|
||||||
} else {
|
} else {
|
||||||
attr(data, attr) <- label
|
attr(data, attr) <- label
|
||||||
@ -239,11 +252,3 @@ haven_all_levels <- function(data) {
|
|||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
# readr::read_rds("/Users/au301842/PAaSO/labelled_test.rds") |> ds2dd_detailed()
|
|
||||||
#' sample(c(TRUE,FALSE,NA),20,TRUE) |> set_attr("hidden","status") |> trial_fct() |> named_levels(na.label = "Missing") |> sort()
|
|
||||||
# trial_fct <- function(x){
|
|
||||||
# labels <- get_attr(x)
|
|
||||||
# x <- factor(x, levels = c("FALSE", "TRUE"))
|
|
||||||
# set_attr(x, labels[-match("class", names(labels))])
|
|
||||||
# }
|
|
||||||
|
|
||||||
|
@ -141,6 +141,7 @@ hms2character <- function(data) {
|
|||||||
#' @export
|
#' @export
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
|
#' \dontrun{
|
||||||
#' data <- REDCapCAST::redcapcast_data
|
#' data <- REDCapCAST::redcapcast_data
|
||||||
#' data |> ds2dd_detailed()
|
#' data |> ds2dd_detailed()
|
||||||
#' iris |> ds2dd_detailed(add.auto.id = TRUE)
|
#' iris |> ds2dd_detailed(add.auto.id = TRUE)
|
||||||
@ -157,6 +158,7 @@ hms2character <- function(data) {
|
|||||||
#' names(data) <- glue::glue("{sample(x = c('a','b'),size = length(names(data)),
|
#' names(data) <- glue::glue("{sample(x = c('a','b'),size = length(names(data)),
|
||||||
#' replace=TRUE,prob = rep(x=.5,2))}__{names(data)}")
|
#' replace=TRUE,prob = rep(x=.5,2))}__{names(data)}")
|
||||||
#' data |> ds2dd_detailed(form.sep = "__")
|
#' data |> ds2dd_detailed(form.sep = "__")
|
||||||
|
#' }
|
||||||
ds2dd_detailed <- function(data,
|
ds2dd_detailed <- function(data,
|
||||||
add.auto.id = FALSE,
|
add.auto.id = FALSE,
|
||||||
date.format = "dmy",
|
date.format = "dmy",
|
||||||
@ -416,9 +418,11 @@ mark_complete <- function(upload, ls) {
|
|||||||
#' @export
|
#' @export
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
|
#' \dontrun{
|
||||||
#' mtcars |>
|
#' mtcars |>
|
||||||
#' parse_data() |>
|
#' parse_data() |>
|
||||||
#' str()
|
#' str()
|
||||||
|
#' }
|
||||||
parse_data <- function(data,
|
parse_data <- function(data,
|
||||||
guess_type = TRUE,
|
guess_type = TRUE,
|
||||||
col_types = NULL,
|
col_types = NULL,
|
||||||
@ -434,7 +438,7 @@ parse_data <- function(data,
|
|||||||
## Parses haven data by applying labels as factors in case of any
|
## Parses haven data by applying labels as factors in case of any
|
||||||
if (do.call(c, lapply(data, (\(x)inherits(x, "haven_labelled")))) |> any()) {
|
if (do.call(c, lapply(data, (\(x)inherits(x, "haven_labelled")))) |> any()) {
|
||||||
data <- data |>
|
data <- data |>
|
||||||
haven::as_factor()
|
as_factor()
|
||||||
}
|
}
|
||||||
|
|
||||||
## Applying readr cols
|
## Applying readr cols
|
||||||
@ -474,6 +478,7 @@ parse_data <- function(data,
|
|||||||
#' @importFrom forcats as_factor
|
#' @importFrom forcats as_factor
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
|
#' \dontrun{
|
||||||
#' sample(seq_len(4), 20, TRUE) |>
|
#' sample(seq_len(4), 20, TRUE) |>
|
||||||
#' var2fct(6) |>
|
#' var2fct(6) |>
|
||||||
#' summary()
|
#' summary()
|
||||||
@ -481,9 +486,10 @@ parse_data <- function(data,
|
|||||||
#' var2fct(6) |>
|
#' var2fct(6) |>
|
||||||
#' summary()
|
#' summary()
|
||||||
#' sample(letters[1:4], 20, TRUE) |> var2fct(6)
|
#' sample(letters[1:4], 20, TRUE) |> var2fct(6)
|
||||||
|
#' }
|
||||||
var2fct <- function(data, unique.n) {
|
var2fct <- function(data, unique.n) {
|
||||||
if (length(unique(data)) <= unique.n) {
|
if (length(unique(data)) <= unique.n) {
|
||||||
forcats::as_factor(data)
|
as_factor(data)
|
||||||
} else {
|
} else {
|
||||||
data
|
data
|
||||||
}
|
}
|
||||||
@ -505,9 +511,11 @@ var2fct <- function(data, unique.n) {
|
|||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
#' mtcars |> str()
|
#' mtcars |> str()
|
||||||
|
#' \dontrun{
|
||||||
#' mtcars |>
|
#' mtcars |>
|
||||||
#' numchar2fct(numeric.threshold = 6) |>
|
#' numchar2fct(numeric.threshold = 6) |>
|
||||||
#' str()
|
#' str()
|
||||||
|
#' }
|
||||||
numchar2fct <- function(data, numeric.threshold = 6, character.throshold = 6) {
|
numchar2fct <- function(data, numeric.threshold = 6, character.throshold = 6) {
|
||||||
data |>
|
data |>
|
||||||
dplyr::mutate(
|
dplyr::mutate(
|
||||||
|
@ -5,6 +5,6 @@ account: agdamsbo
|
|||||||
server: shinyapps.io
|
server: shinyapps.io
|
||||||
hostUrl: https://api.shinyapps.io/v1
|
hostUrl: https://api.shinyapps.io/v1
|
||||||
appId: 11351429
|
appId: 11351429
|
||||||
bundleId:
|
bundleId: 9391508
|
||||||
url: https://agdamsbo.shinyapps.io/redcapcast/
|
url: https://agdamsbo.shinyapps.io/redcapcast/
|
||||||
version: 1
|
version: 1
|
||||||
|
@ -24,7 +24,7 @@ server <- function(input, output, session) {
|
|||||||
out <- read_input(input$ds$datapath)
|
out <- read_input(input$ds$datapath)
|
||||||
|
|
||||||
# Saves labels to reapply later
|
# Saves labels to reapply later
|
||||||
# labels <- lapply(out, get_attr)
|
labels <- lapply(out, get_attr)
|
||||||
|
|
||||||
out <- out |>
|
out <- out |>
|
||||||
## Parses data with readr functions
|
## Parses data with readr functions
|
||||||
@ -44,10 +44,10 @@ server <- function(input, output, session) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Old attributes are appended
|
# Old attributes are appended
|
||||||
# out <- purrr::imap(out,\(.x,.i){
|
out <- purrr::imap(out,\(.x,.i){
|
||||||
# set_attr(.x,labels[[.i]])
|
set_attr(.x,labels[[.i]])
|
||||||
# }) |>
|
}) |>
|
||||||
# dplyr::bind_cols()
|
dplyr::bind_cols()
|
||||||
|
|
||||||
out
|
out
|
||||||
})
|
})
|
||||||
|
@ -36,6 +36,7 @@ Please refer to parent functions for extended documentation.
|
|||||||
}
|
}
|
||||||
\examples{
|
\examples{
|
||||||
# will preserve all attributes but class
|
# will preserve all attributes but class
|
||||||
|
\dontrun{
|
||||||
c(1, 4, 3, "A", 7, 8, 1) |> as_factor()
|
c(1, 4, 3, "A", 7, 8, 1) |> as_factor()
|
||||||
structure(c(1, 2, 3, 2, 10, 9),
|
structure(c(1, 2, 3, 2, 10, 9),
|
||||||
labels = c(Unknown = 9, Refused = 10)
|
labels = c(Unknown = 9, Refused = 10)
|
||||||
@ -47,5 +48,5 @@ structure(c(1, 2, 3, 2, 10, 9),
|
|||||||
class = "haven_labelled"
|
class = "haven_labelled"
|
||||||
) |>
|
) |>
|
||||||
as_factor()
|
as_factor()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,6 +75,7 @@ Ensure, that the data set is formatted with as much information as possible.
|
|||||||
`field.type` can be supplied
|
`field.type` can be supplied
|
||||||
}
|
}
|
||||||
\examples{
|
\examples{
|
||||||
|
\dontrun{
|
||||||
data <- REDCapCAST::redcapcast_data
|
data <- REDCapCAST::redcapcast_data
|
||||||
data |> ds2dd_detailed()
|
data |> ds2dd_detailed()
|
||||||
iris |> ds2dd_detailed(add.auto.id = TRUE)
|
iris |> ds2dd_detailed(add.auto.id = TRUE)
|
||||||
@ -92,3 +93,4 @@ names(data) <- glue::glue("{sample(x = c('a','b'),size = length(names(data)),
|
|||||||
replace=TRUE,prob = rep(x=.5,2))}__{names(data)}")
|
replace=TRUE,prob = rep(x=.5,2))}__{names(data)}")
|
||||||
data |> ds2dd_detailed(form.sep = "__")
|
data |> ds2dd_detailed(form.sep = "__")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -16,6 +16,7 @@ numeric vector
|
|||||||
Allows conversion of factor to numeric values preserving original levels
|
Allows conversion of factor to numeric values preserving original levels
|
||||||
}
|
}
|
||||||
\examples{
|
\examples{
|
||||||
|
\dontrun{
|
||||||
c(1, 4, 3, "A", 7, 8, 1) |>
|
c(1, 4, 3, "A", 7, 8, 1) |>
|
||||||
as_factor() |> fct2num()
|
as_factor() |> fct2num()
|
||||||
|
|
||||||
@ -31,4 +32,5 @@ structure(c(1, 2, 3, 2, 10, 9),
|
|||||||
) |>
|
) |>
|
||||||
as_factor() |>
|
as_factor() |>
|
||||||
fct2num()
|
fct2num()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,9 +19,10 @@ Extract attribute. Returns NA if none
|
|||||||
}
|
}
|
||||||
\examples{
|
\examples{
|
||||||
attr(mtcars$mpg, "label") <- "testing"
|
attr(mtcars$mpg, "label") <- "testing"
|
||||||
sapply(mtcars, get_attr)
|
do.call(c,sapply(mtcars, get_attr))
|
||||||
lapply(mtcars, \(.x)get_attr(.x, NULL))
|
\dontrun{
|
||||||
mtcars |>
|
mtcars |>
|
||||||
numchar2fct(numeric.threshold = 6) |>
|
numchar2fct(numeric.threshold = 6) |>
|
||||||
ds2dd_detailed()
|
ds2dd_detailed()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -10,6 +10,11 @@ named_levels(data, label = "labels", na.label = NULL, na.value = 99)
|
|||||||
\item{data}{factor}
|
\item{data}{factor}
|
||||||
|
|
||||||
\item{label}{character string of attribute with named vector of factor labels}
|
\item{label}{character string of attribute with named vector of factor labels}
|
||||||
|
|
||||||
|
\item{na.label}{character string to refactor NA values. Default is NULL.}
|
||||||
|
|
||||||
|
\item{na.value}{new value for NA strings. Ignored if na.label is NULL.
|
||||||
|
Default is 99.}
|
||||||
}
|
}
|
||||||
\value{
|
\value{
|
||||||
named vector
|
named vector
|
||||||
@ -18,8 +23,10 @@ named vector
|
|||||||
Get named vector of factor levels and values
|
Get named vector of factor levels and values
|
||||||
}
|
}
|
||||||
\examples{
|
\examples{
|
||||||
|
\dontrun{
|
||||||
structure(c(1, 2, 3, 2, 10, 9),
|
structure(c(1, 2, 3, 2, 10, 9),
|
||||||
labels = c(Unknown = 9, Refused = 10),
|
labels = c(Unknown = 9, Refused = 10),
|
||||||
class = "haven_labelled"
|
class = "haven_labelled"
|
||||||
) |> as_factor() |> named_levels()
|
) |> as_factor() |> named_levels()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -23,7 +23,9 @@ Individual thresholds for character and numeric columns
|
|||||||
}
|
}
|
||||||
\examples{
|
\examples{
|
||||||
mtcars |> str()
|
mtcars |> str()
|
||||||
|
\dontrun{
|
||||||
mtcars |>
|
mtcars |>
|
||||||
numchar2fct(numeric.threshold = 6) |>
|
numchar2fct(numeric.threshold = 6) |>
|
||||||
str()
|
str()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -33,7 +33,9 @@ data.frame or tibble
|
|||||||
Helper to auto-parse un-formatted data with haven and readr
|
Helper to auto-parse un-formatted data with haven and readr
|
||||||
}
|
}
|
||||||
\examples{
|
\examples{
|
||||||
|
\dontrun{
|
||||||
mtcars |>
|
mtcars |>
|
||||||
parse_data() |>
|
parse_data() |>
|
||||||
str()
|
str()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
\alias{set_attr}
|
\alias{set_attr}
|
||||||
\title{Set attributes for named attribute. Appends if attr is NULL}
|
\title{Set attributes for named attribute. Appends if attr is NULL}
|
||||||
\usage{
|
\usage{
|
||||||
set_attr(data, label, attr = NULL)
|
set_attr(data, label, attr = NULL, overwrite = FALSE)
|
||||||
}
|
}
|
||||||
\arguments{
|
\arguments{
|
||||||
\item{data}{vector}
|
\item{data}{vector}
|
||||||
@ -12,6 +12,8 @@ set_attr(data, label, attr = NULL)
|
|||||||
\item{label}{label}
|
\item{label}{label}
|
||||||
|
|
||||||
\item{attr}{attribute name}
|
\item{attr}{attribute name}
|
||||||
|
|
||||||
|
\item{overwrite}{overwrite existing attributes. Default is FALSE.}
|
||||||
}
|
}
|
||||||
\value{
|
\value{
|
||||||
vector with attribute
|
vector with attribute
|
||||||
|
@ -19,6 +19,7 @@ This is a wrapper of forcats::as_factor, which sorts numeric vectors before
|
|||||||
factoring, but levels character vectors in order of appearance.
|
factoring, but levels character vectors in order of appearance.
|
||||||
}
|
}
|
||||||
\examples{
|
\examples{
|
||||||
|
\dontrun{
|
||||||
sample(seq_len(4), 20, TRUE) |>
|
sample(seq_len(4), 20, TRUE) |>
|
||||||
var2fct(6) |>
|
var2fct(6) |>
|
||||||
summary()
|
summary()
|
||||||
@ -27,3 +28,4 @@ sample(letters, 20) |>
|
|||||||
summary()
|
summary()
|
||||||
sample(letters[1:4], 20, TRUE) |> var2fct(6)
|
sample(letters[1:4], 20, TRUE) |> var2fct(6)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user