In R, error handling is supported through the use of try, tryCatch, and the withCallingHandlers functions. These built-in constructs allow programmers to handle errors gracefully, ensuring that programs can respond to unexpected situations without crashing. Below is a discussion of how R handles errors, along with examples related to both general exceptions and file I/O operations.
Error Handling in R:
try Function: This function allows execution of an expression and captures any errors that occur during its execution. If the expression fails, try returns an object of class "try-error" instead of stopping execution.
This R code implements a few different examples of error handling, including an example with a divide by zero error, an example with a file input error, and it also displays the use of a Try Catch block in R.
The program Output is shown above, showing a similar implementation of a Try Catch block to Java, that is able to print messages to console during error handling.
R employs automatic memory management via a garbage collection mechanism that is largely hidden from the user. This means that R automatically manages memory, freeing it when objects become unreachable, unlike C, where developers must manage memory manually. Also, memory is allocated implicitly when creating objects in R, which simplifies development compared to manual management in C.
My R code above is designed to count the frequency of words in a specified text file. It starts by reading the file's contents and merging all lines into a single string, converting everything to lowercase to ensure case insensitivity. Punctuation is removed using a regular expression, which replaces non-alphabetic characters with spaces. The cleaned string is then split into individual words based on whitespace.
Next, the code uses a table to count the occurrences of each unique word, converts this table into a data frame for easier manipulation, and sorts it in descending order of frequency. Finally, my program returns and prints the top 20 words along with their corresponding counts!
File I/O in R:
R provides built-in functions for reading from and writing to files, making file input/output operations straightforward. In R, you can read data from a text file using functions like readLines() or read.table(). See the example below:
To write data to a text file, you can use the writeLines() or write.table() functions. Also, R supports reading and writing binary files using the readBin() and writeBin() functions. See examples below:
R can also handle URLs for reading data using readLines() or similar functions. For example:
R provides interactive user input capabilities using the readline() function, allowing programs to prompt users for input. For example:
R Garbage Collection:
My function memory_allocation_function allocates a specified number of memory-intensive objects (a list of random number vectors) in each iteration. It measures the time taken for this process and deallocates the memory each time by setting the variable to NULL, making it eligible for garbage collection.
The function then records the time taken for each allocation call and stores the values in a times vector. Finally, the main function runs the memory allocation function multiple times and computes the average time taken for all iterations. It then checks each iteration’s time against the average to identify potential garbage collection events based on a defined threshold, as shown by my output.