英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论