Set relative output directory in R -


i have r script reads data multiple xlsx files, converts them dataframe , creates folders in directory @ computer based on row values of dataframe. i've set main directory in beginning of code that:

choose.dir(getwd(), "choose suitable folder")

and output directory folders created, as:

pth <- "c:/users/dev/desktop/test/"

i want run script @ multiple computers, means output directory named pth not same. there way set relative output path, every time run script specify want results located? tried

pth <- choose.dir(default = "", caption = "choose output path")

although dialogue window pops , choose desired directory, can't see of folders there.

this code far:

#choose working directory choose.dir(getwd(), "choose suitable folder")  library(xlsx) library(tcltk)  #get file names f = list.files("./")  #read files dat <- lapply(tk_choose.files(caption="choose files"), function(i) {   x <- read.xlsx(i, sheetindex = 1, sheetname = null, startrow = 24,                  endrow = null, as.data.frame = true, header = false, filters = filters[c("xlsx")])   #return columns names , colors   x <- x[, c(2, 5, 7, 9, 11, 13, 15, 17, 19), drop = false]   #return data   x })  library(plyr) df1 <- ldply(dat, data.frame) ## convert list dataframe  #remove na's complete.cases(df1) x <- df1[complete.cases(df1),] str(x)  #show specific rows df1$x2 <- gsub("[[:punct:]]","-",df1$x2) df <-df1[grepl("^[0-9]|^[a-za-z][0-9].*", df1$x2), ] #print(df)  ##pth <- "c:/users/dev/desktop/test/" pth = choose.dir(default = "", caption = "choose 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 , 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])) } 

you can using relative path specifiers. example:

getwd() [1] "c:/users/uwe/documents/rdevel/coverage" setwd("../stackoverflow/")   # .. means go 1 directory level getwd() [1] "c:/users/uwe/documents/rdevel/stackoverflow" setwd("../..")   # go 2 levels getwd() [1] "c:/users/uwe/documents" 

this work on unix well.

btw: . denotes actual directory. so,

getwd() [1] "c:/users/uwe/documents" setwd("./rdevel/")   # . denotes actual directory getwd() [1] "c:/users/uwe/documents/rdevel" 

is equivalent to

getwd() [1] "c:/users/uwe/documents" setwd("rdevel") getwd() [1] "c:/users/uwe/documents/rdevel" 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -