Skip to content

Instantly share code, notes, and snippets.

@johnatasjmo
Created January 20, 2018 16:44
Show Gist options
  • Save johnatasjmo/f6073976b5e1aac3d755ee1852a3aca5 to your computer and use it in GitHub Desktop.
Save johnatasjmo/f6073976b5e1aac3d755ee1852a3aca5 to your computer and use it in GitHub Desktop.
R - Source all files within folder
setwd("~/rProgramming")
files.sources = list.files()
sapply(files.sources, source)
@laurettemhlanga
Copy link

Thank you so much :)

@nprompt2
Copy link

excellent idea. just to add on to it. One may not aways be able to change the working directory and in that case, drop the setwd() function. Then, add the path to list.files() as list.files(path="~/rProgramming")

@gracerhpotter
Copy link

excellent idea. just to add on to it. One may not aways be able to change the working directory and in that case, drop the setwd() function. Then, add the path to list.files() as list.files(path="~/rProgramming")

Note - for this to work you also need to have full.names = TRUE within the list.files function so that when you call source in the sapply function it has the path information for each file, which will be required since you didn't change your working directory. I.e. list.files(path="~/rProgramming", full.names = TRUE).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment