storaged
storaged

Reputation: 1847

Global paths conversions from WIndows to MACOS (R language example)

I tried to find some already existing solutions - no success so far.

The problem

There are some projects, all run in R, by a group of people, where each team member uses Windows as the main operating system.

Nearly each script file uses the following command at the very beginning

setwd("Z://00-00-00/path/to/project")

What is used here is some common disc space under the path Z://00-00-00/. Since I work on MAC OS my paths are /common-drive/path/to/project the question is:

Is there a way to include a command/script in some sort of file like ~/.bashrc or maybe some R-related settings that will convert Windows-like absolute file paths to paths that are MAC OS-like when they detect it?

What I think should run is:

path.to.be.used <- "Z://00-00-00/path/to/project"
str_replace(path.to.be.used, "Z://00-00-00/", "/common-drive/")

however, all scripts have the path hard-coded directly in setwd, so I cannot change each file by hand. That is why I am trying to find out some workaround that will convert these paths in a "silent mode".

Does anyone have an idea how to do this? Any way to make a control on system or R-studio level if the path should be converted?

Thank you for you time and help!

Upvotes: 0

Views: 340

Answers (1)

user2554330
user2554330

Reputation: 44897

As others said in the comments, you should convince your co-workers not to do that. However, that's often difficult, so here's a hack solution (mentioned by @MrFlick):

setwd <- function(dir) {
  newdir <- sub("Z://00-00-00/", "/common-drive/", dir)
  cat("Requested ", dir, ", using ", newdir, "\n")
  base::setwd(newdir)
}

Upvotes: 1

Related Questions