How to access to specify file in subfolder without change working directory In R?

set working directory in r
r list files in subdirectories
r cannot change working directory
list.files r
r get directory of file
error in file(file, "rt") : cannot open the connection
r working directory best practices
file.path r

In R, I want to access to some file in subfolder. But I don't want to change working directory then move back. It lost time and long.

For exmaple, I working on /home/phuong folder. Here is the tree structure of phuong.

phuong-> data1, data2, data3.
data1-> abc.csv, def.csv, script1.R
data2-> bond.csv, option.csv, pricing.R

So i want to load data in abc.csv, def.csv and run code in pricing.R.

So if use code setwd, it make me lost many time and look code so stupid, like this:

setwd("/home/phuong/data1" );

setwd("/home/phuong/data2" );

I lost a lot of times to move from folder to another folder but all of them in the same folder home/phuong/. So I need some way to access to any file in subfolder without setwd command. Please help me , thks.

Assuming your working directory is /home/hermie and you want to load a .csv file from a directory below your current WD (let's say /home/hermie/data), you can simply do this:

myData <- read.csv('./data/myCsvFile.csv')

Of course you could also navigate "upwards" in the directory tree. Let's say you want to load a file in Bob's home directory (/home/bob). You can do it as follows:

data_from_bob <- read.csv('../bob/otherDataFile.csv') # Of course, this will work
                                                      # only if you can read
                                                      # files from that directory

Hope this helps.


Somehow I think you want someone to write the solution for you... and I propose this:

> setwd('/home/phuong')
> data_abc <- read.csv('./data1/abc.csv')
> data_def <- read.csv('./data1/def.csv')
> source('./data2/pricing.R')

Is it really so dificult to write this? You would have to write much more if you changed your WD on every step of the way.

And, about my sugestion on symlinks, on your bash terminal you could do something like this:

$ cd /home/phuong
$ ln -s ./data1/abc.csv data1_abc.csv
$ ln -s ./data1/def.csv data1_def.csv
$ ln -s ./data2/pricing.R pricing.R

And then, from R:

> setwd('/home/phuong')
> data_abc <- read.csv('data_abc.csv')
> data_def <- read.csv('data_def.csv')
> source('pricing.R')

Working with files and folders in R, Learn how to work with files and folders in R. Import, delete, and create thing you need to know is how to get and setup your working directory. ? #Set the working directory to a subfolder within the current working directory. Working with the current directory. Unless you specify it otherwise, all files will be read and saved into the working directory. Therefore, the first thing you need to know is how to get and setup your working directory. #Get the working directory: getwd() #Setup the working directory: setwd("C:/Documents and Settings/Folder name")

You could use what Hadley calls a closure in Advanced R if I understand what you're after:

## Make a function that takes a path and another function
## and returns that same function with the path pre-progammed in
pathit <- function(FUN, path){
    function(file, ...){
        FUN(file=file.path(path, file), ...)

## generate new functions that have the path pre-programmed in
read.csv2b <- pathit(read.csv, "home/phuong/data1")
source2 <- pathit(source, "home/phuong/data2")


If you have a lot of stuff to read in this may be worthwhile otherwise why not supply the whole path to the actual functions? If this isn't what you're after it was still a fun learning experience for me :-)

Finding files in project subdirectories, the project root (when running R scripts),; a subdirectory (when building This vignette starts with a very brief summary that helps you get If you are sure that your working directory is somewhere below your Specify a path/to/file relative to the root rel_path_from_root Error: No root directory found in� You also can use file.path () to specify file paths that include the filename at the end. Simply add the filename to the path argument. For example, here’s the file path to the file in the roxygen2 package installed in a local folder: > file.path ("F:", "git", "roxygen2", "roxygen2", "" ) [1] "F:/git/roxygen2/roxygen2/".

For me, the most intuitive way to learn to navigate folders is by using list.files("../"). You will see how upstream or downstream you need to navigate from your current location :)

getwd, setwd, You can find out which directory by running the getwd (get working directory) function; this function has no arguments. To change your working directory, use setwd and specify the path to the desired folder. getwd() I want to go to (getting to my Videos folder), navigate further to the subfolder packageFiles. getwd() returns an absolute filepath representing the current working directory. This result will be a character string. It can return Null if the working directory is not available. For users using Windows, the path will use “/” as a separator between levels of the directory. The path will not have a trailing slash unless it is the root directory.

I don't know if this is what you were looking for, but I have a library of files in a specific github folder that I source when I initialize a project in a new branch, and I found one solution online like this:

library_files <- list.files()
sapply(library_files, source)

which is great except now I'm starting every file in a directory where I wouldn't want to save plots etc., so I realized all it needed was this:

library_files <- list.files("~/repos/library/all_the_things",
sapply(library_files, source)

and now it runs them without changing directories.

Running RStudio and Setting Up Your Working Directory, This sub-folder, also known as working directory, will be used by R to read and save files. Launch R by double-clicking on the icon. Specify your working directory� Before working with files, it’s usually a good idea to first know what directory you’re working in. The working directory is the folder that any files you create or refer to without explicitly spelling out the full path fall within. In R, you can figure this out with the getwd function. To change this directory, you can use the aptly named

How to Work with Files and Folders in R, You need to keep track and deliberately set your working directory in each R session. If you read or To change the working directory, use the setwd() function. Be sure to enter the working directory as a character string (enclose it in quotes). Create a sub-directory, say R, in your “Documents” folder. This sub-folder, also known as working directory, will be used by R to read and save files. Launch R by double-clicking on the icon. Specify your working directory to R: On Windows: File –> Change directory; On MAC OSX: Tools –> Change the working directory

RPubs, “Directory” is a fancy way to say folder, which may have subfolders, Specifying Files: Since you're printing a vector of every file in the directory, Note: This is important to remember: Changing working directories in text files using an iteractive GUI to modify arguments and get the data read in properly. The pattern matching works with the case of file names as returned by the OS. On a POSIX filesystem recursive listings will follow symbolic links to directories. Author(s) Ross Ihaka, Brian Ripley. See Also., file.access and files for many more file handling functions and file.choose for interactive selection.

working directory, An introductory book to R written by, and for, R pirates. on your computer that tells R where to start looking for your files related to a specific project. The working directory is just a file path on your computer that sets the default location of any files If you want to change your working directory, use the setwd() function. It is this separation of Share Permissions and Security Permissions that is the cause of so many "Access Denied" messages. SORRY BUT MY PROBLEM IS THAT THE SHARE FILE IS ON WINDOWS 7 AND FROM 1 PC WITH WINDOWS XP I CANT HAVE ACCESS TO SOME SUBFOLDERS BUT FROM ANOTHER WINDOWS XP PC I CAN HAVE ACCESS TO ALL FOLDERS AND SUBFOLDERS

  • Why do you need to change the directory every time? It's not necessary.
  • I know this way, but it make me type full path to folder, and when we load many files in some folder with different time, so it make my code is so long and complex.
  • create links in your file system. You don't have a problem with R, you have a problem with your folder structure... and that has two solutions: 1. reorganize your files in a way you feel comfortable, or 2. use links to your files (more info here)
  • @Barrabka, i don't understand your idea, It the first question i ask about this problem.
  • what is so complex about writing the route to each data file you need? you don't need to change the WD every time to do that, and you will only write that once! Once again, if you want to have access to all the required files from a single location, create symlinks to them in your current WD, and simply read the symlinks (it's just like reading the file directly) (I am asuming you are working on a linux environment).
  • I prefer this solution more than the accepted answer. It works like a charm!