system and system2 in R [Software]

posted by ElMaestro  – Denmark, 2022-08-30 00:29 (596 d 03:46 ago) – Posting: # 23240
Views: 2,115

Hi all,

I am experimenting with Linux these days. Kubuntu. So far all looks very well.

I have a little issue with R:
I need to spawn the gcc compiler now and then from within R. That's easily done with the system or system2 functions. But I also need to capture the console output that is produced by gcc. And that's my trouble.

To illustrate my issue, please consider this example:

x=system(command="dir", intern=T)

And this nicely prints the directory content. The intern argument tells we want to collect the output as string(s).

So, along the same lines I hope to execute gcc as:
x=system(command="gcc -Wall -O2 myfile.c -o Target", intern=T)

and even though gcc does the compilation nicely, x is -surprisingly to me- empty. It is not because gcc does not spit out info and the command is fully ok [foot note 1]. gcc prints plenty lines to the console. I just do not know how to capture them with R.
If I switch to system2 the issue is the same; I play around with stdout="" etc and this creates a file, but that file does not contain the gcc output.
I also tried sink("") prior to the system or system2 call and the result is still an empty file.

Do one of you experts know how to capture the gcc output via system or system2 or some other way?

You can try the following simpler example:
a=system(command="hamburger", intern=T)
The following is printed to the console:
sh: 1: hamburger: not found
Error in system(command = "hamburger", intern = T) :
error in running command

But this isn't the content of the variable a as I was hoping. Note that in this example, an error is produced but not in the R sense, only in the system (or system2) sense. Thus, there is no gain in putting the code into try{} brackets.

Foot note 1: If any of you wonder what the command above means the narrative is as follows: "compile the file called myfile.c and produce an executable called Target while showing warnings and optimising little bit." But the meaning is beside the point.

Pass or fail!

Complete thread:

UA Flag
 Admin contact
22,984 posts in 4,822 threads, 1,654 registered users;
50 visitors (0 registered, 50 guests [including 7 identified bots]).
Forum time: 04:15 CEST (Europe/Vienna)

Complex, statistically improbable things are by their nature
more difficult to explain than
simple, statistically probable things.    Richard Dawkins

The Bioequivalence and Bioavailability Forum is hosted by
BEBAC Ing. Helmut Schütz