Nick
Nick

Reputation: 9051

How to get current working directory in R?

  1. How to get current working dir? I guess there should be a command like getcwd(), however, I can't find something like this in the documens.

  2. How to change to another dir?

  3. How to set default working dir when I start R?

    version _
    platform x86_64-w64-mingw32
    arch x86_64
    os mingw32
    system x86_64, mingw32
    status
    major 3
    minor 1.0
    year 2014
    month 04
    day 10
    svn rev 65387
    language R
    version.string R version 3.1.0 (2014-04-10) nickname Spring Dance

Thanks!

Upvotes: 21

Views: 35988

Answers (2)

Rui Barradas
Rui Barradas

Reputation: 76460

As a complement to @Dirk's answer, I would like to add something that might be useful and is frequently overlooked.
It is possible to save the current working directory and set the new one at the same time.

Function setwd returns the working directory so all you have to do is to save its value. This can be useful if you, at a later moment, need to return to the original working directory.

old_dir <- setwd("/path/to/new/dir")

#[run the code you want]

setwd(old_dir)

Upvotes: 6

Dirk is no longer here
Dirk is no longer here

Reputation: 368261

  1. It is getwd()

  2. It is setwd("path/to/new/dir")

  3. Either via a Windows property, or via .Rprofile etc, or (as I recall) by defining $HOME which Windows does not set by default. See help(Startup).

Upvotes: 39

Related Questions