This doesn't seem like a super safe idea, given how easily line numbers can change during editing. It seems like it would be safer to split your larger file into smaller parts that are safer to include and run. But you could do something like this
sourcePartial <- function(fn, skip=0, n=-1) { lines <- scan(fn, what=character(), sep="\n", skip=skip, n=n, quiet=TRUE) tc <- textConnection(lines) source(tc) close(tc) }
here we use scan() to read lines from a file. See the documentation for skip= and n= in ?scan to see how to skip a certain number of lines and stop reading after a certain number. So
sourcePartial("test.R", 4, 11)
will run lines 5-15 from "test.R"
source share