6

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
  • Only the course teachers can answer to your question. Both commands do exactly the same thing. Commented Mar 5, 2018 at 12:58

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

Comments

6

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.