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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s an issue of reproducible code. If you specify a directory that doesn’t exist on someone else’s computer, then they can’t use your code. This is particularly bad with absolute file paths, and particularly bad with Windows file paths (which are absolutely impossible to replicate on a Unix system).
My preferred solution is to specify that the user should be in the relevant directory on their own system before starting to run the code. If for your own convenience you want to put a
setwd(...)right at the top of your code, where other people can notice it and comment it out as appropriate, but the rest of your code assumes only relative paths from that starting directory, that’s OK with me.Yihui Xie (author of
knitr) feels particularly strongly about this:https://groups.google.com/forum/?fromgroups=#!topic/knitr/knM0VWoexT0
And from the aforementioned FAQ 7:
See also: https://github.com/yihui/knitr/issues/38