The “readLines” Function in R
Package: Base R (No specific package)
Purpose: To read lines from a connection or file.
General Class: Input/Output
Required Argument(s):
con: A connection or a character string giving the name of the file to read from.
Notable Optional Arguments:
n: The number of lines to read. Negative values indicate all lines should be read. The default is -1 (meaning all lines).
skipNul: A logical indicating whether to skip nulls in the file. The default is FALSE.
Example:
# Example usage
# Create a sample text file
writeLines(c("Line 1", "Line 2", "Line 3"), "example.txt")
# Read all lines from the file
lines <- readLines("example.txt", n = -1, skipNul = FALSE)
# Print the lines
print(lines)In this example, the readLines function is used to read all lines from a file named “example.txt”. The n argument is set to -1 to read all lines, and the skipNul argument is set to FALSE to include null characters in the read lines. The resulting lines are stored in the lines variable and printed to the console.