I have a package that I am developing and one of the functions is called make_dicho(). Every time I run devtools::check(), one of the examples in make_dicho() causes the check() to fail. make_dicho accepts haven_labelled vectors or factor vectors and converts it to a factor vector with only two response options. The function also adds two new attributes to the vector. Here is the code for the function:
make_dicho <- function(x, flip_levels = FALSE) {
# get the object's name
x_lab <- deparse(substitute(x))
# get the variable lable
variable_label <- labelled::var_label(x)
# convert the vector to a factor
x <- haven::as_factor(x)
# remove the first word if there are multiple words
x <- dplyr::if_else(
stringr::str_detect(x, "\\s"),
stringr::str_replace(x, "\\w+\\s", ""),
x
) |>
stringr::str_to_title()
if (flip_levels == TRUE) {
# Get the second level of the new vector
lab <- unique(x)[2]
# change the factor levels
forcats::fct_relevel(x, lab) %>%
# add new attributes
structure(
# indicate that the original variable was converted to a dichotomous factor
transformation = glue::glue("Converting '{x_lab}' to a dichotomous factor and reordering the factor levels so that '{lab}' is the reference level"),
# add the original variable label
label = variable_label
)
} else {
# Get the first levels of the new vector
lab <- unique(x)[1]
# add new attributes
forcats::as_factor(x) %>% structure(
# indicate that the original variable was converted to a dichotomous factor
transformation = glue::glue("Converting '{x_lab}' to a dichotomous factor with '{lab}' as the reference level"),
# add the original variable label
label = variable_label
)
}
}
Here is the code for the examples:
#' @examples
#' library(tibble)
#' library(dplyr)
#' library(labelled)
#' library(haven)
#'
#' # create fake data
#' df <- tibble::tribble(
#' ~x, ~y, ~z,
#' 3, 2, 3,
#' 4, 4, 2,
#' 2, 6, 1,
#' 1, 1, 4,
#' 5, 4, 3,
#' 6, 5, 6
#' ) %>%
#' # add value labels
#' labelled::set_value_labels(
#' x = c(`Strongly agree` = 1,
#' `Agree` = 2,
#' `Somewhat agree` = 3,
#' `Somewhat disagree` = 4,
#' `Disagree` = 5,
#' `Strongly disagree` = 6),
#' y = c(`Strongly agree` = 1,
#' `Agree` = 2,
#' `Somewhat agree` = 3,
#' `Somewhat disagree` = 4,
#' `Disagree` = 5,
#' `Strongly disagree` = 6),
#' z = c(`Strongly agree` = 1,
#' `Agree` = 2,
#' `Somewhat agree` = 3,
#' `Somewhat disagree` = 4,
#' `Disagree` = 5,
#' `Strongly disagree` = 6)
#' ) %>%
#' # add variable labels
#' labelled::set_variable_labels(
#' x = "This is the variable label for x",
#' y = "This is the variable label for y",
#' z = "This is the variable label for z"
#' )
#'
#' # show the data transformation with a haven_labelled vector
#' dicho_df <- df %>% dplyr::mutate(dicho_x = make_dicho(x))
#' # check the updated dataset
#' dicho_df
#'
#' # Check the attributes
#' attributes(dicho_df$dicho_x)
#' # another way of checking the attributes
#' str(dicho_df$dicho_x)
#'
#' # check the factor levels
#' unique(dicho_df$dicho_x)
#'
#' ----------------------------------------------------------------------------
#' # function also works with factors
#' dicho_df <- df %>%
#' dplyr::mutate(
#' # convert variable to a factor
#' factor_x = haven::as_factor(x),
#' # convert the factor to a dichotomous factor
#' dicho_x = make_dicho(factor_x)
#' )
#'
#' # check the updated dataset
#' dicho_df
#'
#' # Check the attributes
#' attributes(dicho_df$dicho_x)
#' # another way of checking the attributes
#' str(dicho_df$dicho_x)
#'
#' # check the factor levels
#' unique(dicho_df$dicho_x)
#'
#' ----------------------------------------------------------------------------
#' # function also works inside dplyr::across()
#' \dontrun{
#' # Create new columns using `across()`
#' dicho_df <- df %>%
#' dplyr::mutate(
#' # use this example if you don't want to flip the factor levels
#' dplyr::across(
#' x:z,
#' make_dicho,
#' .names = "dicho_{col}"
#' ),
#' # if you want to flip the factor levels, follow this example
#' dplyr::across(
#' x:z,
#' ~make_dicho(., flip_levels = TRUE),
#' .names = "dicho_flipped_{col}"
#' )
#' )
#' }
#'
When I run devtools::check() I get the following error message for the second example. When I removed that example, I got the same error for the third example (the one that uses dplyr::across().
I'm not sure why I'm getting this message as it works just fine when I check it myself. Any advice or ideas for how to fix this would be great. I'd rather not use dontrun\ if I can help it though.
