Using setwd() to return to default in R

r

Solution

Here is an alternative solution, since the `Defaults` package has been archived:

# Use `formals<-`, but note the comment in the examples of ?formals:
#
## You can overwrite the formal arguments of a function (though this is
## advanced, dangerous coding).
formals(setwd) <- alist(dir = "C:/Users/me/Desktop")

Or you could mask `base::setwd()` with something like:

setwd <- function(dir) {
  if (missing(dir) || is.null(dir) || dir == "") {
    dir <- "C:/Users/me/Desktop"
  }
  base::setwd(dir)
}

UPDATE: The Defaults package has been archived, so this solution only works if you download the package from the CRAN archive and build from source yourself.

You could use the Defaults package to set it to what you want. Then you could just call `setwd()`.

library(Defaults)
setDefaults(setwd, dir="C:/Users/me/Desktop")
setwd()

See this answer if you want to put the above code in your .Rprofile.

Problem

Does anyone know of a simple way to return to the default working directory in R? I know I can just type in my home path... ``` setwd("C:/Users/me/Desktop") ``` ...but I guess I'm lazy. Is there a default command or something such as... ``` setwd(default)? ``` Thanks if you know the answer to this. Paul

Original source

Related problems