Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running linux commands from R

Tags:

linux

r

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

like image 766
user2543622 Avatar asked Feb 12 '23 04:02

user2543622


1 Answers

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"
  • http://stat.ethz.ch/R-manual/R-devel/library/base/html/system.html

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"
  • http://stat.ethz.ch/R-manual/R-devel/library/base/html/Sys.glob.html
like image 144
nodakai Avatar answered Feb 14 '23 16:02

nodakai