system and system2 in R [Software]

posted by ElMaestro  – Denmark, 2022-08-30 00:29 (96 d 14:43 ago) – Posting: # 23240
Views: 786

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)
print(x)

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)
print(x)


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="foo.bar" etc and this creates a foo.bar file, but that file does not contain the gcc output.
I also tried sink("foo.bar") 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!
ElMaestro

Complete thread:

UA Flag
Activity
 Admin contact
22,428 posts in 4,694 threads, 1,598 registered users;
10 visitors (0 registered, 10 guests [including 5 identified bots]).
Forum time: 14:12 CET (Europe/Vienna)

Operational hectic replaces
intellectual calms.    Alexander Huiskes

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