What is verbose in R?
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.
The verbose argument in R
In 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?
Possible values for 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.
Code examples
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 = FALSE
calculateSum <- 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 = TRUE
calculateSum <- 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)
Code explanation
This explanation covers both situations and what the output might be in both cases.
Line 1: We define a function
calculateSumin R that takes in a vector of digits named asnumbers, as well as the verbose parameter, which is either set toTRUEorFALSE.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
summethod is called on the elements ofnumbersand is saved in a variable namedresultSum.Lines 8–10: If the
verboseargument isTRUEwe print "The calculated sum is: ", followed by theresultSum. Otherwise, just theresultSumis shown.Line 12: The method returns the calculated sum using
resultSum.Line 16: We define
inputNumbersas the input vector we'll be passing to our function.Line 19: Finally, we call our
calculateSummethod and it renders the output.
Output
Check out the difference between the rendered outputs, just by adding a single verbose argument.
Use cases of verbose = TRUE
Debugging: When troubleshooting or debugging code, setting the
verboseargument 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
verboseoutput allows us to track progress by displaying informative messages or even progress indicators at different stages.
Use cases of verbose = FALSE
Production environment: In a production environment, we usually wish to minimize unnecessary output. So, setting the
verboseargument toFalsein 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.
Why is verbose useful in debugging?
Free Resources