ElMaestro
★★★

Denmark,
2022-08-30 00:29
(544 d 15:51 ago)

Posting: # 23240
Views: 1,898
 

 system and system2 in R [Software]

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
Helmut
★★★
avatar
Homepage
Vienna, Austria,
2022-08-30 03:10
(544 d 13:09 ago)

@ ElMaestro
Posting: # 23241
Views: 1,561
 

 system and system2 in R

Hi ElMaestro,

❝ 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.


❝ 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.


I tried this

#include <stdio.h>
int main() {
  printf("Hello, world!\n");
  return 0;
}

saved as hw.c
gcc-x86_64-w64 10.3.0 called from R 4.1.2:

system(command = "gcc -Wall -O2 hw.c -o hw", intern = TRUE)
character(0)

compiles to hw.exe
In R 4.2.1:

system(command = "hw", intern = TRUE)
[1] "Hello, world!"

Do I understand you correctly that you want to get eventual warnings / errors of the compiler in R? If yes, your approach cannot work. Since the file compiled successfully, R cannot ‘know’ of any problems. You have to tell the compiler to redirect them to a file and read it in R. Perhaps this thread at stackoverflow helps.

PS: This thread is perfectly On-topic.

Dif-tor heh smusma 🖖🏼 Довге життя Україна! [image]
Helmut Schütz
[image]

The quality of responses received is directly proportional to the quality of the question asked. 🚮
Science Quotes
ElMaestro
★★★

Denmark,
2022-08-30 10:52
(544 d 05:28 ago)

@ Helmut
Posting: # 23242
Views: 1,547
 

 system and system2 in R

Hi Helmut,

❝ I tried this

#include <stdio.h>

❝ int main() {

❝   printf("Hello, world!\n");
❝   return 0;

❝ }

saved as hw.c

❝ gcc-x86_64-w64 10.3.0 called from R 4.1.2:

system(command = "gcc -Wall -O2 hw.c -o hw", intern = TRUE)

character(0)

compiles to hw.exe

❝ In R 4.2.1:

system(command = "hw", intern = TRUE)

[1] "Hello, world!"


❝ Do I understand you correctly that you want to get eventual warnings / errors of the compiler in R? If yes, your approach cannot work. Since the file compiled successfully, R cannot ‘know’ of any problems.


It is about capturing the info that gcc prints, regardless of what that info is. Doesn't matter if it is error or success or a recipe for apple pie. When you ran the system command on hw.c, gcc probably gave you a message. It is that message (in your case that the compilation is successful) I need to capture in a variable.

❝ You have to tell the compiler to redirect them to a file and read it in R. Perhaps this thread at stackoverflow helps.


Yes, I have been there prior to posting. Pipes, stdin, stdout and God knows what. I am too old for this. :-)

Pass or fail!
ElMaestro
Helmut
★★★
avatar
Homepage
Vienna, Austria,
2022-08-30 13:25
(544 d 02:54 ago)

@ ElMaestro
Posting: # 23243
Views: 1,518
 

 system and system2 in R

Hi ElMaestro,

not sure why your original attempt failed.

A new one, missing the IO-declaration saved as FawltyTowers.c

int main() {
  printf("Hello, world!\n");
  return 0;
}

Then

x <- system(command = "gcc -Wall -O2 FawltyTowers.c -o FawltyTowers", intern = TRUE)
print(x)
[1] "FawltyTowers.c: In function 'main':"
[2] "FawltyTowers.c:2:3: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]"
[3] "    2 |   printf(\"Hello, world!\\n\");"
[4] "      |   ^~~~~~"
[5] "FawltyTowers.c:2:3: warning: incompatible implicit declaration of built-in function 'printf'"           
[6] "FawltyTowers.c:1:1: note: include '<stdio.h>' or provide a declaration of 'printf'"
[7] "  +++ |+#include <stdio.h>"
[8] "    1 | int main() {"

The compiler is tolerant (warnings not a problem). FawltyTowers.c compiled successfully to FawltyTowers.exe, which differs from hw.exe only in the leading bytes at addresses 00088 and 000DB.

system(command = "FawltyTowers", intern = TRUE)
[1] "Hello, world!"


Dif-tor heh smusma 🖖🏼 Довге життя Україна! [image]
Helmut Schütz
[image]

The quality of responses received is directly proportional to the quality of the question asked. 🚮
Science Quotes
mittyri
★★  

Russia,
2022-08-31 19:50
(542 d 20:30 ago)

@ Helmut
Posting: # 23244
Views: 1,496
 

 processx package

Hi Helmut and ElMaestro,

❝ Do I understand you correctly that you want to get eventual warnings / errors of the compiler in R? If yes, your approach cannot work. Since the file compiled successfully, R cannot ‘know’ of any problems. You have to tell the compiler to redirect them to a file and read it in R.


take a look at processx package. I think it is possible to capture everything without writing/reading

Kind regards,
Mittyri
mittyri
★★  

Russia,
2022-08-31 19:53
(542 d 20:27 ago)

@ ElMaestro
Posting: # 23245
Views: 1,529
 

 tryCatch in R

Hi ElMaestro,

❝ 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.


maybe I misunderstood your goal but won't
tryCatch(
  {   
    a <- system(command="hamburger", intern = T)
  }, 
  error = function(cond) {
    a <<- cond
  }
)

work?

Kind regards,
Mittyri
ElMaestro
★★★

Denmark,
2022-09-01 12:33
(542 d 03:46 ago)

@ mittyri
Posting: # 23247
Views: 1,391
 

 tryCatch in R

Hi mittyri,

❝ maybe I misunderstood your goal but won't

tryCatch(

❝   {   
❝     a <- system(command="hamburger", intern = T)
❝   }, 
❝   error = function(cond) {
❝     a <<- cond
❝   }

❝ )

❝ work?


Thank you for the proposal. :-)
It works with the hamburger example but not when I invoke gcc :

> tryCatch(
+   {   
+     a <- system("gcc -O2 -Wall Tester301.c -o T301", intern=T)
+   },
+   error = function(cond) {
+     a <<- cond
+   }
+ )

cc1: fatal error: Tester301.c: No such file or directory
compilation terminated.
Warning message:
In system("gcc -O2 -Wall Tester301.c -o T301", intern = T) :
  running command 'gcc -O2 -Wall Tester301.c -o T301' had status 1

> a
character(0)
attr(,"status")
[1] 1


So given the difference in behaviour I have a feeling this is about stderr and stdout. My own understanding of these concepts is rudimentary, to say the least.
R must be listening on both channels, so to say. Need to play a little more around with the ">" and ">2" commandline switches.

Pass or fail!
ElMaestro
mittyri
★★  

Russia,
2022-09-01 16:07
(542 d 00:13 ago)

@ ElMaestro
Posting: # 23250
Views: 1,418
 

 tryCatch in R

Hi ElMaestro,

❝ It works with the hamburger example but not when I invoke gcc :


> tryCatch(

❝ +   {   

❝ +     a <- system("gcc -O2 -Wall Tester301.c -o T301", intern=T)

❝ +   },

❝ +   error = function(cond) {

❝ +     a <<- cond

❝ +   }

❝ + )

cc1: fatal error: Tester301.c: No such file or directory

❝ compilation terminated.

❝ Warning message:

❝ In system("gcc -O2 -Wall Tester301.c -o T301", intern = T) :

❝   running command 'gcc -O2 -Wall Tester301.c -o T301' had status 1

> a

character(0)

❝ attr(,"status")

❝ [1] 1


OK, a second attempt with already mentioned processx package
tryCatchHamburger <- function(string) {
  # initializing to prevent 'a' to go higher closures in case of error
  a <- NULL
  tryCatch({
    a <- processx::run(string, stderr = NULL)
  },
  error = function(cond) {
    a <<- cond
  })
  a
}

b <- tryCatchHamburger("Hamburger")
class(b)
# [1] "c_error"        "rlib_error_3_0" "rlib_error"     "error"          "condition" 
print(b$parent$message)
# [1] "cannot start processx process 'gcc -O2 -Wall Tester301.c -o T301' (system error 2, No such file or directory) @unix/processx.c:613 (processx_exec)"
c <- tryCatchHamburger("gcc -O2 -Wall Tester301.c -o T301")
class(c)
# [1] "c_error"        "rlib_error_3_0" "rlib_error"     "error"          "condition"   
print(c$parent$message)
# [1] "cannot start processx process 'gcc -O2 -Wall Tester301.c -o T301' (system error 2, No such file or directory) @unix/processx.c:613 (processx_exec)"

Kind regards,
Mittyri
UA Flag
Activity
 Admin contact
22,911 posts in 4,806 threads, 1,635 registered users;
36 visitors (0 registered, 36 guests [including 3 identified bots]).
Forum time: 15:20 CET (Europe/Vienna)

The history of statistics is like a telephone directory:
the plot is boring, full of numbers and the cast is endless.    Stephen Senn

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