From b0ed06f78f850e55d7d6895d8b75002f975b0777 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 26 Jun 2024 14:05:50 +0200 Subject: [PATCH] fix --- R/find_transformation.R | 4 ++-- tests/testthat/test-find_transformation.R | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/R/find_transformation.R b/R/find_transformation.R index 61166f4c5..6779f3402 100644 --- a/R/find_transformation.R +++ b/R/find_transformation.R @@ -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, ")") @@ -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, ")") diff --git a/tests/testthat/test-find_transformation.R b/tests/testthat/test-find_transformation.R index 4580c171b..06278f098 100644 --- a/tests/testthat/test-find_transformation.R +++ b/tests/testthat/test-find_transformation.R @@ -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)")