When you write functions do you include a `return` statement? It isn't necessary in most cases since R will return the last evaluated expression as what it returns when it reaches the end of the function. I know some people feel passionately that since it isn't necessary that it's misguided and introduces clutter. Some people think it provides clarity. What are your opinions on include return. Here are some examples for either
Using returns:
without returns:
Using returns:
C-like:
monitor_object <- function(object_name, FUN = dim){
# Get
fun_name <- deparse(substitute(FUN))
print(fun_name)
object_dim <- NULL
if(exists(object_name)){
object_output <- FUN(get(object_name))
}
callback_function <- function(...){
new_output <- NULL
if(exists(object_name)){
new_output <- FUN(get(object_name))
}
if(!identical(new_output, object_output)){
msg <- paste0(object_name, " changed. Summary function: ", fun_name,
"\nOld output: ", paste(object_output, collapse = " "),
"\nNew output: ", paste(new_output, collapse = " "))
object_output <<- new_output
message(msg)
}
return(TRUE)
}
return(callback_function)
}
C-like:
monitor_object <- function(object_name, FUN = dim){
# Get
fun_name <- deparse(substitute(FUN))
print(fun_name)
object_dim <- NULL
if(exists(object_name)){
object_output <- FUN(get(object_name))
}
callback_function <- function(...){
new_output <- NULL
if(exists(object_name)){
new_output <- FUN(get(object_name))
}
if(!identical(new_output, object_output)){
msg <- paste0(object_name, " changed. Summary function: ", fun_name,
"\nOld output: ", paste(object_output, collapse = " "),
"\nNew output: ", paste(new_output, collapse = " "))
object_output <<- new_output
message(msg)
}
TRUE
}
callback_function
}