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

一个人想着一个人 提交于 2019-12-01 18:03:51

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!