5

I have a bunch of random files and I am going to run LINUX-file command on each file. Linux screen will be as follows

m7% file date-file.csv
date-file.csv: ASCII text, with CRLF line terminators
m7% file image-file.JPG
image-file.JPG: JPEG image data, EXIF standard

Only when Linux says that the file is a text file, I want to run a R script that goes through that file and finds all column names. In above screen, I want to run R script only on the first file. How could I achieve this conditional processing?

Is there any way I can run Linux commands from R? If i can do that then I can analyze the output given by Linux command to see if it contains text and then I can execute R script if required.

I am having difficulty achieving this and any help is appreciated

user2543622
  • 5,760
  • 25
  • 91
  • 159

1 Answers1

9

Try system()

08/27 7:08 [nodakai@kaidev01] ~/R$ R -q
> system("ls /")
bin  boot  dev  etc  home  initrd.img  initrd.img.old  lib  lib32  lib64  libGL.so  libnss3.so  lost+found  media  mnt  opt  proc  root  run  sbin  selinux  sftp  srv  sys  tmp  usr  var  vmlinuz  vmlinuz.old
> x = system("ls", TRUE)
> x
[1] "a.csv"                       "abi.pdf"                    
[3] "b.csv"                       "image.jpeg"                 
[5] "x86_64-pc-linux-gnu-library"
> for (f in system("ls", TRUE)) { if (length(grep("ASCII", system(paste("file", f), TRUE)))) { print(f) }  }
[1] "a.csv"
[1] "b.csv"

Globbing

If you're sure all files ending ".csv" are really plain text CSV files, just use Sys.glob()

> list.files()
[1] "a.csv"                       "abi.pdf"                    
[3] "b.csv"                       "image.jpeg"                 
[5] "x86_64-pc-linux-gnu-library"
> Sys.glob("*.csv")
[1] "a.csv" "b.csv"
nodakai
  • 7,773
  • 3
  • 30
  • 60
  • I won't have csv files everytime. In that case any idea how could I export them to R? From Linux command I will know that it is a "text" file. – user2543622 Aug 27 '14 at 01:38
  • It heavily depends on the format of each particular file... I recommend you to read "2.1 Variations on read.table" of http://cran.r-project.org/doc/manuals/r-release/R-data.pdf – nodakai Aug 27 '14 at 04:43