C++ solution
It is not too difficult to write some c++ code for this:
#include <fstream>
#include <R.h>
#include <Rdefines.h>
extern "C" {
// [[Rcpp::export]]
SEXP dump_n_lines(SEXP rin, SEXP rout, SEXP rn) {
// no checks on types and size
std::ifstream strin(CHAR(STRING_ELT(rin, 0)));
std::ofstream strout(CHAR(STRING_ELT(rout, 0)));
int N = INTEGER(rn)[0];
int n = 0;
while (strin && n < N) {
char c = strin.get();
if (c == '\n') ++n;
strout.put(c);
}
strin.close();
strout.close();
return R_NilValue;
}
}
When saved as yourfile.cpp
, you can do
Rcpp::sourceCpp('yourfile.cpp')
From RStudio you don't have to load anything. In the console you will have to load Rcpp. You will probably have to install Rtools in Windows.
More efficient R-code
By reading larger blocks instead of single lines your code will also speed up:
dump_n_lines2 <- function(infile, outfile, num_lines, block_size = 1E6) {
incon <- file( infile , "r")
outcon <- file( outfile , "w")
remain <- num_lines
while (remain > 0) {
size <- min(remain, block_size)
lines <- readLines(incon , n = size)
writeLines(lines , outcon)
# check for eof:
if (length(lines) < size) break
remain <- remain - size
}
close( incon )
close( outcon )
}
Benchmark
lines <- "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean commodo
imperdiet nunc, vel ultricies felis tincidunt sit amet. Aliquam id nulla eu mi
luctus vestibulum ac at leo. Integer ultrices, mi sit amet laoreet dignissim,
orci ligula laoreet diam, id elementum lorem enim in metus. Quisque orci neque,
vulputate ultrices ornare ac, interdum nec nunc. Suspendisse iaculis varius
dapibus. Donec eget placerat est, ac iaculis ipsum. Pellentesque rhoncus
maximus ipsum in hendrerit. Donec finibus posuere libero, vitae semper neque
faucibus at. Proin sagittis lacus ut augue sagittis pulvinar. Nulla fermentum
interdum orci, sed imperdiet nibh. Aliquam tincidunt turpis sit amet elementum
porttitor. Aliquam lectus dui, dapibus ut consectetur id, mollis quis magna.
Donec dapibus ac magna id bibendum."
lines <- rep(lines, 1E6)
writeLines(lines, con = "big.txt")
infile <- "big.txt"
outfile <- "small.txt"
num_lines <- 1E6L
library(microbenchmark)
microbenchmark(
solution0(infile, outfile, num_lines),
dump_n_lines2(infile, outfile, num_lines),
dump_n_lines(infile, outfile, num_lines)
)
Results in (solution0 is the OP's original solution):
Unit: seconds
expr min lq mean median uq max neval cld
solution0(infile, outfile, num_lines) 11.523184 12.394079 12.635808 12.600581 12.904857 13.792251 100 c
dump_n_lines2(infile, outfile, num_lines) 6.745558 7.666935 7.926873 7.849393 8.297805 9.178277 100 b
dump_n_lines(infile, outfile, num_lines) 1.852281 2.411066 2.776543 2.844098 2.965970 4.081520 100 a
The c++ solution can probably be sped up by reading in large blocks of data at a time. However, this will make the code much more complex. Unless this is something I would have to do on a very regular basis, I would probably stick with the pure R solution.
Remark: when your data is tabular, you can use my LaF
package to read arbitrary lines and columns from your data set without having to read all of the data into memory.