-
-
Notifications
You must be signed in to change notification settings - Fork 873
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
Comments
I understand the situation, but why not |
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. |
Hi, I have added a package option 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. |
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 |
@paul4forest Glad to know that :) |
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 You should use |
OK, i'll stick to this solution. thank you ! |
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 There are two problems with using
Therapy is expensive, so it would be nice to resolve this. |
@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().
|
@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! |
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. |
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" That will change the work directory for all chunks. |
@wzrzt That is correct. |
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, It would be very useful to either have a global option to allow path changes in the chunks to affect the Is it something possible? |
Apparently, {r, include=FALSE} And later: {r, include=FALSE} |
When set in a chunk |
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. |
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 tosetwd()
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 withindocs/
that the.Rmd
document happens to live in. That's why I callsetwd()
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.The text was updated successfully, but these errors were encountered: