Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jun 26, 2024
1 parent a789982 commit b0ed06f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions R/find_transformation.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ find_transformation.character <- function(x, ...) {
eval(parse(text = gsub("log\\(([^,\\+)]*)(.*)\\)", "\\1", x)))
)
}
if (is.null(plus_minus)) {
if (is.null(plus_minus) || is.function(plus_minus)) {
transform_fun <- "log"
} else {
transform_fun <- paste0("log(x+", plus_minus, ")")
Expand All @@ -94,7 +94,7 @@ find_transformation.character <- function(x, ...) {
} else if (any(grepl("sqrt\\((.*)\\)", x))) {
# sqrt-transformation
plus_minus <- eval(parse(text = gsub("sqrt\\(([^,\\+)]*)(.*)\\)", "\\2", x)))
if (is.null(plus_minus)) {
if (is.null(plus_minus) || is.function(plus_minus)) {
transform_fun <- "sqrt"
} else {
transform_fun <- paste0("sqrt(x+", plus_minus, ")")
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-find_transformation.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ test_that("find_transformation - log", {
expect_identical(find_transformation("log(Sepal.Length)"), "log")
})

test_that("find_transformation - log for function names", {
# "time" is a function name - make sure this works correctly
iris$time <- iris$Sepal.Length
model <- lm(log(time) ~ Species, data = iris)
expect_identical(find_transformation(model), "log")
expect_identical(find_transformation("log(time)"), "log")
})

test_that("find_transformation - log+x", {
model <- lm(log(Sepal.Length + 3) ~ Species, data = iris)
expect_identical(find_transformation(model), "log(x+3)")
Expand Down

0 comments on commit b0ed06f

Please sign in to comment.