1

I'm trying to run a compiled cpp file in my R program using system2(). The documentation for the cpp suggests that it's just one big command, so I'm thinking I'm not supposed to use the stdout or stder options in sys2.

  • the required network.nodes and network.edges files are there in the /files folder
  • I can run the system2() line but it doesn't output anything
  • I previously compiled the socialrank.cpp and put it in the /exe folder using Cygwin or cmd prompt maybe (g++ -o socialrank socialrank.cpp)

Guidance: - To run the algorithm, simply run: ./socialrank summary_stats.txt graphname > debug.log (You need to have the files graphname.nodes and graphname.edges)

My code (let me know if you need to see more):

> nodelist %>% write_delim("./files/network.nodes", col_names = F)  
> edgelist %>% write_delim("./files/network.edges", col_names = F)
> #system("../exe/socialrank ../files/summary_stats.txt ../files/network")   #I think this code is for macs?? 
> system2("./exe/socialrank ./files/summary_stats.txt ./files/network") #Is this how you correct relative file directories for Windows?

So nothing is being output into the /files folder. I can't tell if the CPP file is being run, not exporting files, or exporting them somewhere else?

Please let me know if you any suggestions on compiling, calling cpp programs, or the system2 function. I've also heard about the sys and processx packages, so not sure if there is a better way to call system files that perhaps works across operating systems?

Thank you so much for your help!!

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
D Schiff
  • 91
  • 9
  • I’m quite unhappy with the terminology here (because I don’t understand what exactly you mean): a “compiled C++ file” is an object file, and can’t be directly executed. A compiled C++ application (note: *not* “file”!) is just an application, and it’s irrelevant where it comes from. I’m also unsure what you mean by “documentation for the cpp”. – Konrad Rudolph Jun 25 '18 at 14:00
  • Apologies, I am obviously a major novice. So there's a .cpp file (program?). After compiling, it becomes a .exe (that's called an object file? or application?). The cpp file itself came with a readme, including how to compile and execute it. When you say it's irrelevant where it comes from - does that mean I don't need to indicate its file directory location? – D Schiff Jun 25 '18 at 14:10
  • I am removing the `rcpp` tag as this has _nothing_ to do with combing R and C++ code via Rcpp -- it is about execution of existing binaries on one particular OS. Good luck. – Dirk Eddelbuettel Jun 25 '18 at 14:16
  • @DSchiff No, I mean that it’s (in this case) irrelevant that it was built from a C++ source. Application is application. Anyway, have a look at my answer. – Konrad Rudolph Jun 25 '18 at 14:18

1 Answers1

0

The documentation for system2 gives us two pieces of information:

  1. We need to specify the command to be executed and the args as separate arguments.
  2. By default, the return value of system2 is invisible, and it’s the status code of the command we executed.

The second point is the reason you’re not seeing any output.1 The first point is the reason why it doesn’t work in the first place: you need to specify the command and its arguments separately (and the arguments need to be a vector):

system2('./exe/socialrank', c('./files/summary_stats.txt', './files/network'))

This assumes that exe and files are subdirectories of the current working directory (and that the respective files exist in these locations).

In your case, the same command works for macOS, Windows and Linux.

Anyway, this is not quite the same as the example given in the usage guidance:

./socialrank summary_stats.txt graphname > debug.log

… because in the command above, output isn’t stored in a debug.log file but sent to the R console. This is very rarely useful. It’s much more common that you want to store the output itself in a variable in R. You can do that by adding the argument stdout = TRUE to the system2 call. Alternatively, specify stdout = 'debug.log' to do the same as the command above, i.e. store the output in a file.


1 Actually, on my system I still get a message: “[…] command not found”.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • With your adjustments, it is indeed calling the cpp file and I can see it running in the R console. But it's not storing output in the summary_stats.txt or network files. I tried adding the stdout: system2('./exe/socialrank', c('./files/summary_stats.txt', './files/network', stdout='debug.log')) #works?? But it doesn't seem to do anything different. For referenc, the next thing in the R code is to read in the file that was supposed to be produced: hierarchy <- read_delim("./files/network.ranks", delim = " ") – D Schiff Jun 25 '18 at 15:12
  • Given that I'm just reading that file back into R, I suppose it would work to just store it as an R object (but not sure what format that would be). And the input files are in indeed the /exe and /files folders, and I'm expecting the output files to be written in /files, but they're not showing up anywhere – D Schiff Jun 25 '18 at 15:20
  • Still no luck getting output - either in a file (preferred) or as an R object system2('./exe/socialrank', c('./files/summary_stats.txt', './files/network')) #runs but won't store to R system2('./exe/socialrank', c('./files/summary_stats.txt', './files/network', stdout=TRUE)) #runs but won't store to R system2('./exe/socialrank', c('./files/summary_stats.txt', './files/network', stdout='./files/debugSR.log')) #runs but won't store to file – D Schiff Jun 27 '18 at 12:54
  • @DSchiff I’m confused. What’s the contents of the file `./files/debugSR.log` after the last command is run? Is it empty? Is any output displayed on the console? – Konrad Rudolph Jun 27 '18 at 13:03
  • So r prints out stuff in the console... but the exe doesn't create a file in my /files directory. I'm expecting a network.ranks file, mabye a summary_stats.txt file. But no files are produced on my computer. Just some output on the r console that I don't know how to get in the right format: [1] " Here ? \r\nnodefile\r\nedgefile \r\nBasic reading done. \r\nopening file:./files/network.nodes\r\n0 1\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n12 13\r\n13 14\r\n14 15\r\n15 16\r\n16 1... – D Schiff Jun 27 '18 at 13:17
  • @DSchiff That sounds like a bug in the application you’re trying to call. Have you tried running it outside R? – Konrad Rudolph Jun 27 '18 at 13:28
  • Embarrassed to say I'm not sure how. If I just double click on the exe, it opens in CMD prompt, is blank, and closes quickly, no files produced. But it DOES seem to be doing the right stuff in the R console. Should I share the code or exe with you? – D Schiff Jun 27 '18 at 13:30
  • @DSchiff I couldn’t execute it on my system. If you want to execute it outside of R you need to familiarise yourself with the command line. – Konrad Rudolph Jun 27 '18 at 13:40
  • 1
    Got it working using cmder. Now it outputs networks.ranks and summary_stats.txt as files, huge thank you! One of the issues was my antivirus was blocking the exe files :) – D Schiff Jun 27 '18 at 13:59