R Func Getwd Setwd
# R getwd() and setwd() Functions - Working Directory
[ R Language Examples](https://example.com/r/r-examples.html)
The R getwd() function is used to get the current working directory, and setwd() is used to set the working directory.
The working directory is the default path where R reads and writes files.
The syntax format for the two functions is as follows:
getwd() setwd(dir)
**Parameter Description:**
* **dir** - The new working directory path (string).
## Example
# Get current working directory
current_dir <-getwd()
print(paste("Current working directory:", current_dir))
# View directory contents
print("Directory file list:")
print(list.files())
# Set working directory (example)
# setwd("/Users/username/projects/tutorial")
# View R's installation directory
print(paste("R installation directory (R.home):", R.home()))
Executing the above code will display the current R working directory and file list.
> When reading or writing files, if only the file name is provided (without a path), R will look for the file in the current working directory. To ensure code portability, it is recommended to set the correct working directory first.
[ R Language Examples](https://example.com/r/r-examples.html)
YouTip