Skip to content

Not display message when using system()

4 messages · Henrik Bengtsson, Marc Girondot

#
Dear list members,

My question concerns the use of system() in R version 3.1.1 patched and 
MacosX 10.9.4.
I want capture the result of a system command without displaying error 
message. I give exemple.

In terminal, if I do this command:
find $HOME -type f -name 'PuertoSanJose.csv'

I get the correct answer but also a message about Permission denied for 
one directory:
/Users/marc/Dropbox/DropBoxPerso/Data_Ale/Original/PuertoSanJose.csv
find: /Users/marc/Library/Saved Application 
State/com.adobe.flashplayer.installmanager.savedState/data.data: 
Permission denied

I want get the output of this command in R; then I do:
 > pathfile <- system("find $HOME -type f -name 'PuertoSanJose.csv'", 
intern=TRUE, ignore.stderr = TRUE)
Message d'avis :
l'ex?cution de la commande 'find $HOME -type f -name 'PuertoSanJose.csv' 
2>/dev/null' renvoie un statut 1

In pathfile, I have the correct answer but I have also a message that I 
don't want.

My question is then: How to prevent display this message?

I try the following:
 > pathfile <- capture.output(system("find $HOME -type f -name 
'PuertoSanJose.csv'", intern=TRUE, ignore.stderr = TRUE))
Message d'avis :
l'ex?cution de la commande 'find $HOME -type f -name 'PuertoSanJose.csv' 
2>/dev/null' renvoie un statut 1

The same

I try also:
 > pathfile <- suppressMessages(system("find $HOME -type f -name 
'PuertoSanJose.csv'", intern=TRUE, ignore.stderr = TRUE))
Message d'avis :
l'ex?cution de la commande 'find $HOME -type f -name 'PuertoSanJose.csv' 
2>/dev/null' renvoie un statut 1

The same

The only solution to not see this message is:
 > pathfile <- system("find $HOME -type f -name 'PuertoSanJose.csv'", 
intern=FALSE, ignore.stderr = TRUE)
/Users/marc/Dropbox/DropBoxPerso/Data_Ale/Original/PuertoSanJose.csv
 > pathfile
[1] 1

But pathfile does not capture the output.

And the use of capture.output() does not help:
 > pathfile <- capture.output(system("find $HOME -type f -name 
'PuertoSanJose.csv'", intern=FALSE, ignore.stderr = TRUE))
/Users/marc/Dropbox/DropBoxPerso/Data_Ale/Original/PuertoSanJose.csv
 > pathfile
character(0)


I really don't know how to not see this message...
If someone knows, I will appreciate !

Marc
#
As a start try to use system2() instead and look at its argument for how to
capture stdout and/or stderr. It's a neater function.

It may be that those messages cannot be captured easily, but hopefully they
are.

My $0.02

Henrik
On Aug 29, 2014 12:21 PM, "Marc Girondot" <marc_grt at yahoo.fr> wrote:

            

  
  
1 day later
#
Dear Henrik and list-members,

Thanks for your proposition but it is the same:

For example, no error message but no result:
 > pathfile <- system2(command="find", args="$HOME -type f -name 
'PuertoSanJose.csv'", stderr = FALSE, stdout="")
/Users/marc/Dropbox/DropBoxPerso/Data_Ale/Original/PuertoSanJose.csv
 > pathfile
[1] 1

Example 2 with error message but I get the result !
 > pathfile <- system2(command="find", args="$HOME -type f -name 
'PuertoSanJose.csv'", stderr = FALSE, stdout=TRUE)
Message d'avis :
l'ex?cution de la commande ''find' $HOME -type f -name 
'PuertoSanJose.csv' 2>/dev/null' renvoie un statut 1
 > pathfile
[1] "/Users/marc/Dropbox/DropBoxPerso/Data_Ale/Original/PuertoSanJose.csv"
attr(,"status")
[1] 1

I try another solution with options(warn=2) and try(xxx, silent=TRUE) to 
convert the warning in error and mask the error, but I cannot get the 
result anymore:
 > options(warn=2)
 > pathfile <- try(system2(command="find", args="$HOME -type f -name 
'PuertoSanJose.csv'", stderr = FALSE, stdout=TRUE), silent=TRUE)
 > pathfile
[1] "Error : (converti depuis l'avis) l'ex?cution de la commande ''find' 
$HOME -type f -name 'PuertoSanJose.csv' 2>/dev/null' renvoie un statut 1\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<simpleError: (converti depuis l'avis) l'ex?cution de la commande 
''find' $HOME -type f -name 'PuertoSanJose.csv' 2>/dev/null' renvoie un 
statut 1>

I try also this combinations but without success:
 > pathfile <- ""
 > try(assign("pathfile", system2(command="find", args="$HOME -type f 
-name 'PuertoSanJose.csv'", stderr = FALSE, stdout=TRUE), 
envir=globalenv()), silent=TRUE)
 > pathfile
[1] ""
 > try({pathfile <<- system2(command="find", args="$HOME -type f -name 
'PuertoSanJose.csv'", stderr = FALSE, stdout=TRUE)}, silent=TRUE)
 > pathfile
[1] ""

Sooo... any other idea ?

Thanks a lot

Marc

Le 29/08/2014 22:33, Henrik Bengtsson a ?crit :

  
  
#
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