write a csv file on working directory based on operation system in r

137 views Asked by At

I wrote a function ended with a csv output named table. I wanted to share the code with MacOS and Windows users, but MacOS users told me that they had problem with below code which I wrote for Windows. Could somebody help me make it somehow dynamic to return table.csv based on operation system without error. Thanks in advance.

write_csv(table, paste(getwd(), "/table.csv", sep = ""))
1

There are 1 answers

0
Mark On

The code you included works on Mac (source: I use a Mac, and I just ran the code below on my computer, and it worked perfectly)

df = data.frame(x = 1:10, y = 11:20)
write.csv(df, paste(getwd(), "/table.csv", sep = ""))

Running Sys.info()['sysname'] can give you the operating system of the user, so you could create something based on that, e.g.:

if (Sys.info()["sysname"] == "Darwin") {
  write.csv(df, "mac.csv")
} else if (Sys.info()["sysname"] == "Windows") {
  write.csv(df, "windows.csv")
} else {
  write.csv(df, "linux.csv")
}

It's hard to say without knowing more what the issues they are facing actually are.

Regardless, the issue doesn't affect all Mac users, so it is probably something more specific than the operating system one is using.