Dear useRs, I believe this is R code so appropriate for this list, but let me know if this relates more to RStudio itself. I am working on an RStudio project. In that project directory, I have a folder called 'analysis' and in there a folder called 'scripts' ('~/analysis/scripts'). My data files needed for the scripts are in '~/analysis/raw_data' and the output should be in '~/analysis/derived_data'. My scripts are Rmd files, so when I knit them, their working directory is where they are located, i.e. '~/analysis/scripts'. The problem I then have is to specify the path for 'raw_data' and 'derived_data' since during the rendering I am not relative to the project directory anymore. And these folders are not subfolders of the working directory? '~/analysis/scripts'. I hope I am clear here... I would like to avoid absolute paths of course, but I do not know how to proceed. What would be nice is a way to get the project directory in the scripts, rather than their working directory. Does that make sense? Thank you in advance Best, Ivan -- Dr. Ivan Calandra TraCEr, laboratory for Traceology and Controlled Experiments MONREPOS Archaeological Research Centre and Museum for Human Behavioural Evolution Schloss Monrepos 56567 Neuwied, Germany +49 (0) 2631 9772-243 https://www.researchgate.net/profile/Ivan_Calandra
I recommend not using setwd. Then you can always assume your current working directory is your project directory and reference relative to that. On April 2, 2020 1:30:29 AM PDT, Ivan Calandra <calandra at rgzm.de> wrote:>Dear useRs, > >I believe this is R code so appropriate for this list, but let me know >if this relates more to RStudio itself. > >I am working on an RStudio project. In that project directory, I have a >folder called 'analysis' and in there a folder called 'scripts' >('~/analysis/scripts'). >My data files needed for the scripts are in '~/analysis/raw_data' and >the output should be in '~/analysis/derived_data'. > >My scripts are Rmd files, so when I knit them, their working directory >is where they are located, i.e. '~/analysis/scripts'. The problem I >then >have is to specify the path for 'raw_data' and 'derived_data' since >during the rendering I am not relative to the project directory >anymore. >And these folders are not subfolders of the working directory? >'~/analysis/scripts'. >I hope I am clear here... > >I would like to avoid absolute paths of course, but I do not know how >to >proceed. >What would be nice is a way to get the project directory in the >scripts, >rather than their working directory. >Does that make sense? > >Thank you in advance >Best, >Ivan-- Sent from my phone. Please excuse my brevity.
Hi Jeff, But if I do not use setwd(), the current working directory is NOT the project directory. That's what my problem is about... I guess I was not clear in my email... Ivan -- Dr. Ivan Calandra TraCEr, laboratory for Traceology and Controlled Experiments MONREPOS Archaeological Research Centre and Museum for Human Behavioural Evolution Schloss Monrepos 56567 Neuwied, Germany +49 (0) 2631 9772-243 https://www.researchgate.net/profile/Ivan_Calandra On 02/04/2020 10:37, Jeff Newmiller wrote:> I recommend not using setwd. Then you can always assume your current working directory is your project directory and reference relative to that. > > On April 2, 2020 1:30:29 AM PDT, Ivan Calandra <calandra at rgzm.de> wrote: >> Dear useRs, >> >> I believe this is R code so appropriate for this list, but let me know >> if this relates more to RStudio itself. >> >> I am working on an RStudio project. In that project directory, I have a >> folder called 'analysis' and in there a folder called 'scripts' >> ('~/analysis/scripts'). >> My data files needed for the scripts are in '~/analysis/raw_data' and >> the output should be in '~/analysis/derived_data'. >> >> My scripts are Rmd files, so when I knit them, their working directory >> is where they are located, i.e. '~/analysis/scripts'. The problem I >> then >> have is to specify the path for 'raw_data' and 'derived_data' since >> during the rendering I am not relative to the project directory >> anymore. >> And these folders are not subfolders of the working directory? >> '~/analysis/scripts'. >> I hope I am clear here... >> >> I would like to avoid absolute paths of course, but I do not know how >> to >> proceed. >> What would be nice is a way to get the project directory in the >> scripts, >> rather than their working directory. >> Does that make sense? >> >> Thank you in advance >> Best, >> Ivan
On Thu, 2 Apr 2020 10:30:29 +0200 Ivan Calandra <calandra at rgzm.de> wrote:> The problem I then have is to specify the path for 'raw_data' and > 'derived_data' <...> And these folders are not subfolders of > the working directory '~/analysis/scripts'.> I would like to avoid absolute paths of courseIs there a reason to avoid relative paths built using '..' to access parent directories? -- Best regards, Ivan
I do not know this ".." command (could you please show me how to use it in a relative path?), but it sounds like a good start. But it implies that I know in advance how many folders up the parent directory is. I guess in most cases it will always be the same, but it would be even better if it could be applied generically. As I said, ideally, I would like to get the project directory from a script located in a subfolder. Thanks! Ivan -- Dr. Ivan Calandra TraCEr, laboratory for Traceology and Controlled Experiments MONREPOS Archaeological Research Centre and Museum for Human Behavioural Evolution Schloss Monrepos 56567 Neuwied, Germany +49 (0) 2631 9772-243 https://www.researchgate.net/profile/Ivan_Calandra On 02/04/2020 10:54, Ivan Krylov wrote:> On Thu, 2 Apr 2020 10:30:29 +0200 > Ivan Calandra <calandra at rgzm.de> wrote: > >> The problem I then have is to specify the path for 'raw_data' and >> 'derived_data' <...> And these folders are not subfolders of >> the working directory '~/analysis/scripts'. > >> I would like to avoid absolute paths of course > Is there a reason to avoid relative paths built using '..' to access > parent directories? >