Source a bunch of files under given folders and/or subfolders

I wrote a function sourceDir() that can source all the functions under given folders and/or subfolders. This function is very much like “Set Path” in Matlab.Please visit this page to obtain the update to dated version.

To use it more efficiently, I suggest you put the function at some place, e.g. ~/workspace/R_utils/sourceDir.R, and write the following lines in .Rprofile file.

.my.env <- new.env()
 sys.source("~/workspace/R_utils/sourceDir.R", envir=.my.env)
 attach(.my.env)

Next time the function will be automatically loaded under a personal environment .my.env when R is launched and you can use it directly. The advantage of this is that rm(list=ls()) command will not remove the function sourceDir. But if you really want to remove sourceDir function, you can use rm(list=ls(all=TRUE)) command instead.

Published
Categorized as Default, R

By Feng Li

Dr. Feng Li is an Associate Professor of Statistics in the School of Statistics and Mathematics at Central University of Finance and Economics in Beijing, China. Feng obtained his Ph.D. degree in Statistics from Stockholm University, Sweden in 2013. His research interests include Bayesian computation, econometrics and forecasting, and distributed learning. His recent research output appeared in statistics and forecasting journals such as the International Journal of Forecasting and Statistical Analysis and Data Mining, AI journals such as Expert Systems with Applications, and medical journals such as BMJ Open.

Leave a comment

Your email address will not be published. Required fields are marked *