diff --git a/r/R/dplyr-functions.R b/r/R/dplyr-functions.R index 5ddd6968972..61046d5e829 100644 --- a/r/R/dplyr-functions.R +++ b/r/R/dplyr-functions.R @@ -559,3 +559,22 @@ nse_funcs$wday <- function(x, label = FALSE, abbr = TRUE, week_start = getOption Expression$create("day_of_week", x, options = list(one_based_numbering = TRUE, week_start = week_start)) } + +nse_funcs$log <- function(x, base = exp(1)) { + + if (base == exp(1)) { + return(Expression$create("ln_checked", x)) + } + + if (base == 2) { + return(Expression$create("log2_checked", x)) + } + + if (base == 10) { + return(Expression$create("log10_checked", x)) + } + # ARROW-13345 + stop("`base` values other than exp(1), 2 and 10 not supported in Arrow", call. = FALSE) +} + +nse_funcs$logb <- nse_funcs$log diff --git a/r/R/expression.R b/r/R/expression.R index be80c9db969..16915427292 100644 --- a/r/R/expression.R +++ b/r/R/expression.R @@ -43,7 +43,15 @@ "yday" = "day_of_year", "hour" = "hour", # second is defined in dplyr-functions.R - "minute" = "minute" + "minute" = "minute", + "log10" = "log10_checked", + "log2" = "log2_checked", + "log1p" = "log1p_checked", + "sin" = "sin_checked", + "cos" = "cos_checked", + "tan" = "tan_checked", + "asin" = "asin_checked", + "acos" = "acos_checked" ) .binary_function_map <- list( diff --git a/r/tests/testthat/helper-expectation.R b/r/tests/testthat/helper-expectation.R index d173620398e..359e31ef57d 100644 --- a/r/tests/testthat/helper-expectation.R +++ b/r/tests/testthat/helper-expectation.R @@ -123,7 +123,7 @@ expect_dplyr_equal <- function(expr, } if (!is.null(skip_msg)) { - skip(paste(skip_msg, collpase = "\n")) + skip(paste(skip_msg, collapse = "\n")) } } @@ -212,7 +212,7 @@ expect_vector_equal <- function(expr, # A vectorized R expression containing `in } if (!is.null(skip_msg)) { - skip(paste(skip_msg, collpase = "\n")) + skip(paste(skip_msg, collapse = "\n")) } } @@ -273,7 +273,7 @@ expect_vector_error <- function(expr, # A vectorized R expression containing `in } if (!is.null(skip_msg)) { - skip(paste(skip_msg, collpase = "\n")) + skip(paste(skip_msg, collapse = "\n")) } } diff --git a/r/tests/testthat/test-dplyr.R b/r/tests/testthat/test-dplyr.R index 459c5ebc441..63d0433fc23 100644 --- a/r/tests/testthat/test-dplyr.R +++ b/r/tests/testthat/test-dplyr.R @@ -945,3 +945,112 @@ test_that("abs()", { df ) }) + +test_that("log functions", { + + df <- tibble(x = c(1:10, NA, NA)) + + expect_dplyr_equal( + input %>% + mutate(y = log(x)) %>% + collect(), + df + ) + + expect_dplyr_equal( + input %>% + mutate(y = log(x, base = exp(1))) %>% + collect(), + df + ) + + expect_dplyr_equal( + input %>% + mutate(y = log(x, base = 2)) %>% + collect(), + df + ) + + expect_dplyr_equal( + input %>% + mutate(y = log(x, base = 10)) %>% + collect(), + df + ) + + expect_error( + nse_funcs$log(Expression$scalar(x), base = 5), + "`base` values other than exp(1), 2 and 10 not supported in Arrow", + fixed = TRUE + ) + + expect_dplyr_equal( + input %>% + mutate(y = logb(x)) %>% + collect(), + df + ) + + expect_dplyr_equal( + input %>% + mutate(y = log1p(x)) %>% + collect(), + df + ) + + expect_dplyr_equal( + input %>% + mutate(y = log2(x)) %>% + collect(), + df + ) + + expect_dplyr_equal( + input %>% + mutate(y = log10(x)) %>% + collect(), + df + ) + +}) + +test_that("trig functions", { + + df <- tibble(x = c(seq(from = 0, to = 1, by = 0.1), NA)) + + expect_dplyr_equal( + input %>% + mutate(y = sin(x)) %>% + collect(), + df + ) + + expect_dplyr_equal( + input %>% + mutate(y = cos(x)) %>% + collect(), + df + ) + + expect_dplyr_equal( + input %>% + mutate(y = tan(x)) %>% + collect(), + df + ) + + expect_dplyr_equal( + input %>% + mutate(y = asin(x)) %>% + collect(), + df + ) + + expect_dplyr_equal( + input %>% + mutate(y = acos(x)) %>% + collect(), + df + ) + +}) \ No newline at end of file