Set relative output directory in R
<p>I have an R script which reads data from multiple xlsx files, converts them into a dataframe and then creates folders in a directory at my computer based on the row values of the dataframe. I've set the main directory in the beginning of my code like that: </p>

<p><code>choose.dir(getwd(), "Choose a suitable folder")</code></p>

<p>And the output directory where the folders are created, as:</p>

<p><code>pth &lt;- "C:/Users/Dev/Desktop/test/"</code></p>

<p>I want to run the script at multiple computers, which means that the output directory named <em>pth</em> will not always be the same. Is there a way to set a relative output path, so that every time I run the script to specify where I want my results to be located? I tried </p>

<p><code>pth &lt;- choose.dir(default = "", caption = "Choose the output path")</code></p>

<p>Although a dialogue window pops up and I choose the desired directory, I can't see any of the folders there.</p>

<p>This is my code so far: </p>

<pre><code>#choose working directory
choose.dir(getwd(), "Choose a suitable folder")

library(xlsx)
library(tcltk)

#get file names
f = list.files("./")

#read files
dat &lt;- lapply(tk_choose.files(caption="Choose your files"), function(i) {
  x &lt;- read.xlsx(i, sheetIndex = 1, sheetName = NULL, startRow = 24,
                 endRow = NULL, as.data.frame = TRUE, header = FALSE, Filters = Filters[c("xlsx")])
  #return columns with names and colors
  x &lt;- x[, c(2, 5, 7, 9, 11, 13, 15, 17, 19), drop = FALSE]
  #return the data
  x
})

library(plyr)
df1 &lt;- ldply(dat, data.frame) ## convert list into a dataframe

#remove NA's
complete.cases(df1)
x &lt;- df1[complete.cases(df1),]
str(x)

#show specific rows
df1$X2 &lt;- gsub("[[:punct:]]","-",df1$X2)
df &lt;-df1[grepl("^[0-9]|^[a-zA-Z][0-9].*", df1$X2), ]
#print(df)

##pth &lt;- "C:/Users/Dev/Desktop/test/"
pth = choose.dir(default = "", caption = "Choose the output path")

# Iteratate within each row of df
for(i in 1:nrow(df)){
  if (!file.exists(pth))
    # Create 1st path
    dir.create(paste0(pth , df$X2[i]), na.omit(df$X2[i]))
  # Create 2nd and 3rd paths
  dir.create(paste0(pth, df$X2[i], "/",df$X5[i]), na.omit(df$X2[i]))
  dir.create(paste0(pth, df$X2[i], "/",df$X7[i]), na.omit(df$X2[i]))
  dir.create(paste0(pth, df$X2[i], "/",df$X9[i]), na.omit(df$X2[i]))
  dir.create(paste0(pth,  df$X2[i], "/",df$X11[i]), na.omit(df$X2[i]))
  dir.create(paste0(pth,  df$X2[i], "/",df$X13[i]), na.omit(df$X2[i]))
  dir.create(paste0(pth,  df$X2[i], "/",df$X15[i]), na.omit(df$X2[i]))
  dir.create(paste0(pth,  df$X2[i], "/",df$X17[i]), na.omit(df$X2[i]))
  dir.create(paste0(pth,  df$X2[i], "/",df$X19[i]), na.omit(df$X2[i]))
}
</code></pre>