How can I run an 'R' script without suppressing output?
IMHO you need to specify print.eval
parameter set to TRUE
if you want to get only the output (and not the commands). If you would need the commands too, you should set echo
to TRUE
(which implies setting print.eval
to TRUE
).
For example:
source('myscript.R', print.eval = TRUE)
How can I avoid having my R script printed every time I run it?
Resolution is to run with Rscript, and not with R. Examples elsewhere (e.g. How can I read command line parameters from an R script?), run scripts from the command line with
R --args args1 args2... < foo.R
running with
Rscript foo.R args1 args2 ...
produces only the output, and not the script. It's also a much cleaner way to run scripts.
Using Rscript, is there a decent way to suppress the non-script output?
Andrew, I ran into the same thing and suppressMessages()
didn't remove all the extra output, but using sink()
in the form of capture.output()
wrapped around the suppressMessages()
works.
$ rscript --vanilla -e 'library(Rmpfr)'
Loading required package: methods
Loading required package: gmp
---->8----
Loading C code of R package 'Rmpfr': GMP using 32 bits per limb
---->8----
$ rscript --vanilla -e 'suppressMessages( library(Rmpfr) )'
Loading C code of R package 'Rmpfr': GMP using 32 bits per limb
$ rscript --vanilla -e 'msg.out <- capture.output( suppressMessages( library(Rmpfr) ) )'
What is going on when loading the Rmpfr package is several well behaved startup messages written using the message
connection along with a not so nice message using the output
connection. Sure, you could create and manipulate a sink()
on your own, but that is what capture.output()
is already setup to do.
Perhaps setting a verbose arg to get a little more control would be helpful::
$ cat sample.R
#!/c/opt/R/R-2.15.0/bin/rscript --vanilla
cmd_args <- commandArgs( TRUE );
if( length( cmd_args ) > 0 ) {
eval( parse( text = cmd_args[1] ) )
}
if( exists( "verbose" ) ) {
library( Rmpfr )
} else {
msg.trap <- capture.output( suppressMessages( library( Rmpfr ) ) )
}
print("Hello")
Which yields::
$ ./sample.R
[1] "Hello"
$ ./sample.R "verbose=TRUE"
Loading required package: methods
Loading required package: gmp
Attaching package: 'gmp'
---->8----
[1] "Hello"
Lots of stuff you could play around with there, but at least you can see how to totally suppress the msg output.
Hope it helps. Have fun!
Suppressing R script's output
If you want to completely suppress all stderr messages, put this line early in your script:
sink(file("/dev/null", "w"), type="message")
Obviously this isn't going to help with debugging...
Suppress automatic output to console in R
The issue is due to the fact that the function has multiple print
statements, where stop
, warning
, or message
would have been appropriate so that people can use suppressWarnings
or suppressMessages
.
You can work arount it using invisible(capture.output())
around your whole assignment (not just the right side).
f1 <- function(n, ...){
print("Random print statement")
cat("Random cat statement\n")
rnorm(n = n, ...)
}
f1(2)
#> [1] "Random print statement"
#> Random cat statement
#> [1] -0.1115004 -1.0830523
invisible(capture.output(x <- f1(2)))
x
#> [1] 0.0464493 -0.1453540
See also suppress messages displayed by "print" instead of "message" or "warning" in R.
Suppress system() output in R
Use intern=TRUE
in your system call. From the system
help page:
intern
a logical (not NA) which indicates whether to capture the output of the command as an R character vector.
Once you do that you can either use invisible or just store the results to keep them from being printed.
How to show code but hide output in RMarkdown?
As @ J_F answered in the comments, using {r echo = T, results = 'hide'}
.
I wanted to expand on their answer - there are great resources you can access to determine all possible options for your chunk and output display - I keep a printed copy at my desk!
You can find them either on the RStudio Website under Cheatsheets (look for the R Markdown cheatsheet and R Markdown Reference Guide) or, in RStudio, navigate to the "Help" tab, choose "Cheatsheets", and look for the same documents there.
Finally to set default chunk options, you can run (in your first chunk) something like the following code if you want most chunks to have the same behavior:
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = T,
results = "hide")
```
Later, you can modify the behavior of individual chunks like this, which will replace the default value for just the results option.
```{r analysis, results="markup"}
# code here
```
How to suppress output in RStudio?
Use source instead of source with echo in R Studio.
Same in MAC.
Suppress output of a function
It isn't clear why you want to do this without sink
, but you can wrap any commands in the invisible()
function and it will suppress the output. For instance:
1:10 # prints output
invisible(1:10) # hides it
Otherwise, you can always combine things into one line with a semicolon and parentheses:
{ sink("/dev/null"); ....; sink(); }
Related Topics
R Shiny Error: Object Input Not Found
Split Date Data (M/D/Y) into 3 Separate Columns
Get Name of Dataframe Passed Through Pipe in R
Replace Character at Certain Location Within String
Dynamically Adjust Height And/Or Width of Shiny-Plotly Output Based on Window Size
How to Make My Axis Ticks Face Inwards in Ggplot2
Data.Table Inner/Outer Join with Na in Join Column of Type Double Bug
Shrink Dt::Datatableoutput Size
Apply a Function to Each Data Frame
Dplyr::N() Returns "Error: This Function Should Not Be Called Directly"
Convert Daily to Weekly/Monthly Data with R
Reshape Wide to Long with Character Suffixes Instead of Numeric Suffixes
How to Fill a Geom_Area() Plot Using Ggplot
How to Append Data from a Data Frame in R to an Excel Sheet That Already Exists
Rename a Sequence of Variable Names in Data Frame