Questions tagged [setwd]
41 questions
27
votes
6 answers
risks of using setwd() in a script?
I've heard it said that it is bad practice to use setwd() in a script.
What are the risks/dangers associated with it?
What are better alternatives?

Ricardo Saporta
- 54,400
- 17
- 144
- 178
26
votes
5 answers
How to set the current file location as the default working directory in R programming?
I want to make the current file location as the working directory.
Using Rstudio (Works!):
# Author : Bhishan Poudel
# Program : writehere.r
# Source : Rscript writehere.r
# set working directory here
this.dir <- dirname(parent.frame(2)$ofile)…

BhishanPoudel
- 15,974
- 21
- 108
- 169
6
votes
2 answers
Is it possible to define a cross-platform working directory for R?
I am teaching R tutorials in person w/ a large number of undergraduate R novices. I am also trying to format my notes on RPubs so that they can easily be used by other people. Nothing derails things faster than people mis-specifying working…

N Brouwer
- 4,778
- 7
- 30
- 35
4
votes
0 answers
"Error in setwd(dir): cannot change working directory"
Trying to run a function where I set the directory to wherever is specified in the arguments.
However every time I do, I get the error message:
"Error in setwd(dir): cannot change working directory"
This happens even if I've already changed the wd…

Rose Savage
- 61
- 1
- 4
4
votes
1 answer
What is the philosophy behind the workspaces in R?
When I start R session from some directory, R automatically loads the corresponding workspace (if it exists). After I finish to work in this workspace I can decide if I want to modify (save) the current workspace. This logic is simple and…

Roman
- 124,451
- 167
- 349
- 456
4
votes
3 answers
Setting working directory through a function
I am new to R. I am facing trouble setting my working directory through a function.
This is what I have tried:
myfunction<-function(directory)
{
setwd(paste(getwd(),("/directory"))
}
When I run myfunction("name")
It gives error:cannot…

user3540633
- 41
- 1
- 1
- 2
3
votes
1 answer
Opening Rdata file on R
I have some trouble to open Rdata files on Rstudio.
I tried different directory.
I tried the load() function.
I set up the file pathway with setwd().
I made sure that the file pathway did not contain spaces or accent or particular character in it.
I…

M.Raynaud
- 33
- 1
- 1
- 5
3
votes
1 answer
How to save files from Rstudio AMI EC2 on my local computer
I have installed Rstudio server Amazon EC2. It works fine, but I can't understand how to save files on my local computer...
In example,
x=5
write.csv(x,file="x.csv")
getwd()
[1] "/home/rstudio"
I can see the file in my working directory, but I…

user1665355
- 3,324
- 8
- 44
- 84
2
votes
2 answers
How to open next folder in working directory for only one file?
I have a working directory
setwd("C:/User/WorkDirectory")
I have files in the working directory
"File 1.csv", "File 2.csv", "File 3.csv", "Folder 1"
I have no problem accessing the 3 files
df1<-read.csv("File 1.csv",check.names=FALSE)
However i…

user35131
- 1,105
- 6
- 18
2
votes
2 answers
Read a file in R without changing the working directory
How can others who run my R program read a file(eg: csv) used in my R code without having to change the working directory in setwd()?

KK47
- 25
- 1
- 5
2
votes
1 answer
Error setwd() "cannot change working directory"
I get "cannot change working directory" error when I try to set up my working directory:
setwd("C:\Users\alimo\Desktop\DataVisualizationwithggplot2.R")
*Error: '\U' used without hex digits in character string starting ""C:\U"*
then I did…

sociolog
- 83
- 1
- 8
2
votes
1 answer
Automatically Set Working Directory to Currently Opened Folder in R
Is it possible in R to set the working directory to a currently opened folder automatically?
Example: Let's assume that I have currently opened the folder example_dir on my computer.
Now, I want to run some R code to set this folder as my working…

Joachim Schork
- 2,025
- 3
- 25
- 48
2
votes
4 answers
Setting a working directory: "Error in setwd: cannot change working directory" under "C:/Documents and Settings/..."
I was wondering why it won't let me change my working directory.
I keep seeing this error:
Error in setwd(dataDir) : cannot change working directory
This is my code:
dataDir <- "C:/Documents and Settings/My Documents/R/"
setwd(dataDir)

Vanessa M
- 21
- 1
- 1
- 2
2
votes
1 answer
setwd error: directories within directories
Sorry this is long, but I'm a novice and want to be specific.
I have varied numbers of dataframes within a set of directories, within a set of directories. (That's 60 inner directories, hence I'm attempting to automate this.) My goal is to list and…

istudybrains
- 21
- 2
2
votes
1 answer
R: setwd delay when using file.choose
So what im doing is making my browse button so that when i click on them bring the user straight to the directory that i want them to save their file in or look for their file.
For example
setwd("C:\\Users\\Eric\\Desktop\\Program\\graphs") #set…

Napmi
- 521
- 2
- 13
- 32