简体   繁体   中英

Using system2() in R to run compiled cpp file

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!!

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, ie store the output in a file.


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM