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

1
  • 2
    see ?system ... see particular the internal argument. Commented Aug 26, 2014 at 23:07

1 Answer 1

10

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"
Sign up to request clarification or add additional context in comments.

2 Comments

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.
It heavily depends on the format of each particular file... I recommend you to read "2.1 Variations on read.table" of cran.r-project.org/doc/manuals/r-release/R-data.pdf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.