R list.files(my_working_directory) shows no files but I know they are there. How to fix?

13,449

Since I think you're wanting to keep some relativity in your paths, and because you're using RStudio, I'll share one strategy that I often use. If I've correctly divined what you're looking for, this should sidestep the issue entirely.

Start by creating a new project from within RStudio. Once you've opened that project, you'll find that your working directory getwd() will be conveniently set where you have your project file at.

If you're doing something larger, you can get yourself off on the right foot by using the "ProjectTemplate" package. On my system:

# install.packages(c("ProjectTemplate"))
library(ProjectTemplate)
create.project("~/Desktop/MyProject")

This sets up a project skeleton, complete with unit test folder, doc folder, and everything else I might need for a "proper" R project.

Next I create a project from RStudio as I mentioned above. Create it from an existing directory. The project will open, and all of your paths will be relative to the root of your 'MyProject' folder.

I also like to initialize a git repository while I'm at it. From an OS X terminal window:

$ cd ~/Desktop/MyProject; git init

...and then add / commit the bare skeleton

$ git add * ; git commit -m "initial project skeleton"

From your RStudio session you can load things relative to your project root. Let's say you had iris.csv in your data subfolder:

read.csv("./data/iris.csv", header=TRUE)
Share:
13,449
A.Mstt
Author by

A.Mstt

Professional procrastinator, biologist in my free time

Updated on June 25, 2022

Comments

  • A.Mstt
    A.Mstt almost 2 years

    I'm trying to work with R using a directory in my external hard drive as working directory. I can set the working directory to where I want, but when I use list.files R can't see anything. I also can't use read.delim to load a file.

    For example:

    > rm(list = ls())
    > WD<-"../../../Volumes/My_HD/my_working_directory" 
    > setwd(WD) 
    > getwd()
    [1] "/Volumes/My_HD/my_working_directory"
    

    So far so good, but then:

    > list.files(WD)
    character(0)
    

    Whilst I would expect a list of the files that I know are there and that I can see and open with Finder and with the Terminal. Actually the R script itself is inside that directory.

    If then I try to open a file I get something like this:

    > myfile <- read.delim(paste(WD,"/file_name", sep = ""), header = T)
    Error in file(file, "rt") : cannot open the connection
    In addition: Warning message:
    In file(file, "rt") :
    cannot open file '../../../Volumes/My_HD/my_working_directory/file_name': No such file or directory
    

    I thought I was doing something wrong specifying the path to the directory, but I tried point and click from RStudio (Session>Set Working Directory and either >To Source File Location or >Choose Directory...) and got the same unhappy result.

    I'm using R version 2.15.1 on a MacBookPro (OS X 10.7.05). I'm running R from RStudio version 0.97.551, but the problem is not related to RStudio as it happens also when I try directly on R.

    Any help will be very much appreciated. Thanks.

  • A.Mstt
    A.Mstt over 10 years
    I think that @flodel comment on absolute and relative paths better suits my problem, as I was given an script that uses the object WD at several points and couldn't make it to work.