R includes some powerful yet easy to use functions that provide a direct interface to the computer’s file system. In the example below I use two of these functions (file.info and file.copy) to copy a random selection of the songs on my computer to my mp3 player, while making sure that the combined size of these files does not exceed 500MB.
First step is to extract the file name and size information for the Music directory.
|mylist <- file.info(list.files(path=”/media/MyHD/Music/”, recursive=TRUE, full.names=TRUE))|
Next step is not strictly needed, but I just like to have a dataframe with only the two columsn with the file name and size. I create the dataframe and column names in one go using the ‘structure’ function (I found out about this option here).
|mylist <- structure(cbind(as.data.frame(row.names(mylist)), as.numeric(mylist[,”size”])), names=c(“file”,”size”))|
The last steps are (i) to select all files that the media player can play (mp3, mp4, m4a, mav,wma), (ii) to shuffle the list, (iii) to create additional column with cumulative file sizes, (iv) to select the subset which together is <= 500MB, and finally, (v) to copy the files to your mp3 player.
mylist <- mylist[grep(‘.mp3|.mp4|.mp4|.m4a|.mav|.wma’,mylist[,1], extended=TRUE),]
mylist <- mylist[sample(row.names(mylist)),]
mylist$cumsize <- cumsum(mylist[,2])
selectlist <- subset(mylist,cumsize <= 500000000)
In Unix-like systems it is fairly easy to create a command-line tool based on this code. As explained here, you need to create an text file where the first line is #!/usr/bin/Rscript followed by the R code given above (note that you’ll need to replace /usr/bin with your actual R executable installation directory). Next, the file need to be made executable (chmod +x) and included in your PATH, after which it will be available as a command-line tool.