RMHogervorst / blog

posts about R
https://blog.rmhogervorst.nl
2 stars 1 forks source link

blogidea #14

Closed roelhogervorst closed 4 years ago

roelhogervorst commented 4 years ago

title: "where do file outputs go" output: github_document

knitr::opts_chunk$set(echo = TRUE)

I created this file message_warning.R

#!/usr/bin/env Rscript
print("this is a print \n")
cat("this is a cat \n")
message("this is a message \n")
warning("this is a warning")
stop("this is a stop, or error!")

If you execute this file with Rscript

Rscript message_warning.R You get this

[1] "this is a print \n"
this is a cat
this is a message

Warning message:
this is a warning
Error: this is a stop, or error!
Execution halted

However, if you pipe the output into a file you only get the stout

Rscript message_warning.R > stuff.txt

The shell returns

this is a message

Warning message:
this is a warning
Error: this is a stop, or error!
Execution halted

and stuff.txt contains

[1] "this is a print \n"
this is a cat 

But we can also write the sterr to file.

Rscript messgae_warning.R 2> stuff.txt

Now the shell outputs

[1] "this is a print \n"
this is a cat

and the stuff.txt file contains

this is a message 

Warning message:
this is a warning 
Error: this is a stop, or error!
Execution halted

How do you get them both?

Rscript message_warning.R &> stuff.txt
RMHogervorst commented 4 years ago

created post