Search code examples
rcsvdataframesystemstdout

R read data from stdout of system command into data frame


I call a system command with

driveFileList <- try(as.data.frame(system(paste0("/usr/local/bin/gdrive list "), intern = TRUE, ignore.stderr = TRUE)))

I check the result with

print(dim(driveFileList))
print(typeof(driveFileList))

and I see that I have a list with 3 rows and 0 columns.

But the system command gives me actually something like this

1  Id             Name              Type              Created
2  23423423       nameOfFile1.csv   doc               2016-08-22 18:40:05
3  2342342        nameOfFile2.csv   doc               2016-08-22 18:39:39
4  34323334       nameOfFile3.csv   doc               2016-08-23 17:58:46

How can I 'explode' this, that I get a real data frame?

Thanks Joerg


Solution

  • as.data.frame() doesn’t do what you think it does. The system() command gives you back a character string (of some format), so you need to parse it. Try read.table() or a similar function (which exactly to use, and what parameters, depends on the exact output of the call).

    You either need to pass the text into the function via its text argument (i.e. result = read.table(text = system_output, …)) or you could use pipe() instead of system(), and read the resulting stream.

    Looking at the readme of gdrive, it looks as if read.fwf() would be more appropriate than read.table() in your instance.