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

huangapple go评论75阅读模式
英文:

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

问题

我编写了一个以csv输出命名为table的函数。我想与MacOS和Windows用户分享这段代码,但是MacOS用户告诉我他们在我为Windows编写的下面代码中遇到了问题。是否有人可以帮助我使它以某种动态方式根据操作系统返回table.csv而没有错误。提前致谢。

write_csv(table, paste(getwd(), "/table.csv", sep = ""))
英文:

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

得分: 1

以下是翻译好的部分:

你包含的代码在 Mac 上运行正常(来源:我使用的是 Mac,刚刚在我的电脑上运行了下面的代码,它完美运行)

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

运行 Sys.info()['sysname'] 可以获取用户的操作系统,所以你可以基于此创建一些内容,例如:

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")
}

没有更多的信息很难说出他们真正面临的问题是什么。

无论如何,这个问题并不影响所有的 Mac 用户,所以可能与使用的操作系统有关的内容更具体。
英文:

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.

huangapple
  • 本文由 发表于 2023年6月13日 10:11:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461318.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定