Rotate your phone or change to desktop for better experience

Rotate your phone or change to desktop for better experience

8. Write an R Program Illustrate Reading & Writing Files. || R

 # Define the file paths 

input_file <-"D:/R Program/Lab/input.txt" 

output_file <-" D:/R Program/Lab/output.txt" 

# Reading data from a file 

cat("Reading data from input file...\n") 

tryCatch({ 

data <- readLines(input_file) 

cat("Data read successfully!\n") 

}, error = function(e) { 

cat("Error reading data from the input file:", e$message, 

"\n") 

data <- NULL 

}) 

# Check if data was successfully read 

if (!is.null(data)) {

    # Perform a simple operation (e.g., convert text to 

uppercase) 

modified_data <- toupper(data) 

# Writing data to an output file 

cat("Writing modified data to output file...\n") 

tryCatch({ 

writeLines(modified_data, output_file) 

cat("Data written successfully!\n") 

}, error = function(e) { 

cat("Error writing data to the output file:", 

e$message, "\n") 

}) 

}

Post a Comment

0 Comments