Ever heard of the words verbose and non-verbose in the English language?
In straightforward language, we characterize the two words as:
Verbose: A text containing a lot of words.
Could be more words than necessary.
Resembles the expression "full of".
Non-verbose: A text not composed of many words.
To the point.
Clear and concise, may eliminate minor details.
Just like these English words, the verbose
argument in an R function targets the wordiness of an output.
Note: R is a programming language mainly used in the domains of statistics and data analysis.
verbose
argument in RIn R, the verbose
argument is a common parameter that is used in functions to control the level of output or messages displayed during the execution of the function. It allows us to toggle between verbose i.e. a detailed output and non-verbose i.e. a concise output based on your specific needs.
It's a pretty great tool that just requires a true or a false from our side and can alter our output for us accordingly in just a matter of seconds!
Why is verbose an argument?
verbose
The verbose
argument is a logical parameter, represented by Boolean variables. This argument accepts either TRUE
or FALSE
values.
TRUE:
When we wish to keep the details of our output as they are.
FALSE:
When we wish to keep our output minimal.
Let's now map our English definitions to the argument we've just learned.
This argument has many real-world applications and is highly useful for us as developers. Let's explore a few possibilities and see how it works.
verbose
= FALSEcalculateSum <- function(numbers, verbose = FALSE) {if (verbose) {cat("We are now going to perform calculations \n")}resultSum <- sum(numbers)if (verbose) {cat("The calculated sum is:", resultSum, "\n")}return(resultSum)}# our inputinputNumbers <- c(2, 4, 6, 8, 10)# calculating the sum without verbose outputcalculateSum(inputNumbers, verbose = FALSE)
Note: See how only the sum is shown and the code recognizes what to print.
verbose
= TRUEcalculateSum <- function(numbers, verbose = TRUE) {if (verbose) {cat("We are now going to perform calculations \n")}resultSum <- sum(numbers)if (verbose) {cat("The calculated sum is:", resultSum, "\n")}return(resultSum)}# our inputinputNumbers <- c(2, 4, 6, 8, 10)# calculating the sum with verbose outputcalculateSum(inputNumbers, verbose = TRUE)
This explanation covers both situations and what the output might be in both cases.
Line 1: We define a function calculateSum
in R that takes in a vector of digits named as numbers
, as well as the verbose parameter, which is either set to TRUE
or FALSE
.
Lines 2–4: If the verbose argument is true we print "We are now going to perform calculations". Otherwise, this line is skipped to keep the output concise.
Line 6: The built-in sum
method is called on the elements of numbers
and is saved in a variable named resultSum
.
Lines 8–10: If the verbose
argument is TRUE
we print "The calculated sum is: ", followed by the resultSum
. Otherwise, just the resultSum
is shown.
Line 12: The method returns the calculated sum using resultSum
.
Line 16: We define inputNumbers
as the input vector we'll be passing to our function.
Line 19: Finally, we call our calculateSum
method and it renders the output.
Check out the difference between the rendered outputs, just by adding a single verbose argument.
verbose
= TRUEDebugging: When troubleshooting or debugging code, setting the verbose
argument to true helps us understand the execution flow, variable values, and intermediate results. This directly aids in quickly identifying issues.
Progress tracking: For long-running tasks, enabling verbose
output allows us to track progress by displaying informative messages or even progress indicators at different stages.
verbose
= FALSEProduction environment: In a production environment, we usually wish to minimize unnecessary output. So, setting the verbose
argument to False
in this case, helps in reducing clutter and improving efficiency.
User interface (UI): When integrating code with a user interface or interactive application, non-verbose output helps us ensure a clean and concise display for the end-users. Therefore, we can achieve the goal to avoid overwhelming them with excessive information.
It’s quiz time!
Why is verbose
useful in debugging?
Free Resources