R_User
R_User

Reputation: 11082

(How) is it possible in R to include external files with source code

Is is somehow possible in R to include a textfile containing R source code and execute at the position where it it is included?

In PHP I would use the command include ( http://php.net/manual/en/function.include.php )

I have a file, where I first define functions (~ 200 lines), then I create and set lots of variables by processing lots of files and using the defined functions (~1500 lines) and finally I use the values of the variables for calculations and for plotting (~700 lines).

# functions
readfile <- function (...) {
     ...
}


# reading files, general plots,...
dataFolder1="..."
pdf("param01_Set01.pdf")
    param01_Set01_SV = readfile(dataFolder1, ...)
    param01_Set01_KP = readfile(dataFolder1, ...)
    param01_Set01_NK = readfile(dataFolder1, ...)
dev.off()

dataFolder2="..."
pdf("param01_Set01.pdf")
    param01_Set02_SV = readfile(dataFolder2, ...)
    param01_Set02_KP = readfile(dataFolder2, ...)
    param01_Set02_NK = readfile(dataFolder2, ...)
dev.off()

...


# dooing specific calculations + plotting
result1 = (param01_Set01_SV$xyz + 123) * param02_Set08$xyz
plot(...)

I like to "outsource" the middle Part (Varaible definitions, file reading, general plotting) into a separate file, simply because it is very large and not of interest once I entered all filenames,... . Is here a command like include in R?

Upvotes: 5

Views: 4442

Answers (1)

Seb
Seb

Reputation: 5507

the command you are looking for is source() - check out ?source for further information. An alternative is sys.source()

Upvotes: 8

Related Questions