Dev
Dev

Reputation: 23

Unable to read output from System Command executed in R

I am new to R. So please bear with me if the question seems to be simple to you.

I was trying to execute a java program from R.

randomGraphPoints<-function(exec="randomcoordinates")
    {
      command=paste("java -cp . ",exec)
      system(command)
    }

The above function randomGraphPoints when executed shows an output as shown below:

> source("Hello.R")
> randomGraphPoints()
(-12,7,-6,-7,10,-6,5,-8,-4,2,10,-12,-4,-13,-1,-15,12,12,1,8,-11,-13,-6,-3,6,-3,1,12,10,10,-8,15,-15,6,1,4,-13,-8,-6,2)

My plan is to create a matrix which consists of 2 columns. The output should be like:

-12,7
-6,-7
10,-6
5,-8
-4,2
10,-12
....
-6,2

and then plot a graph using Graphviz.

I am stuck at the stage where I need to assign the output from System call in R to a R Variable.

Please advice me on how to proceed.

TIA

Upvotes: 1

Views: 61

Answers (1)

Rich Scriven
Rich Scriven

Reputation: 99331

Use intern = TRUE to get proper R output from system(). Then you can safely assign the reault. So try changing last line of the function to

system(command, intern = TRUE)

So then the function would be

randomGraphPoints <- function(exec = "randomcoordinates") {
    command = paste("java -cp . ", exec)
    system(command, intern = TRUE)
}

And then you can do

x <- randomGraphPoints()
matrix(scan(text = x, sep = ","), ncol = 2, byrow = TRUE)

Upvotes: 1

Related Questions