Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

set.wd

Set working directory to where the current file is.


Description

Set working directory to the path of the currently opened file. You can use this function in both .R/.Rmd files and the R console. RStudio (version >= 1.2) is required for running this function.

Usage

set.wd(path = NULL, directly = TRUE, ask = FALSE)

Arguments

path

NULL (default) or a specific path. Default is to extract the path of the currently opened file (usually .R or .Rmd) using the rstudioapi::getSourceEditorContext function.

directly

TRUE (default) or FALSE. Default is to directly execute setwd("...") within the function (recommended). Otherwise, it will send code setwd("...") to the R console and then execute it (not recommended due to a delay of execution).

ask

TRUE or FALSE (default). If TRUE, you can select a folder with the prompt of a dialog.

Value

Invisibly return the path.

See Also

Examples

## Not run: 
# RStudio (version >= 1.2) is required for running this function.
set.wd()  # set working directory to the path of the currently opened file
set.wd("~/")  # set working directory to the home directory
set.wd("../")  # set working directory to the parent directory
set.wd(ask=TRUE)  # select a folder with the prompt of a dialog

## End(Not run)

bruceR

Broadly Useful Convenient and Efficient R Functions

v0.6.2
GPL-3
Authors
Han-Wu-Shuang Bao [aut, cre]
Initial release
2021-04-08

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.