set a path according to the user to a folder

Asked

Viewed 61 times

-3

I have a script that I am setting a path where are the datasets that I will work, but now the scripts will start to be run by other people on the team, as I do to leave the folder with dynamic value according to the user who uses the script.

setwd("C:/Users/PedroAugusto/Database")

I’m even creating a variable to receive the user from the machine, but I don’t know how to add this in setwd

u <- Sys.info()["user"]

I tried to do it but I didn’t succeed.

 setwd("C:/Users/u/Database")

In addition to the question, an Rscript will be used that is stored in a Devops, the databases are in a shared folder, when I use the Seto script to the folder with the datasets, in which case when another user runs it will only change the user that is using.

1 answer

2


u is the object that is stored the string text, is not the string itself. You can use paste to compose the path name. Or better yet, as pointed out by @Rui-Arradas, file.path:

file.path("C:/Users", u, "Database")

But if any user uses any different directory structure, the script will generate error. Can include the option for a different path to be passed as argument to Rscript, using the default path if none is provided:

# Captura argumentos da linha de comando
args <- commandArgs(trailingOnly = TRUE)

# Gera caminho padrão se nenhum for fornecido
if (!length(args)) {
  wd <- file.path("C:/Users", Sys.info()["user"], "Database")
  message("Caminho do Database não especificado. Usando ", wd)
} else {
  wd <- args[1]
}

# Tenta estabelecer o diretório de trabalho; emite mensagem de erro se o caminho não existir
tryCatch(setwd(wd), error = function(...) message("Erro: Diretório não encontrado"))

Pass the directory name as argument to Rscript when needed:

Rscript script.R /caminho/do/diretorio

Windows / *Nix

On *Nix systems, til indicates the current user root. You can expand the conditional to create the path according to the system type:

if (!length(args)) {
  if (.Platform$OS.type == "windows") {
    wd <- file.path("C:/Users", Sys.info()["user"], "Database")
  } else if (.Platform$OS.type == "unix") {
    wd <- "~/Database"
  } else {
    stop("Indicar caminho do Database")
  }
  message("Caminho do Database não especificado. Usando ", wd)
} else {
  wd <- args[1]
}

Only confirm if the value of .Platform$OS.type is even "windows", not using Windows to be able to check.

  • 1

    Instead of paste, file.path("C:/Users", u, "Database") is independent of the platform. + 1, by the way.

  • 1

    Thanks for the suggestion, I will update the reply. But platform independence is about using the backslash, not the users' directory structure. I had thought about using path.expand("~"), but I don’t know (nor can I verify) how consistent it behaves in Windows. As far as I have seen, you can direct to the user’s Documents folder depending on the configuration.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.