From 0d137de4d00bb206594eeb99c438467c5727d3e5 Mon Sep 17 00:00:00 2001 From: joshhwuu Date: Mon, 27 May 2024 12:44:06 -0700 Subject: [PATCH 1/8] update fifelse documentation --- man/fifelse.Rd | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/man/fifelse.Rd b/man/fifelse.Rd index 4165dd796d..5ee7e79f5b 100644 --- a/man/fifelse.Rd +++ b/man/fifelse.Rd @@ -15,12 +15,16 @@ } \details{ In contrast to \code{\link[base]{ifelse}} attributes are copied from the first non-\code{NA} argument to the output. This is useful when returning \code{Date}, \code{factor} or other classes. + +\code{fifelse}, contrary to \code{\link[base]{ifelse}}, evalutes both \code{yes} and \code{no} arguments for type checking regardless the result of \code{test}. This means that neither \code{yes} nor \code{no} should be recursive function calls, use \code{fcase} instead. } \value{ A vector of the same length as \code{test} and attributes as \code{yes}. Data values are taken from the values of \code{yes} and \code{no}, eventually \code{na}. } \seealso{ \code{\link{fcoalesce}} + + \code{\link{fcase}} } \examples{ x = c(1:4, 3:2, 1:4) @@ -37,5 +41,9 @@ fifelse(c(TRUE,FALSE,TRUE), yes, no) # Example of using the 'na' argument fifelse(test = c(-5L:5L < 0L, NA), yes = 1L, no = 0L, na = 2L) + +# Example showing both 'yes' and 'no' arguments are evaluated, unlike ifelse +fifelse(1 == 1, print("yes"), print("no")) +ifelse(1 == 1, print("yes"), print("no")) } \keyword{ data } From 79b3ad14b2b3da885a7fbc79aa3f2f26d9b27db6 Mon Sep 17 00:00:00 2001 From: joshhwuu Date: Wed, 29 May 2024 13:21:22 -0700 Subject: [PATCH 2/8] review changes, better grammar --- man/fifelse.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/fifelse.Rd b/man/fifelse.Rd index 5ee7e79f5b..c74a9f724e 100644 --- a/man/fifelse.Rd +++ b/man/fifelse.Rd @@ -16,7 +16,7 @@ \details{ In contrast to \code{\link[base]{ifelse}} attributes are copied from the first non-\code{NA} argument to the output. This is useful when returning \code{Date}, \code{factor} or other classes. -\code{fifelse}, contrary to \code{\link[base]{ifelse}}, evalutes both \code{yes} and \code{no} arguments for type checking regardless the result of \code{test}. This means that neither \code{yes} nor \code{no} should be recursive function calls, use \code{fcase} instead. +Unlike \code{\link[base]{ifelse}}, \code{fifelse} evaluates both \code{yes} and \code{no} arguments for type checking regardless of the result of \code{test}. This means that neither \code{yes} nor \code{no} should be recursive function calls; use \code{fcase} instead. } \value{ A vector of the same length as \code{test} and attributes as \code{yes}. Data values are taken from the values of \code{yes} and \code{no}, eventually \code{na}. From 5d827fc7f4300fd2311b63dfe17974e4e3ea1353 Mon Sep 17 00:00:00 2001 From: joshhwuu Date: Wed, 29 May 2024 13:22:25 -0700 Subject: [PATCH 3/8] better wording --- man/fifelse.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/fifelse.Rd b/man/fifelse.Rd index c74a9f724e..94dc822ad1 100644 --- a/man/fifelse.Rd +++ b/man/fifelse.Rd @@ -16,7 +16,7 @@ \details{ In contrast to \code{\link[base]{ifelse}} attributes are copied from the first non-\code{NA} argument to the output. This is useful when returning \code{Date}, \code{factor} or other classes. -Unlike \code{\link[base]{ifelse}}, \code{fifelse} evaluates both \code{yes} and \code{no} arguments for type checking regardless of the result of \code{test}. This means that neither \code{yes} nor \code{no} should be recursive function calls; use \code{fcase} instead. +Unlike \code{\link[base]{ifelse}}, \code{fifelse} evaluates both \code{yes} and \code{no} arguments for type checking regardless of the result of \code{test}. This means that neither \code{yes} nor \code{no} should be recursive function calls. For recursiveness, use \code{fcase} instead. } \value{ A vector of the same length as \code{test} and attributes as \code{yes}. Data values are taken from the values of \code{yes} and \code{no}, eventually \code{na}. From 2744e4d9a9cdc7a6d010ab89b03260c47690c34d Mon Sep 17 00:00:00 2001 From: joshhwuu Date: Fri, 31 May 2024 18:34:15 -0700 Subject: [PATCH 4/8] review suggestions, update fcase doc as well --- man/fcase.Rd | 12 ++++++++++++ man/fifelse.Rd | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/man/fcase.Rd b/man/fcase.Rd index dd3a119110..83b2c4d56f 100644 --- a/man/fcase.Rd +++ b/man/fcase.Rd @@ -11,6 +11,11 @@ \item{...}{ A sequence consisting of logical condition (\code{when})-resulting value (\code{value}) \emph{pairs} in the following order \code{when1, value1, when2, value2, ..., whenN, valueN}. Logical conditions \code{when1, when2, ..., whenN} must all have the same length, type and attributes. Each \code{value} may either share length with \code{when} or be length 1. Please see Examples section for further details.} \item{default}{ Default return value, \code{NA} by default, for when all of the logical conditions \code{when1, when2, ..., whenN} are \code{FALSE} or missing for some entries. } } +\details{ + \code{fcase} evaluates each when-value pair in order, until it finds a \code{when} that is \code{TRUE}. It then returns the corresponding \code{value}. During evaluation, \code{value} will be evaluated regardless of whether the corresponding \code{when} is \code{TRUE} or not, which means recursive calls should be placed in the last when-value pair, see \code{Examples}. + + \code{default} is always evaluated, regardless of whether it is returned or not. +} \value{ Vector with the same length as the logical conditions (\code{when}) in \code{...}, filled with the corresponding values (\code{value}) from \code{...}, or eventually \code{default}. Attributes of output values \code{value1, value2, ...valueN} in \code{...} are preserved. } @@ -54,5 +59,12 @@ fcase( x > 5L, 3L, default = 5L ) + +# fcase can be used for recursion, unlike fifelse +gcd_dt <- function(x,y) { + r <- x%%y; + return(fcase(!r, y, r, gcd_dt(x, y))) # Recursive call must be in the last when-value pair +} +gcd_dt(10, 1) } \keyword{ data } diff --git a/man/fifelse.Rd b/man/fifelse.Rd index 94dc822ad1..7f03b4e085 100644 --- a/man/fifelse.Rd +++ b/man/fifelse.Rd @@ -16,7 +16,7 @@ \details{ In contrast to \code{\link[base]{ifelse}} attributes are copied from the first non-\code{NA} argument to the output. This is useful when returning \code{Date}, \code{factor} or other classes. -Unlike \code{\link[base]{ifelse}}, \code{fifelse} evaluates both \code{yes} and \code{no} arguments for type checking regardless of the result of \code{test}. This means that neither \code{yes} nor \code{no} should be recursive function calls. For recursiveness, use \code{fcase} instead. +Unlike \code{\link[base]{ifelse}}, \code{fifelse} evaluates both \code{yes} and \code{no} arguments for type checking regardless of the result of \code{test}. This means that neither \code{yes} nor \code{no} should be recursive function calls. For recursion, use \code{fcase} instead. } \value{ A vector of the same length as \code{test} and attributes as \code{yes}. Data values are taken from the values of \code{yes} and \code{no}, eventually \code{na}. From 304bb7297c3fc694d3fb8990f62d5f30c4c2a661 Mon Sep 17 00:00:00 2001 From: joshhwuu Date: Fri, 31 May 2024 18:35:38 -0700 Subject: [PATCH 5/8] remove tabs --- man/fcase.Rd | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/man/fcase.Rd b/man/fcase.Rd index 83b2c4d56f..56f1fa5d03 100644 --- a/man/fcase.Rd +++ b/man/fcase.Rd @@ -12,9 +12,9 @@ \item{default}{ Default return value, \code{NA} by default, for when all of the logical conditions \code{when1, when2, ..., whenN} are \code{FALSE} or missing for some entries. } } \details{ - \code{fcase} evaluates each when-value pair in order, until it finds a \code{when} that is \code{TRUE}. It then returns the corresponding \code{value}. During evaluation, \code{value} will be evaluated regardless of whether the corresponding \code{when} is \code{TRUE} or not, which means recursive calls should be placed in the last when-value pair, see \code{Examples}. +\code{fcase} evaluates each when-value pair in order, until it finds a \code{when} that is \code{TRUE}. It then returns the corresponding \code{value}. During evaluation, \code{value} will be evaluated regardless of whether the corresponding \code{when} is \code{TRUE} or not, which means recursive calls should be placed in the last when-value pair, see \code{Examples}. - \code{default} is always evaluated, regardless of whether it is returned or not. +\code{default} is always evaluated, regardless of whether it is returned or not. } \value{ Vector with the same length as the logical conditions (\code{when}) in \code{...}, filled with the corresponding values (\code{value}) from \code{...}, or eventually \code{default}. Attributes of output values \code{value1, value2, ...valueN} in \code{...} are preserved. From 0bfa0bf51093b948fcb13f6fa6ca988d3bd90ed5 Mon Sep 17 00:00:00 2001 From: joshhwuu Date: Fri, 31 May 2024 18:57:05 -0700 Subject: [PATCH 6/8] escape %s --- man/fcase.Rd | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/man/fcase.Rd b/man/fcase.Rd index 56f1fa5d03..e017ad7d66 100644 --- a/man/fcase.Rd +++ b/man/fcase.Rd @@ -61,10 +61,11 @@ fcase( ) # fcase can be used for recursion, unlike fifelse -gcd_dt <- function(x,y) { - r <- x%%y; - return(fcase(!r, y, r, gcd_dt(x, y))) # Recursive call must be in the last when-value pair +gcd_dt = function(x,y) { + r = x\%\%y + # Recursive call must be in the last when-value pair + return(fcase(!r, y, r, gcd_dt(x, y))) } -gcd_dt(10, 1) +gcd_dt(10L, 1L) } \keyword{ data } From 12cc309882c0e1b58c86c2edb43feed7903a8d59 Mon Sep 17 00:00:00 2001 From: joshhwuu Date: Sat, 1 Jun 2024 10:54:43 -0700 Subject: [PATCH 7/8] unnecessary return --- man/fcase.Rd | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/man/fcase.Rd b/man/fcase.Rd index e017ad7d66..f0b1590a54 100644 --- a/man/fcase.Rd +++ b/man/fcase.Rd @@ -63,8 +63,7 @@ fcase( # fcase can be used for recursion, unlike fifelse gcd_dt = function(x,y) { r = x\%\%y - # Recursive call must be in the last when-value pair - return(fcase(!r, y, r, gcd_dt(x, y))) + fcase(!r, y, r, gcd_dt(x, y)) # Recursive call must be in the last when-value pair } gcd_dt(10L, 1L) } From bda7b0446fc2e8550a48ab5898192732473b0bf9 Mon Sep 17 00:00:00 2001 From: joshhwuu Date: Sun, 2 Jun 2024 23:10:41 -0700 Subject: [PATCH 8/8] add comment for examples --- man/fcase.Rd | 1 + 1 file changed, 1 insertion(+) diff --git a/man/fcase.Rd b/man/fcase.Rd index f0b1590a54..466f3dfc67 100644 --- a/man/fcase.Rd +++ b/man/fcase.Rd @@ -61,6 +61,7 @@ fcase( ) # fcase can be used for recursion, unlike fifelse +# Recursive function to calculate the Greatest Common Divisor gcd_dt = function(x,y) { r = x\%\%y fcase(!r, y, r, gcd_dt(x, y)) # Recursive call must be in the last when-value pair