Suppose I have the following function:
## Just an example
f = function() {
for(i in 1:10000)
cat(i)
return(1)
}
When I call f()
is there a way to stop cat
printing to the screen (without altering the function in anyway)?
Reason behind this question
My students upload their R files. I then run the scripts and check to see if they are correct. Every so often, a student leaves in the cat
command. This is especially irritating when it's in a long for loop
cat command ( short for concatenate) is one of the powerful command to create single or multiple files, view contents of the file, concatenate multiple files, copy the content to other files, and print output to terminal or file.
txt, the cat command using the > operator WILL overwrite it. To get back to the command line and create the text file we use CTRL + D. If we wanted to append or add more text to these files we would use cat >> FILENAME and input the text we want to use.
In order to stop/exit/quit cat command, you can simply press key q, you should return to the prompt.
To exit the prompt and write the changes to the file, hold the Ctrl key and press d.
On Linux, you can use a sink()
call to /dev/null
(or to a temporary file on another OS, see ?tempfile
) :
sink(file="/dev/null")
f()
sink()
This should work?
oldcat = cat
cat = function( ..., file="", sep=" ", fill=F, labels=NULL, append=F ) {}
f()
cat = oldcat
Just replace cat
with an empty function
, and then set it back on completion
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With