Skip to content
Prev 343584 / 398506 Next

Not display message when using system()

On Sun, Aug 31, 2014 at 6:49 AM, Marc Girondot <marc_grt at yahoo.fr> wrote:
Yes as documented in Section 'Value' of help("system2"); "In other
cases, the return value is an error code...".
Yes you capture the standard output as documented also in Section
'Value'; "If stdout = TRUE or stderr = TRUE, a character vector giving
the output of the command...".  You also explicitly say you want to
discard any output that the 'find' command sends to standard error
(technical details: that is why that "2>/dev/null" is part of the
call) - if you would have used stderr=TRUE, then an such messages
would be interweaved into the returned value ('pathname').

More from Section 'Value': "If command runs but gives a non-zero exit
status this will be reported with a warning and in the attribute
"status" of the result: an attribute "errmsg" may also be available."
 This explains why you get a warning ("Message d'avis"); the 'status'
of the call is 1.  I don't know why 'find' does that, that is
something 'man find' would explain.

However, since you now properly capture 'pathname' and you get a
warning in R, all you have to do is to suppress that warnings in R.
Use suppressWarning() to do that, e.g.

pathfile <- suppressWarning(system2(command="find", args="$HOME -type
f -name 'PuertoSanJose.csv'", stderr = FALSE, stdout=TRUE))

The following also works:

suppressWarning({
  pathfile <- system2(command="find", args="$HOME -type f -name
'PuertoSanJose.csv'", stderr = FALSE, stdout=TRUE)
})
This will force R to turn a warning into an error as soon as the
warning is generated.
...so then you decide to catch that error.  This also works as
expected.  system2() generates a warning which is turned into an error
which causes system2() to pre-emptively exit/return and try() captures
that and generates a return object explaining what happened.  Note
that system2() never returns anything.  This is more clear if you
would use:

pathfile <- "not yet assigned"
res <- try({
  pathfile <- system2(command="find", args="$HOME -type f -name
'PuertoSanJose.csv'", stderr = FALSE, stdout=TRUE)
}, silent=TRUE)

Here 'pathfile' would still be "not yet assigned" and 'res' would
contain "Error : (converti depuis l'avis) l'ex?cution de la commande
''find' ..." (as above).
This is no difference from:

pathfile <- ""
res <- try({
  pathfile <- system2(command="find", args="$HOME -type f -name
'PuertoSanJose.csv'", stderr = FALSE, stdout=TRUE)
}, silent=TRUE)

So, as above.
Very similar.  FYI, if you ever find yourself using assign() or '<<-'
that's a pretty good sign you're out on dangerous waters and you're
most likely are doing something wrong (unless you really really really
really really know what you're doing).  In such cases, ask for help...
...as you did.

/Henrik