ElMaestro ★★★ Denmark, 2022-08-30 00:29 (777 d 12:12 ago) Posting: # 23240 Views: 3,247 |
|
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="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 ★★★ Vienna, Austria, 2022-08-30 03:10 (777 d 09:30 ago) @ ElMaestro Posting: # 23241 Views: 2,679 |
|
Hi ElMaestro, ❝ I need to spawn the gcc compiler now and then from within R. That's easily done with the ❝ ❝ So, along the same lines I hope to execute gcc as: ❝ ❝ 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
hw.c gcc-x86_64-w64 10.3.0 called from R 4.1.2:
hw.exe In R 4.2.1:
PS: This thread is perfectly On-topic. — Dif-tor heh smusma 🖖🏼 Довге життя Україна! Helmut Schütz The quality of responses received is directly proportional to the quality of the question asked. 🚮 Science Quotes |
ElMaestro ★★★ Denmark, 2022-08-30 10:52 (777 d 01:49 ago) @ Helmut Posting: # 23242 Views: 2,669 |
|
Hi Helmut, ❝ I tried this ❝
❝ int main() { ❝ return 0; ❝ } saved ashw.c ❝ gcc-x86_64-w64 10.3.0 called from R 4.1.2: ❝
❝ character(0) compiles tohw.exe ❝ In R 4.2.1: ❝
❝ [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 ★★★ Vienna, Austria, 2022-08-30 13:25 (776 d 23:15 ago) @ ElMaestro Posting: # 23243 Views: 2,635 |
|
Hi ElMaestro, not sure why your original attempt failed. A new one, missing the IO-declaration saved as FawltyTowers.c
FawltyTowers.c compiled successfully to FawltyTowers.exe , which differs from hw.exe only in the leading bytes at addresses 00088 and 000DB .
— Dif-tor heh smusma 🖖🏼 Довге життя Україна! Helmut Schütz The quality of responses received is directly proportional to the quality of the question asked. 🚮 Science Quotes |
mittyri ★★ Russia, 2022-08-31 19:50 (775 d 16:51 ago) @ Helmut Posting: # 23244 Views: 2,624 |
|
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 (775 d 16:47 ago) @ ElMaestro Posting: # 23245 Views: 2,647 |
|
Hi ElMaestro, ❝ You can try the following simpler example: ❝ ❝ 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 maybe I misunderstood your goal but won't tryCatch( work? — Kind regards, Mittyri |
ElMaestro ★★★ Denmark, 2022-09-01 12:33 (775 d 00:07 ago) @ mittyri Posting: # 23247 Views: 2,540 |
|
Hi mittyri, ❝ maybe I misunderstood your goal but won't ❝ ❝ 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( 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 (774 d 20:33 ago) @ ElMaestro Posting: # 23250 Views: 2,542 |
|
Hi ElMaestro, ❝ It works with the hamburger example but not when I invoke gcc : ❝ ❝ ❝ + { ❝ + 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) : ❝ > a ❝ character(0) ❝ attr(,"status") ❝ [1] 1 OK, a second attempt with already mentioned processx package tryCatchHamburger <- function(string) { — Kind regards, Mittyri |