ElMaestro
★★★

Denmark,
2022-08-30 00:29
(576 d 21:36 ago)

Posting: # 23240
Views: 2,047
 

 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
(576 d 18:55 ago)

@ ElMaestro
Posting: # 23241
Views: 1,693
 

 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
(576 d 11:13 ago)

@ Helmut
Posting: # 23242
Views: 1,681
 

 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
(576 d 08:40 ago)

@ ElMaestro
Posting: # 23243
Views: 1,652
 

 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
(575 d 02:15 ago)

@ Helmut
Posting: # 23244
Views: 1,626
 

 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
(575 d 02:12 ago)

@ ElMaestro
Posting: # 23245
Views: 1,660
 

 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
(574 d 09:32 ago)

@ mittyri
Posting: # 23247
Views: 1,522
 

 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
(574 d 05:58 ago)

@ ElMaestro
Posting: # 23250
Views: 1,548
 

 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,957 posts in 4,819 threads, 1,636 registered users;
74 visitors (0 registered, 74 guests [including 7 identified bots]).
Forum time: 21:05 CET (Europe/Vienna)

Nothing shows a lack of mathematical education more
than an overly precise calculation.    Carl Friedrich Gauß

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