Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing working directory will impact the location of output #38

Closed
etiennebr opened this issue Dec 6, 2011 · 21 comments
Closed

Changing working directory will impact the location of output #38

etiennebr opened this issue Dec 6, 2011 · 21 comments
Labels
feature Feature requests

Comments

@etiennebr
Copy link

This is a problem with sweave as well, but when you change a directory
setwd("aplace")
this will output the figures and the .tex in ~ directory. It can even lead to having stuff really scattered.
setwd("here") plot(something) setwd("there") plot(something) ...
It can even bring LyX to crash.

@yihui
Copy link
Owner

yihui commented Dec 6, 2011

This is expected. It is a bad idea to use setwd() in your code, and I do not see why it is necessary.

@etiennebr
Copy link
Author

I see. I'm running tools from the command line which require to be in a
certain directory.

I could imagine another example, where sourcing files from different
projects where files are referred as "./data" for instance.

maybe this kind of operation could be enclosed in
`owd <- setwd("there")

make sure you don't produce a graph

setwd(owd)
#back to normal`

So, I understand this won't be possible in knitr. Thanks anyway for the
wonderful tool.

2011/12/5 Yihui Xie <
reply@reply.github.com

This is expected. It is a bad idea to use setwd() in your code, and I do
not see why it is necessary.


Reply to this email directly or view it on GitHub:
#38 (comment)

@yihui
Copy link
Owner

yihui commented Dec 6, 2011

You are right. It is a good practice to always restore the working directory after you are done with the computation.

I just did not realize why there could be a demand on setwd(). It is not impossible for knitr to allow you to setwd() (should be easy to remove this restriction). I will consider about it. Thanks for the feedback!

@hadley
Copy link
Contributor

hadley commented Dec 6, 2011

My two cents: you should not be changing working directories in the middle of a project. Changing working directory should almost never be done automatically - it's a UI operation because when you set a working directory, you're saying, I'm now working on this project.

@etiennebr
Copy link
Author

While I understand your point, and with all the respect I have for your
opinion, I must say I do not see from your arguments if this should not be
done because it is not supported or because it is truly not a good practice.

Indeed, I could see a workaround like wraping these setwd() in a function :
system.cd <- function(wd, command, ...){ owd <- setwd(wd) system(command, ...) setwd(owd) }
However, on a practial point of view, this is changing the working
directory.

The root of the problem is a program I'm using which outputs files in the
directory from which it is launched. The other is not being able to specify
a working directory with system. What would be a good way of working with
this issue ? And would be portable on win and linux. My data is too big to
move it with my code, and this would be another malpractice :-) Would a
chess players call this a fork ?

Etienne

2011/12/6 hadley wickham <
reply@reply.github.com

My two cents: you should not be changing working directories in the middle
of a project. Changing working directory should almost never be done
automatically - it's a UI operation because when you set a working
directory, you're saying, I'm now working on this project.


Reply to this email directly or view it on GitHub:
#38 (comment)

@hadley
Copy link
Contributor

hadley commented Dec 6, 2011

Ah oh, that makes sense. Using system is the one time you do actually need to change the working directory! (I wish it was an option)

I use the following function to run chunks of R code in an arbitrary working directory.

in_dir <- function(dir, code) {
  cur <- getwd()
  setwd(dir)
  on.exit(setwd(cur))

  force(code)
}

@yihui
Copy link
Owner

yihui commented Dec 6, 2011

@etiennebr You can use setwd() freely in knitr now; it won't affect the paths of cache and figures even if you do not restore the working directory. That said, it is still recommended to restore it (e.g. use Hadley's function above); it is a safer practice.

@etiennebr
Copy link
Author

I surrender to the ingenuity of in_dir! Thanks both of you.

@geneorama
Copy link

This working directory issue is challenging...

I have a project that is structured like this

MyProject
MyProject\Data
MyProject\KnitrReports
MyProject\KnitrReports\Report_1
MyProject\KnitrReports\Report_2
MyProject\Other_Things

If I run chunks in R Studio, then the working directory is "MyProject". I want / expect the working directory to be "MyProject".
However, when I knit the document, the working directory changes to the report directory.

I want to keep the reports separate so that I can manually manage the large cache folders, and just to keep things organized. This is an ongoing project with interdependent large sets of data, and I don't want to keep everything at the main project level.

I don't want to create an absolute path to the data, because I share the project on other machines. Plus, I think that's bad form anyway.

Right now I run setwd('../..') at the start of any chunk that loads data (which is nearly always one chunk at the top of any single script / report), and this seems to be working. However, I was confused about this and not sure how to handle it.

Any advice?

@etiennebr
Copy link
Author

This one is kind of tricky, but I would say it is more related to RStudio
not running the script in the same working directory than in the session. I
can however understand why they took that decision.

I found this workaround, I experienced the same problem using
ProjectTemplate organisation.
if (basename(getwd()) == "src") setwd("..")

2012/7/24 Gene Leynes <
reply@reply.github.com

This working directory issue is challenging...

I have a project that is structured like this

My project
My project\Data
My project\KnitrReports
My project\KnitrReports\Report_1
My project\KnitrReports\Report_2
My project\Other_Things

If I run chunks in R Studio, then the working directory is "My Project".
I want / expect the working directory to be "My Project".
However, when I knit the document, the working directory changes to the
report directory.

I want to keep the reports separate so that I can manually manage the
large cache folders, and just to keep things organized. This is an ongoing
project with interdependent large sets of data, and I don't want to keep
everything at the main project level.

I don't want to create an absolute path to the data, because I share the
project on other machines. Plus, I think that's bad form anyway.

Right now I run setwd('../..') at the start of any chunk that loads data
(which is nearly always one chunk at the top of any single script / report).

Any advice?


Reply to this email directly or view it on GitHub:
#38 (comment)

@geneorama
Copy link

Now this is just genius
'if (basename(getwd()) == "src") setwd("..")`

@yihui
Copy link
Owner

yihui commented Jul 25, 2012

You can use the package option root.dir to set the working directory, e.g. opts_knit$set(root.dir = '../Data/'); see http://yihui.name/knitr/options#package_options

Calling setwd() in the code chunks is a bad idea in general.

@geneorama
Copy link

Thanks! I tried that unsuccessfully, but I was doing it wrong.

@abnova
Copy link

abnova commented Sep 6, 2014

I tried to set working directory to my project's home directory, using 'root.dir' option, but it didn't work properly. I used an absolute path to project's home directory, not a relative, as above. Is this a problem?

This is my report's R Markdown document: https://github.com/abnova/diss-floss/blob/master/analysis/eda-report.Rmd. And this is where I set the option: https://github.com/abnova/diss-floss/blob/master/config/knitr/knitr-setup-global.Rmd. Please take a look and advise.

UPDATE: I just tried using a relative path (root.dir='..') and it didn't work either. However, it got me thinking about what is the correct base directory to determine a relative one in a situation where a child document is located in a directory different than one of a parent document...

@paulboardman
Copy link

@abnova - rmarkdown set's the working directory to the location of the Rmd file (separately to knitr).

See the following line in render.R (part of the rmarkdown package): https://github.com/rstudio/rmarkdown/blob/28feca137762651c904e05ce119244fc41112fb4/R/render.R#L96

@abnova
Copy link

abnova commented Feb 25, 2015

@paulboardman Thank you for the comment - I will take a look at that, when I'll get a chance (it's been a while since I was working on that code, but it's good to know nevertheless). Best regards!

@geneorama
Copy link

I've been using a function that I wrote to set the project directory
https://github.com/geneorama/geneorama/blob/master/R/set_project_dir.R

There's an example in this Rmd file:
https://github.com/geneorama/DataScienceForBusiness/blob/master/Lec3/Lecture_3.Rmd

I don't love my solution, but it works.

On Wed, Feb 25, 2015 at 2:50 PM, Aleksandr Blekh notifications@github.com
wrote:

@paulboardman https://github.com/paulboardman Thank you for the comment

  • I will take a look at that, when I'll get a chance (it's been a while
    since I was working on that code, but it's good to know nevertheless). Best
    regards!


Reply to this email directly or view it on GitHub
#38 (comment).

@abnova
Copy link

abnova commented Feb 26, 2015

@geneorama Thank you for the advice, Gene! Will take a look at that.

@geneorama
Copy link

Thanks, I would be really interested to hear if it works out for you, or if
you have any suggestions for a better coding practice.

On Thu, Feb 26, 2015 at 1:27 PM, Aleksandr Blekh notifications@github.com
wrote:

@geneorama https://github.com/geneorama Thank you for the advice, Gene!
Will take a look at that.


Reply to this email directly or view it on GitHub
#38 (comment).

@abnova
Copy link

abnova commented Feb 26, 2015

@geneorama I would love to, but it'll be a while until I'll have a chance to get to my software again - currently I'm focusing my efforts on preparing for my Ph.D. dissertation defense (slides, talk, etc.).

@github-actions
Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature Feature requests
Projects
None yet
Development

No branches or pull requests

6 participants