[Experimental]

split_file() split a single file into n parts.

split_file(file = file.choose(), n, dir = dirname(file), has_header = FALSE)

Arguments

file

(optional) a string indicating the file path. (default:: base::file.choose()).

n

An integer number indicating the amount of parts to split the file.

dir

(optional) a string indicating the directory where to write the file parts. (default:: base::dirname(file)).

has_header

(optional) a logical flag indicating if the file has a header (e.g., a CSV file). If TRUE, the header will be repeated as the first line in every part (default: FALSE).

Value

An invisible NULL. This function don't aim to return values.

Examples

file_name <- tempfile(tmpdir = tempfile())
dir_name <- dirname(file_name)
dir.create(dir_name)
file.create(file_name)
#> [1] TRUE

con <- file(file_name, "r+")
data <- as.character(0:100)
writeLines(data, con = con)
close(con)

split_file(file = file_name, n = 3)

con <- file(file_name, "r+")
readLines(con)
#>   [1] "0"   "1"   "2"   "3"   "4"   "5"   "6"   "7"   "8"   "9"   "10"  "11" 
#>  [13] "12"  "13"  "14"  "15"  "16"  "17"  "18"  "19"  "20"  "21"  "22"  "23" 
#>  [25] "24"  "25"  "26"  "27"  "28"  "29"  "30"  "31"  "32"  "33"  "34"  "35" 
#>  [37] "36"  "37"  "38"  "39"  "40"  "41"  "42"  "43"  "44"  "45"  "46"  "47" 
#>  [49] "48"  "49"  "50"  "51"  "52"  "53"  "54"  "55"  "56"  "57"  "58"  "59" 
#>  [61] "60"  "61"  "62"  "63"  "64"  "65"  "66"  "67"  "68"  "69"  "70"  "71" 
#>  [73] "72"  "73"  "74"  "75"  "76"  "77"  "78"  "79"  "80"  "81"  "82"  "83" 
#>  [85] "84"  "85"  "86"  "87"  "88"  "89"  "90"  "91"  "92"  "93"  "94"  "95" 
#>  [97] "96"  "97"  "98"  "99"  "100"
close(con)

con <- file(paste0(file_name, "_part-1"), "r+")
readLines(con)
#>  [1] "0"  "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12" "13" "14"
#> [16] "15" "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" "27" "28" "29"
#> [31] "30" "31" "32" "33"
close(con)

con <- file(paste0(file_name, "_part-2"), "r+")
readLines(con)
#>  [1] "34" "35" "36" "37" "38" "39" "40" "41" "42" "43" "44" "45" "46" "47" "48"
#> [16] "49" "50" "51" "52" "53" "54" "55" "56" "57" "58" "59" "60" "61" "62" "63"
#> [31] "64" "65" "66" "67"
close(con)

con <- file(paste0(file_name, "_part-3"), "r+")
readLines(con)
#>  [1] "68"  "69"  "70"  "71"  "72"  "73"  "74"  "75"  "76"  "77"  "78"  "79" 
#> [13] "80"  "81"  "82"  "83"  "84"  "85"  "86"  "87"  "88"  "89"  "90"  "91" 
#> [25] "92"  "93"  "94"  "95"  "96"  "97"  "98"  "99"  "100"
close(con)