Jacob
Jacob

Reputation: 422

Run R script in different folder than data in Ubuntu Server

I have an R script in one directory that takes in the files in a different directory, combines them into one file, and outputs a new excel file, as shown below:

first <- read_excel("file1.xlsx")
second <- read_excel("file2.xlsx")
third <- read_excel("file3.xlsx")

df <- bind_rows(first,second,third)
openxlsx::write.xlsx(df, "newfile.xlsx")

In my code, I can set the working directory to a particular folder just putting setwd("path/to/data") but this only works in one directory. I'd like to make a shell script where I can loop through various folders.

I'd like to try something like

for i in folder1,folder2,folder3
do
  # Run Rscript myRscript.R in each folder
done

Ex. Folder 1 has file1, file2, and file3. Folder 2 has file1, file2, and file3. Folder 3 has file1, file2, file3. I'd like the Rscript to be one directory up from the folders and run in each folder and generate a "newfile.xlsx" file for each folder (Each folder is a different set of data but have all the same file names within each folder)

I want to avoid copying a version of the Rscript into each folder to avoid the folder changing nature of my request. Is this possible?

Upvotes: 0

Views: 198

Answers (1)

Martin
Martin

Reputation: 1201

You can loop through the folders and files with R no problem.

folders <- list.dirs()

for (folder in folders) {
  files <- list.files(folder)
  
  # extra: neglect non xlsx files
  # files <- files[which(str_detect(files, ".xlsx$"))]
  
  df <- tibble()
  
  for (file in files) {
    temp <- read_excel(file)
    df <- bind_rows(df, temp)
  }
  
  # creates a newfile.xlsx in each folder
  openxlsx::write.xlsx(df, file.path(folder, "newfile.xlsx"))
  
  # alternative: creates the newfile in the main folder
  # openxlsx::write.xlsx(df, paste0(folder, "_newfile.xlsx"))
}

Upvotes: 1

Related Questions