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 · 18 comments

Comments

9 participants
@kenahoo

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

This comment has been minimized.

Show comment
Hide comment
@yihui

yihui Jun 15, 2012

Owner

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

Owner

yihui commented Jun 15, 2012

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

@kenahoo

This comment has been minimized.

Show comment
Hide comment
@kenahoo

kenahoo 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.

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 in 75e673a Jun 23, 2012

yihui added a commit that referenced this issue Jun 23, 2012

@yihui

This comment has been minimized.

Show comment
Hide comment
@yihui

yihui Jun 23, 2012

Owner

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.

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

This comment has been minimized.

Show comment
Hide comment
@paulrougieux

paulrougieux Dec 23, 2013

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.

paulrougieux commented Dec 23, 2013

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

This comment has been minimized.

Show comment
Hide comment
@yihui

yihui Jan 24, 2014

Owner

@paul4forest Glad to know that :)

Owner

yihui commented Jan 24, 2014

@paul4forest Glad to know that :)

@picasa

This comment has been minimized.

Show comment
Hide comment
@picasa

picasa 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 !

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

This comment has been minimized.

Show comment
Hide comment
@yihui

yihui May 1, 2014

Owner

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

Owner

yihui commented May 1, 2014

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

@picasa

This comment has been minimized.

Show comment
Hide comment
@picasa

picasa May 1, 2014

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

picasa commented May 1, 2014

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

@geneorama

This comment has been minimized.

Show comment
Hide comment
@geneorama

geneorama Sep 16, 2015

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.

geneorama commented Sep 16, 2015

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

This comment has been minimized.

Show comment
Hide comment
@picasa

picasa 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('../'))

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

This comment has been minimized.

Show comment
Hide comment
@geneorama

geneorama Sep 16, 2015

@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!

geneorama commented Sep 16, 2015

@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

This comment has been minimized.

Show comment
Hide comment
@krlmlr

krlmlr Sep 16, 2015

Contributor

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.

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.

@LordCarol

This comment has been minimized.

Show comment
Hide comment
@LordCarol

LordCarol Jun 25, 2017

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.

LordCarol commented Jun 25, 2017

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

This comment has been minimized.

Show comment
Hide comment
@wzrzt

wzrzt Aug 23, 2017

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

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

This comment has been minimized.

Show comment
Hide comment
@yihui

yihui Aug 23, 2017

Owner

@wzrzt That is correct.

Owner

yihui commented Aug 23, 2017

@wzrzt That is correct.

@blaiseli

This comment has been minimized.

Show comment
Hide comment
@blaiseli

blaiseli 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 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

This comment has been minimized.

Show comment
Hide comment
@blaiseli

blaiseli 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="/"))

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

This comment has been minimized.

Show comment
Hide comment
@paulrougieux

paulrougieux Sep 26, 2018

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.

paulrougieux commented Sep 26, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment