RE: [knitr] Globally setting a working directory for R in a knitr document

578 views
Skip to first unread message

Michael Nelson

unread,
Jun 14, 2012, 7:29:29 PM6/14/12
to Roey Angel, kn...@googlegroups.com
By input files do you mean

1) R scripts from which you read chunks or 
2) data files which you are reading into R

I will describe my current work flow 

My current work flow has my working directory set and I DO NOT change this.

I have my .rnw (or .rmd (markdown not sweave / latex) files in a sub directory

'code/'

and my data in another subdirectory

'data/'

To knit a document I manually copy the file from code/my-file.rnw to the working directory

using 


file.copy('code/my-file.rnw','my-file.rnw')
output <- knit2pdf('my-file.rnw')


all the paths in this document are relative to the working directory (not the code subdirectory)

If you are using Rstudio set the default working directory to your current working directory (or use a project based in that working directory)


Michael



From: kn...@googlegroups.com [kn...@googlegroups.com] on behalf of Roey Angel [roey....@gmail.com]
Sent: Thursday, 14 June 2012 6:30 PM
To: kn...@googlegroups.com
Subject: [knitr] Globally setting a working directory for R in a knitr document

Hi,
Just switched from Sweave to Knitr so this is probably basic.
I'm running a knitted document from a different directory than where the input files for my R chunks are.
As it is now, I have to provide setwd() in each chunk individually for it to find the files (although it's the same wd throughout the document).

I tried:
1. switching to the wd in RStudio before running the code
2. using setwd() in the same chunk as the knitr options
3. using dependson="chunk-where-setwd()-is-set"
but none worked

Tips are welcomed.

Thanks
Roey
Reply all
Reply to author
Forward
0 new messages