file.split {NCmisc} | R Documentation |
Split a text file into multiple parts
Description
Wrapper for the bash command 'split' that can separate a text file into multiple roughly equal sized parts. This function removes the need to remember syntax and suffixes of the bash command
Usage
file.split(
fn,
size = 50000,
same.dir = FALSE,
verbose = TRUE,
suf = "part",
win = TRUE
)
Arguments
fn |
character, file name of the text file to split, if the file is an imcompatible format the linux command should return an error message to the console |
size |
integer, the maximum number of lines for the split parts of the file produced |
same.dir |
logical, whether the resulting files should be moved to the same directory as the original file, or simply left in the working directory [getwd()] |
verbose |
logical, whether to report the resulting file names to the console |
suf |
character, suffix for the split files, default is 'part', the original file extension will be appended after this suffix |
win |
logical, set to FALSE if running a standard windows setup (cmd.ext), and the file split will run natively in R. Set to TRUE if you have a unix-alike command system, such as CygWin, sh.exe, csh.exe, tsh.exe, running, and this will then check to see whether the POSIX 'split' command is present (this provides a speed advantage). If in doubt, windows users can always set win=TRUE; the only case where this will cause an issue is if there is a different command installed with the same name (i.e, 'split'). |
Value
returns the list of file names produced (including path)
Author(s)
Nicholas Cooper
Examples
orig.dir <- getwd(); setwd(tempdir()); # move to temporary dir
file.name <- "myfile.txt"
writeLines(fakeLines(max.lines=1000),con=file.name)
new.files <- file.split(file.name,size=50)
unlink(new.files); unlink(file.name)
setwd(orig.dir) # reset working dir to original