[R] Cannot read or write to file in Linux Ubuntu

Sarah Goslee sarah.goslee at gmail.com
Thu May 3 20:07:12 CEST 2012


On Thu, May 3, 2012 at 1:53 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:
> "I like the idea of staying with absolute paths."
>
> Before you write too much R code that builds in absolute paths, please consider how difficult it will be to adjust all of those paths if you need to run on a different computer or you need to reorganize your overall directory structure. If you keep related R files in the same project directory, you can collapse all of those paths down to short relative paths, and do one setwd at the beginning, or learn to manually set your base working directory as a matter of habit before each working session. (This habit is useful in more areas than just R programming.)

I agree with this, which is why I suggested you use absolute paths
*until you get more comfortable with the file system.* It's a good way
to diagnose your problem and figure out how to deal with paths on
Linux, but not a good long-term strategy unless you expect that you
will never ever move anything or change to a new computer.

An intermediate solution that I use a lot is to put something like
this at the beginning of R script file:
basepath = "/home/sarahg/whatever"
and then load files using something like
read.table(paste(basepath, "plantdata.csv", sep="/"))

This eases portability between computers: I exchange a lot of analyses
with postdocs, students and techs, and somehow they've not all become
convinced that my way of organizing directories is the best one. Using
an object with the absolute path for the data means that we can pass
things back and forth with only changing one value within the script
rather than all input/output commands.

Sarah

-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list