Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R command dir.create and file.path

Tags:

directory

path

r

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?

like image 794
Steve Avatar asked Mar 05 '18 12:03

Steve


People also ask

How do you create a file path in R?

You can use getwd() to obtain the current working directory. To change the directory, use setwd("/path/to/your/working/directory") . If you have a permanent path in which the file functions. r will always be present, then you can use source("/path/to/permanent/directory/functions.

How do I set the path to a folder in R?

Under Windows and MAC OSX Launch R by double-clicking on the icon. Specify your working directory to R: On Windows: File –> Change directory. On MAC OSX: Tools –> Change the working directory.

What does dir create do in R?

dir. create creates the last element of the path, unless recursive = TRUE . Trailing path separators are discarded. The mode will be modified by the umask setting in the same way as for the system function mkdir .

How do I create a file path?

If you're using Windows 10, hold down Shift on your keyboard and right-click on the file, folder, or library for which you want a link. If you're using Windows 11, simply right-click on it. Then, select “Copy as path” in the contextual menu.


2 Answers

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.

like image 99
De Novo Avatar answered Nov 08 '22 22:11

De Novo


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

like image 43
TMAN Avatar answered Nov 08 '22 20:11

TMAN