Line Filters in R Programming Language
Our line filter program reads input from stdin, processes it, and then prints a derived result to stdout. In this case, it will write a capitalized version of all input text. You can use this pattern to write your own R line filters.
In this R version:
We use the
readLines()
function to read all input lines from stdin.We iterate over each line using a for loop.
The
str_to_upper()
function from thestringr
package is used to convert each line to uppercase.We use
cat()
to print the uppercase line, adding a newline character.After processing all lines, we check for any warnings that occurred during input reading. If there were warnings, we print them to stderr and exit with a status code of 1.
To try out our line filter, first make a file with a few lowercase lines:
Then use the line filter to get uppercase lines:
This R script provides similar functionality to the original Go program, reading input line by line, converting each line to uppercase, and writing the result to stdout.