Objective: We will practice scripting commands in R,
including file creation, editing, sourcing scripts, and using
sink()
to capture and redirect output.
Problem Statement:
Perform the following tasks in R:
Create a script file named
command_example.R
.
Edit the script file using file.edit()
to
add the following content:
Copy & paste code from here.
Execute the script file command_example.R
and print the results.
Redirect the output of the script to a text file named
result_command_example.txt
.
Stop redirecting output.
Hints:
Use file.create()
to create the script
file.
Use file.edit()
to modify the script file with the
provided content.
Use source("")
to execute the script and display its
output in the console.
Use sink("file_name.txt")
to redirect R output to a
text file.
Use sink()
without arguments to stop redirecting
output.
Challenge Question: How would you modify the script to handle errors or warnings that might occur during script execution? What methods can you use to capture and handle such messages effectively?
This exercise will help students practice fundamental scripting tasks
in R, including file handling, sourcing scripts, and managing script
output using sink()
, preparing them for more complex data
processing and automation tasks in R.