getwd() function in R with Example

R is regularly pointed at a directory on your computer. You can find out which directory by running the getwd() function, and the function does not take any argument.

getwd() function in R

To get the current directory in R, use the getwd() function. The getwd() function returns an absolute filepath representing the current working directory of the R process.

Syntax

getwd()

Parameters

The function does not take any parameter.

Return Value

The getwd() function returns a character string or NULL if the working directory is not available. On Windows OS, the path returned will use “/” as the path separator and be encoded in UTF-8. The path will not have a trailing / unless it is the root directory.

Example

If you want to get the current working directory in R, use the getwd() function.

getwd()

Output

[1] "/Users/krunal/Desktop/code/R"

As you can see that my current folder is the output, which means inside the R directory.

Default Working Directory in RStudio

RStudio employs the notion of a global default working directory. Normally this is the user home directory. When RStudio starts up, it does the following:

  1. Executes the .Rprofile (if any) from the default working directory.
  2. Loads the .RData file (if any) from the default working directory into the workspace.
  3. Performs the other actions described in R Startup.

When RStudio exits, and there are changes to the workspace, a prompt asks whether these changes should be saved to the.RData file in the current working directory.

This default behavior can be customized in the following ways using the RStudio Options dialog:

  1. Change the default working directory.
  2. Enable/disable the loading of .RData from the default working directory at startup
  3. Specify whether .RData is always saved, never saved, or prompted for saving at the exit.

That is it for getwd() function in R.

 

Leave a Comment