From 1a0c4e669d9ca773d04728be0434f21571af4b85 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 19 Nov 2023 20:01:10 +0100 Subject: [PATCH 1/3] support haven_labelled --- DESCRIPTION | 2 +- NAMESPACE | 3 +++ NEWS.md | 2 ++ R/to_factor.R | 6 ++++++ R/to_numeric.R | 3 +++ tests/testthat/test-data_to_factor.R | 20 ++++++++++++++++++++ tests/testthat/test-data_to_numeric.R | 13 +++++++++++++ 7 files changed, 48 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 1aaf0d501..efc356179 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.9.0.2 +Version: 0.9.0.3 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")), diff --git a/NAMESPACE b/NAMESPACE index 7809afcf4..a2a911db3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -167,7 +167,9 @@ S3method(to_factor,Date) S3method(to_factor,character) S3method(to_factor,data.frame) S3method(to_factor,default) +S3method(to_factor,double) S3method(to_factor,factor) +S3method(to_factor,haven_labelled) S3method(to_factor,logical) S3method(to_factor,numeric) S3method(to_numeric,Date) @@ -179,6 +181,7 @@ S3method(to_numeric,data.frame) S3method(to_numeric,default) S3method(to_numeric,double) S3method(to_numeric,factor) +S3method(to_numeric,haven_labelled) S3method(to_numeric,logical) S3method(to_numeric,numeric) S3method(unnormalize,data.frame) diff --git a/NEWS.md b/NEWS.md index 404961f86..d799a0d5b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,8 @@ CHANGES * `rescale()` gains `multiply` and `add` arguments, to expand ranges by a given factor or value. +* `to_factor()` and `to_numeric()` now support class `haven_labelled`. + # datawizard 0.9.0 NEW FUNCTIONS diff --git a/R/to_factor.R b/R/to_factor.R index ece6290e4..c31580072 100644 --- a/R/to_factor.R +++ b/R/to_factor.R @@ -79,6 +79,12 @@ to_factor.character <- to_factor.numeric #' @export to_factor.Date <- to_factor.numeric +#' @export +to_factor.haven_labelled <- to_factor.numeric + +#' @export +to_factor.double <- to_factor.numeric + #' @rdname to_factor #' @export to_factor.data.frame <- function(x, diff --git a/R/to_numeric.R b/R/to_numeric.R index 56001b039..80fb7db27 100644 --- a/R/to_numeric.R +++ b/R/to_numeric.R @@ -157,6 +157,9 @@ to_numeric.double <- to_numeric.numeric #' @export to_numeric.logical <- to_numeric.numeric +#' @export +to_numeric.haven_labelled <- to_numeric.numeric + #' @export to_numeric.Date <- function(x, verbose = TRUE, ...) { if (verbose) { diff --git a/tests/testthat/test-data_to_factor.R b/tests/testthat/test-data_to_factor.R index 0fd31b26a..289dccf0c 100644 --- a/tests/testthat/test-data_to_factor.R +++ b/tests/testthat/test-data_to_factor.R @@ -141,3 +141,23 @@ test_that("data_read, convert many labels correctly", { expect_snapshot(data_tabulate(to_factor(d$c12c))) unlink(temp_file) }) + + +test_that("to_factor works with haven_labelled, convert many labels correctly", { + withr::with_tempfile("temp_file", fileext = ".sav", code = { + request <- httr::GET("https://raw.github.com/easystats/circus/main/data/EFC.sav") + httr::stop_for_status(request) + writeBin(httr::content(request, type = "raw"), temp_file) + + d <- haven::read_spss(temp_file) + x <- to_factor(d$c172code) + expect_identical( + levels(x), + c( + "low level of education", + "intermediate level of education", + "high level of education" + ) + ) + }) +}) diff --git a/tests/testthat/test-data_to_numeric.R b/tests/testthat/test-data_to_numeric.R index 68eb8f0dc..fa5c408b2 100644 --- a/tests/testthat/test-data_to_numeric.R +++ b/tests/testthat/test-data_to_numeric.R @@ -160,3 +160,16 @@ test_that("to_numeric regex", { to_numeric(mtcars, select = "mpg") ) }) + + +test_that("to_numeric works with haven_labelled, convert many labels correctly", { + withr::with_tempfile("temp_file", fileext = ".sav", code = { + request <- httr::GET("https://raw.github.com/easystats/circus/main/data/EFC.sav") + httr::stop_for_status(request) + writeBin(httr::content(request, type = "raw"), temp_file) + + d <- haven::read_spss(temp_file) + x <- to_numeric(d$c172code) + expect_identical(as.vector(table(x)), c(180L, 506L, 156L)) + }) +}) From df7b172bb512cabe42156fd4135f9a09fa74265f Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 19 Nov 2023 20:45:19 +0100 Subject: [PATCH 2/3] fix --- tests/testthat/test-data_to_numeric.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/testthat/test-data_to_numeric.R b/tests/testthat/test-data_to_numeric.R index fa5c408b2..4bfd969bf 100644 --- a/tests/testthat/test-data_to_numeric.R +++ b/tests/testthat/test-data_to_numeric.R @@ -163,6 +163,10 @@ test_that("to_numeric regex", { test_that("to_numeric works with haven_labelled, convert many labels correctly", { + skip_if_not_installed("httr") + skip_if_not_installed("haven") + skip_on_cran() + withr::with_tempfile("temp_file", fileext = ".sav", code = { request <- httr::GET("https://raw.github.com/easystats/circus/main/data/EFC.sav") httr::stop_for_status(request) From 6babb972a0744d8a60342f8ba94952025a4870ce Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 19 Nov 2023 20:46:36 +0100 Subject: [PATCH 3/3] fix --- tests/testthat/test-data_to_factor.R | 1 + tests/testthat/test-data_to_numeric.R | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-data_to_factor.R b/tests/testthat/test-data_to_factor.R index 289dccf0c..e45423bd5 100644 --- a/tests/testthat/test-data_to_factor.R +++ b/tests/testthat/test-data_to_factor.R @@ -144,6 +144,7 @@ test_that("data_read, convert many labels correctly", { test_that("to_factor works with haven_labelled, convert many labels correctly", { + skip_if_not_installed("withr") withr::with_tempfile("temp_file", fileext = ".sav", code = { request <- httr::GET("https://raw.github.com/easystats/circus/main/data/EFC.sav") httr::stop_for_status(request) diff --git a/tests/testthat/test-data_to_numeric.R b/tests/testthat/test-data_to_numeric.R index 4bfd969bf..df43f401f 100644 --- a/tests/testthat/test-data_to_numeric.R +++ b/tests/testthat/test-data_to_numeric.R @@ -163,9 +163,12 @@ test_that("to_numeric regex", { test_that("to_numeric works with haven_labelled, convert many labels correctly", { + skip_on_cran() skip_if_not_installed("httr") skip_if_not_installed("haven") - skip_on_cran() + skip_if_not_installed("withr") + skip_if_not_installed("curl") + skip_if_offline() withr::with_tempfile("temp_file", fileext = ".sav", code = { request <- httr::GET("https://raw.github.com/easystats/circus/main/data/EFC.sav")