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

Working directory is forced for each chunk? #277

Closed
kenahoo opened this issue Jun 15, 2012 · 19 comments
Closed

Working directory is forced for each chunk? #277

kenahoo opened this issue Jun 15, 2012 · 19 comments
Milestone

Comments

@kenahoo
Copy link

kenahoo commented Jun 15, 2012

I asked a question over on the RStudio site, but apparently RStudio isn't actually involved, so I ended up over here.

It seems that during knit(), the working directory is forced back to the directory of the input document before every chunk is evaluated. So it's not possible to setwd() to my project directory at the top of the .Rmd document, then keep that directory for all the rest of the chunks.

Is this on purpose?

I see that in issue #38 it's now allowed to call setwd() - but it won't do me much good. =)

In my case, I've got my project directory, which has subdirectories docs/, data/, src/, and so on. To me, it makes sense to specify all paths relative to the project directory root, not the directory within docs/ that the .Rmd document happens to live in. That's why I call setwd() in the first place. And putting all the documents at the top level isn't an option, things would clobber each other since there are multiple documents.

@yihui
Copy link
Owner

yihui commented Jun 15, 2012

I understand the situation, but why not read.whatever('../data/foo.csv')?

@kenahoo
Copy link
Author

kenahoo commented Jun 15, 2012

That's of course possible, but not ideal. At least in the Unix way of thinking, it's bad form to compute paths relative to what's being executed, they should be computed relative to where the user invokes it from. Practically, in my case it would necessitate a whole lot of changes to paths, which are now specified relative to my project root.

@yihui yihui closed this as completed in 75e673a Jun 23, 2012
yihui added a commit that referenced this issue Jun 23, 2012
@yihui
Copy link
Owner

yihui commented Jun 23, 2012

Hi, I have added a package option root.dir to specify the root directory instead of forcibly using the directory of the input document. You can, for example, use

opts_knit$set(root.dir = 'c:/Users/kwilliams/git/ProjectX')

to set the root directory to evaluate your R code chunks. I hope this helps.

@paulrougieux
Copy link

This root.dir option is useful. I usually put Rmd files in a subdirectory /docs of my project and load dataframes from a subdirectory /enddata. It took me a while to figure out that each chunk sets the working directory to the place were the Rmd file is located. I prefer to write all paths relative to the root project directory by setting
opts_knit$set(root.dir = '../..')
This is exactly what I needed, Thanks.

@yihui
Copy link
Owner

yihui commented Jan 24, 2014

@paul4forest Glad to know that :)

@picasa
Copy link
Sponsor

picasa commented May 1, 2014

Hi, one small question: how to set knitr package options in .rmd document (rstudio preview version) without loading knitr ?

For knitr documentation, i suppose that I should use the following in my .rmd source, but I could not manage to get rstudio to call knitr with the working directory I intended to.

options(knitr.root.dir = normalizePath('../'))

Whereas loading knitr before is working.

require(knitr)
opts_knit$set(root.dir = normalizePath('../'))

Any ideas what I'm missing ? Sorry, I could not decide whether it was a knitr or rstudio question :) Anyway, thanks for both these tools !

@yihui
Copy link
Owner

yihui commented May 1, 2014

@picasa You should use opts_knit$set() whenever you can.

@picasa
Copy link
Sponsor

picasa commented May 1, 2014

OK, i'll stick to this solution. thank you !

@geneorama
Copy link

I wrote a function that navigates up until it hits the "project dir". I put it at the top of my Rmd files so that they execute the same whether I'm compiling the whole report or stepping through it.

I really wish there was a way to do this natively within knitr.

There are two problems with using opts_knit$set(root.dir = '..')

  1. It's confusing as heck to new programmers when I'm trying convince them that knitr, markdown, and R Studio makes R their lives better and simpler.
  2. It doesn't work in an R Studio project. For example, I typically keep my reports in ./out or ./reports, and I keep my data in the data folder of the project e.g. in ./data. The first time I compile a report knitr correctly navigates up one directory and finds ./data. The second time I compile the same report (I have yet to write an entire report perfectly the first time) knitr navigates up another directory, and my report and perhaps the whole project falls into existential crisis.

Therapy is expensive, so it would be nice to resolve this.

@picasa
Copy link
Sponsor

picasa commented Sep 16, 2015

@geneorama I'm still using the following code in a header chunk in my .rmd files, with the same directory organization as yours. Working as expected, i.e path are the same wheter I directly execute the code or run knitr for reporting, from rstudio. Anyway, the root path is only changed in the next chunk in .rmd file, so it may be a problem if you are trying to load files or such directly after opts_knit$set().

require(knitr)
opts_knit$set(root.dir = normalizePath('../'))

@geneorama
Copy link

@picasa oh yeah, forgot to mention, I put my function at the top of every code chunk that references the file system in any way. It's really annoying if I change the project name and forget to update the function because it immediately navigates to the root directory. Always happens when I'm doing something urgent!

@krlmlr
Copy link
Contributor

krlmlr commented Sep 16, 2015

My solution to this problem is a package that searches for project roots. There's a vignette, too -- the "Shortcuts" section there contains examples that could help for this particular use case.

@CarolinaGarzonL
Copy link

Hi. I had the same problem but I solved it in a different way. I had to change my directory and charged information from an specific folder inside the directory and then save data in a different folder in the same directory.

folder <- "/directory"
setwd(paste0(folder, "/data"))
charge your data here
setwd(paste0(folder, "/Uni"))

That will change the work directory for all chunks.

@wzrzt
Copy link

wzrzt commented Aug 23, 2017

It seems that r knitr::opts_knit$set(root.dir = '/Users/weirain/My/Rlanguage/Word2Vec') works except the setup chunk.
image

@yihui
Copy link
Owner

yihui commented Aug 23, 2017

@wzrzt That is correct.

@blaiseli
Copy link

blaiseli commented Sep 25, 2018

Dear @yihui , thanks for providing such a useful tool.

I'm using Rmarkdown to create an html page that will be used as course material for an initiation to R.

My intention was to have the chunks represent what the students can see in their R console while they follow the course and type along.

But the fact that the working directory is reset for each chunk makes it apparently impossible to include parts where the students create directories, setwd into them, work there, and eventually go back to their original starting directory.

It would be very useful to either have a global option to allow path changes in the chunks to affect the root.dir for the next ones, or to have a chunk-local option to have this chunk behave with a different root.dir, or some other mechanism to change the root.dir not just in one initial setup, chunk, but as many times as one wants along the Rmarkdown document.

Is it something possible?

@blaiseli
Copy link

blaiseli commented Sep 25, 2018

Apparently, knitr::opts_knit$set(root.dir = ...) works also in normal chunks, not only in the "setup" one. So I can fix my course support by "cheating" in include=FALSE chunks:

{r, include=FALSE}
original_dir <- getwd()

And later:

{r, include=FALSE}
knitr::opts_knit$set(root.dir = paste(original_dir, "stat_course", sep="/"))

@paulrougieux
Copy link

It seems that r knitr::opts_knit$set(root.dir = '/Users/weirain/My/Rlanguage/Word2Vec') works except the setup chunk.

When set in a chunk knitr::opts_knit$set(root.dir = 'path') works in the following chunks.

@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
None yet
Projects
None yet
Development

No branches or pull requests

9 participants