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?
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.
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.
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 .
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.
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"
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With