Overview of the 'tinycodet' "Don't Repeat Yourself" Functionality
Source:R/aaa4_tinycodet_dry.R
aaa4_tinycodet_dry.Rd
"Don't Repeat Yourself", sometimes abbreviated as "DRY", is the coding principle not to write unnecessarily repetitive code. To help in that effort, the 'tinycodet' R-package introduces a few functions:
Examples
object <- matrix(c(-9:8, NA, NA) , ncol=2)
# in base R:
ifelse( # repetitive, and gives unnecessary warning
is.na(object > 0), -Inf,
ifelse(
object > 0, log(object), object^2
)
)
#> Warning: NaNs produced
#> [,1] [,2]
#> [1,] 81 0.0000000
#> [2,] 64 0.6931472
#> [3,] 49 1.0986123
#> [4,] 36 1.3862944
#> [5,] 25 1.6094379
#> [6,] 16 1.7917595
#> [7,] 9 1.9459101
#> [8,] 4 2.0794415
#> [9,] 1 -Inf
#> [10,] 0 -Inf
mtcars$mpg[mtcars$cyl>6] <- (mtcars$mpg[mtcars$cyl>6])^2 # long
# with tinycodet:
object |> transform_if(\(x) x > 0, log, \(x) x^2, \(x) -Inf) # compact & no warning
#> [,1] [,2]
#> [1,] 81 0.0000000
#> [2,] 64 0.6931472
#> [3,] 49 1.0986123
#> [4,] 36 1.3862944
#> [5,] 25 1.6094379
#> [6,] 16 1.7917595
#> [7,] 9 1.9459101
#> [8,] 4 2.0794415
#> [9,] 1 -Inf
#> [10,] 0 -Inf
mtcars$mpg[mtcars$cyl > 6] %:=% \(x) x^2 # short