简体   繁体   中英

R command dir.create and file.path

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?

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.

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" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM