[R] Setting directory in Windows

Duncan Murdoch murdoch.duncan at gmail.com
Mon Feb 8 00:20:46 CET 2016


I added an informative subject line; that's a good idea in this list so 
people can choose whether to read your question or not.

On 07/02/2016 3:18 PM, Walter Niessen wrote:
> Helpers,
>
> I am a pretty savvy computer user (over 40 years) but am having trouble with the most basic process in using the R program.  I am taking a course to learn the statistics utility of “R” but can’t seem to get past the opening where it shows the student how to input data into R.
>
> It suggests identifying as the a target folder in the Properties of the R x64 3.2.3 icon (I use a 64-bit Windows 7 OS) the location where data files are found. I entered:
>
> As the Target:  "C:\Program Files\R\R-3.2.3\bin\x64\Rgui.exe"  ... which contains Rgui.exe and the supporting dlls etc.
>
> As the Start in: "C:\Program Files\R\R-3.2.3\Appendix" ... where Appendix is a folder which contains data files such as the text file DATA1.txt which I created from an Excel file.
>
> When I try and get R to input the data, I get the following sequence of messages:
>
>
>> X=scan("DATA1.txt")
> Error in file(file, "r") : cannot open the connection
> In addition: Warning message:
> In file(file, "r") :
>    cannot open file 'DATA1.txt': No such file or directory
>
> When I try again but use the complete file description, I get:
>
> X=scan("C:\Program Files\R\R-3.2.3\Appendix\DATA1.txt")
> Error: '\P' is an unrecognized escape in character string starting ""C:\P"
>
> I hate to ask a question not related to R or its features (that may come later), but, clearly, the first baby step is to be able to load data into the application . . . and, I am really frustrated with my inability to do so.

There are a few things you should know.

1.  R uses the \ character as an escape, so \t is a tab, etc.  If you 
really want a \ in a string, you need to escape it, i.e. enter \\.  This 
is a pain, so you can instead use / in place of \\ when specifying file 
paths.

2.  I think your course is giving bad advice.  Don't mess with the 
shortcut.  Use RStudio as your front end.  It allows you to set up 
"projects"; it remembers the current directory for each project, so you 
just "Open project" X, and R will be started in the directory of X.

3.  If you don't want to use RStudio, then run choose.dir() in R at the 
start of your session.  It lets you set the current directory to the one 
with your files in it, using a old-fashioned Windows dialog box.  If 
things stop working the way you expect, run getwd() to get R to print 
what's the current directory.

4.  You can also use file.choose() to use Windows dialogs to pick a 
file; then it doesn't really matter what the current directory is.

I hope some of this helps.

Duncan Murdoch



More information about the R-help mailing list