Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current working directory in R?

Tags:

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!

like image 534
Nick Avatar asked May 23 '14 02:05

Nick


People also ask

How do I find my working directory in R?

You can also check your current working directory by running the command getwd() in the console. There are a number of ways to change the current working directory: Use the setwd R function. Use the Tools | Change Working Dir...

How do I get the current path in R?

If we want to check the current directory of the R script, we can use getwd( ) function. For getwd( ), no need to pass any parameters. If we run this function we will get the current working directory or current path of the R script. To change the current working directory we need to use a function called setwd( ).

How do I get a present working directory?

Get the current working directory: os. getcwd() returns the absolute path of the current working directory where Python is running as a string str . getcwd stands for "get current working directory", and the Unix command pwd stands for "print working directory".

What function displays the current directory in R?

The getwd() function in R can be used to display the current working directory. This simple function, which takes no arguments, returns the current working directory.


2 Answers

  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).

like image 186
Dirk Eddelbuettel Avatar answered Sep 19 '22 05:09

Dirk Eddelbuettel


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)
like image 33
Rui Barradas Avatar answered Sep 19 '22 05:09

Rui Barradas