R command dir.create and file.path

て烟熏妆下的殇ゞ 提交于 2021-02-19 01:34:51

问题


I've just started learning r and confused by the following question given in the course:

Create a directory in the current working directory called “testdir2” and a subdirectory for it called “testdir3”, all in one command by using dir.create() and file.path().

I couldn't get it to accept my answer and then found another site online giving the answers. This is the answer the other site gave:

dir.create(file.path('testdir2', 'testdir3'), recursive = TRUE)

After copy/pasting this answer it still didn't let me progress in the course. Is there something wrong with the answer?

Also why would i want to use file.path to create the folders. Would it not make more sense to do this:

dir.create("testdir2/testdir3", recursive = TRUE)

What is the purpose of using the file.path function to create folders?


回答1:


The purpose of using file.path() to create folders is so that you can write a function, script, or package that can be used by people that are using different kinds of computers. Different kinds of computers, or Platforms, use different file separators. Unix systems use the forward slash: /. This includes macs. Windows systems use the back slash: \.

Try looking at .Platform and you'll see there are a number of variables that you can refer to in order to create platform independent code. .Machine is another one.

Anyhow, the idea is that file.path(dir1, dir2, dir3) can create a valid path no matter what kind of platform R is running on.

As to why an auto grader didn't accept your answer, they can be very finicky, especially about hidden whitespace characters you can sometimes pick up when copying and pasting. Sometimes they test the output that your command produces, but sometimes (bad) auto graders just test the input, so even if your command would produce the same behavior, if it's not exactly the same, the auto grader won't accept it.




回答2:


I got this code to work:

dir.create(file.path("testdir2","testdir3"), recursive = TRUE)

I think you just needed double quotes around "testdir2" and "testdir3".



来源:https://stackoverflow.com/questions/49110819/r-command-dir-create-and-file-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!