Skip to content

Commit

Permalink
update docs and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed May 17, 2024
1 parent 99237e0 commit b8e81e6
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 26 deletions.
1 change: 1 addition & 0 deletions R/data_select.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ data_select <- function(data,
}


#' @rdname extract_column_names
#' @export
get_columns <- function(data,
select = NULL,
Expand Down
10 changes: 2 additions & 8 deletions R/data_xtabulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,12 @@ print_html.dw_data_xtabulate <- function(x, big_mark = NULL, ...) {
x$Group <- NULL
}


## FIXME: change group_by argument later

# print table
insight::export_table(
format(x, big_mark = big_mark, format = "html", ...),
missing = "(NA)",
format = "html",
group_by = "groups"
by = "groups"
)
}

Expand Down Expand Up @@ -268,15 +265,12 @@ print_html.dw_data_xtabulates <- function(x, big_mark = NULL, ...) {

out <- do.call(rbind, x)


## FIXME: change group_by argument later

# print table
insight::export_table(
out,
missing = "(NA)",
format = "html",
group_by = "groups"
by = "groups"
)
}
}
Expand Down
1 change: 1 addition & 0 deletions R/extract_column_names.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ extract_column_names <- function(data,
}


#' @rdname extract_column_names
#' @export
data_find <- function(data,
select = NULL,
Expand Down
1 change: 1 addition & 0 deletions R/recode_values.R
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ recode_values.data.frame <- function(x,

## TODO Deprecate and remove alias later

#' @rdname recode_values
#' @export
change_code <- function(x,
select = NULL,
Expand Down
1 change: 1 addition & 0 deletions R/text_format.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ text_format <- function(text, sep = ", ", last = " and ", width = NULL, enclose

## TODO Deprecate and remove alias later

#' @rdname text_format
#' @export
format_text <- function(text, sep = ", ", last = " and ", width = NULL, enclose = NULL, ...) {
insight::format_warning("Function `format_text()` is deprecated and will be removed in a future release. Please use `text_format()` instead.") # nolint
Expand Down
22 changes: 22 additions & 0 deletions man/extract_column_names.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions man/recode_values.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions man/text_format.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions tests/testthat/_snaps/text_format.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# text formatting helpers work as expected

Code
format_text(c("A very long First", "Some similar long Second", "Shorter Third",
text_format(c("A very long First", "Some similar long Second", "Shorter Third",
"More or less long Fourth", "And finally the Last"), width = 20)
Output
[1] "A very long First,\nSome similar long\nSecond, Shorter\nThird, More or less\nlong Fourth and And\nfinally the Last\n"

---

Code
format_text(c("A very long First", "Some similar long Second", "Shorter Third",
text_format(c("A very long First", "Some similar long Second", "Shorter Third",
"More or less long Fourth", "And finally the Last"), last = " or ", enclose = "`",
width = 20)
Output
Expand All @@ -19,7 +19,7 @@

Code
long_text <- strrep("abc ", 100)
cat(format_text(long_text, width = 50))
cat(text_format(long_text, width = 50))
Output
abc abc abc abc abc abc abc abc abc abc abc abc
abc abc abc abc abc abc abc abc abc abc abc abc
Expand All @@ -31,7 +31,7 @@
abc abc abc abc abc abc abc abc abc abc abc abc
abc abc abc abc
Code
cat(format_text(long_text, width = 80))
cat(text_format(long_text, width = 80))
Output
abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc
abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc
Expand All @@ -40,7 +40,7 @@
abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc
Code
withr::with_options(list(width = 50), code = {
cat(format_text(long_text))
cat(text_format(long_text))
})
Output
abc abc abc abc abc abc abc abc abc abc abc abc
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-attributes.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ test_that("categorize, attributes preserved", {

# change_code -----------------------------------

test_that("change_code, attributes preserved", {
test_that("recode_values, attributes preserved", {
x <- mtcars
attr(x, "myattri") <- "I'm here"
x2 <- change_code(x, select = "am", recode = list(`5` = 0, `10` = 1))
x2 <- recode_values(x, select = "am", recode = list(`5` = 0, `10` = 1))
expect_identical(attr(x2, "myattri", exact = TRUE), "I'm here")
})

Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-labelled_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -311,18 +311,18 @@ test_that("convert_to_na, labels preserved", {



# get_columns -----------------------------------
# data_select -----------------------------------

test_that("get_columns, labels preserved", {
x <- get_columns(efc, starts_with("c"))
test_that("data_select, labels preserved", {
x <- data_select(efc, starts_with("c"))
# numeric
expect_equal(
attr(x$c12hour, "label", exact = TRUE),
attr(efc$c12hour, "label", exact = TRUE),
ignore_attr = TRUE
)

x <- get_columns(efc, starts_with("e"))
x <- data_select(efc, starts_with("e"))
# factor
expect_equal(
attr(x$e42dep, "label", exact = TRUE),
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-standardize_datagrid.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# standardize -----------------------------------------------------
test_that("standardize.datagrid", {
x <- insight::get_datagrid(iris, at = "Sepal.Length", range = "sd", length = 3)
x <- insight::get_datagrid(iris, by = "Sepal.Length", range = "sd", length = 3)
out <- standardize(x)
expect_equal(as.numeric(out$Sepal.Length), c(-1, 0, 1))
expect_equal(as.numeric(out$Sepal.Width), c(0, 0, 0))

x <- insight::get_datagrid(iris, at = "Sepal.Length = c(-1, 0)")
x <- insight::get_datagrid(iris, by = "Sepal.Length = c(-1, 0)")
out <- unstandardize(x, select = "Sepal.Length")
expect_equal(out$Sepal.Length[1:2], c(mean(iris$Sepal.Length) - sd(iris$Sepal.Length), mean(iris$Sepal.Length)))
})
10 changes: 5 additions & 5 deletions tests/testthat/test-text_format.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that("text formatting helpers work as expected", {
expect_snapshot(format_text(
expect_snapshot(text_format(
c(
"A very long First",
"Some similar long Second",
Expand All @@ -10,7 +10,7 @@ test_that("text formatting helpers work as expected", {
width = 20
))

expect_snapshot(format_text(
expect_snapshot(text_format(
c(
"A very long First",
"Some similar long Second",
Expand Down Expand Up @@ -73,11 +73,11 @@ test_that("text formatting helpers work as expected", {
test_that("text formatters respect `width` argument", {
expect_snapshot({
long_text <- strrep("abc ", 100)
cat(format_text(long_text, width = 50))
cat(format_text(long_text, width = 80))
cat(text_format(long_text, width = 50))
cat(text_format(long_text, width = 80))

withr::with_options(list(width = 50), code = {
cat(format_text(long_text))
cat(text_format(long_text))
})
})
})

0 comments on commit b8e81e6

Please sign in to comment.