55# '
66# ' @param name Character string. The name to bid farewell to. Default is "world".
77# ' @param language Character string. The language for the farewell.
8- # ' Supported languages: "english" (default), "spanish", "french", "portuguese".
8+ # ' Supported languages: "english" (default), "spanish", "french", "portuguese", "german", "italian" .
99# ' @param exclamation Logical. Whether to add an exclamation mark. Default is TRUE.
10+ # ' @param capitalize Logical. Whether to capitalize the name. Default is FALSE.
1011# '
1112# ' @return A character string containing the farewell message.
1213# ' @export
1617# ' goodbye("R Users")
1718# ' goodbye("amigos", language = "spanish")
1819# ' goodbye("mes amis", language = "french", exclamation = FALSE)
20+ # ' goodbye("world", capitalize = TRUE)
1921# '
2022# ' @seealso \code{\link{hello}} for a greeting function
21- goodbye <- function (name = " world" , language = " english" , exclamation = TRUE ) {
23+ goodbye <- function (name = " world" , language = " english" , exclamation = TRUE , capitalize = FALSE ) {
2224 # Input validation
2325 if (! is.character(name ) || length(name ) != 1 ) {
2426 stop(" 'name' must be a single character string" )
@@ -28,18 +30,43 @@ goodbye <- function(name = "world", language = "english", exclamation = TRUE) {
2830 stop(" 'language' must be a single character string" )
2931 }
3032
33+ # Validate language parameter
34+ supported_languages <- c(" english" , " spanish" , " french" , " portuguese" , " german" , " italian" )
35+ language <- tolower(language )
36+ if (! language %in% supported_languages ) {
37+ warning(
38+ sprintf(
39+ " Language '%s' not supported. Using English instead. Supported languages: %s" ,
40+ language ,
41+ paste(supported_languages , collapse = " , " )
42+ )
43+ )
44+ language <- " english"
45+ }
46+
3147 if (! is.logical(exclamation ) || length(exclamation ) != 1 ) {
3248 stop(" 'exclamation' must be a single logical value" )
3349 }
3450
51+ if (! is.logical(capitalize ) || length(capitalize ) != 1 ) {
52+ stop(" 'capitalize' must be a single logical value" )
53+ }
54+
55+ # Capitalize name if requested
56+ if (capitalize ) {
57+ name <- paste0(toupper(substr(name , 1 , 1 )), substr(name , 2 , nchar(name )))
58+ }
59+
3560 # Select farewell based on language
3661 farewell <- switch (
37- tolower( language ) ,
62+ language ,
3863 english = " Goodbye" ,
3964 spanish = " Adi\u 00f3s" , # Using Unicode escape for 'ó'
4065 french = " Au revoir" ,
4166 portuguese = " Adeus" ,
42- # Default to English if language not supported
67+ german = " Auf Wiedersehen" ,
68+ italian = " Arrivederci" ,
69+ # Default to English if language not supported (shouldn't happen due to validation)
4370 " Goodbye"
4471 )
4572
0 commit comments