R命令dir.create和file.path [英] R command dir.create and file.path

查看:78
本文介绍了R命令dir.create和file.path的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习r,并对该课程中提出的以下问题感到困惑:

使用dir.create()和file.path()在一个命令中,在当前工作目录中创建一个名为"testdir2"的目录以及一个名为"testdir3"的子目录.

我无法接受我的答案,然后在网上找到另一个提供答案的网站.这是另一个网站给出的答案:

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

复制/粘贴此答案后,它仍然不能使我在课程中取得进步.答案有问题吗?

还有为什么我要使用file.path创建文件夹.这样做更没有道理吗?

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

使用file.path函数创建文件夹的目的是什么?

解决方案

使用 file.path()创建文件夹的目的是使您可以编写一个函数,脚本或程序包可供使用不同类型计算机的人使用.不同种类的计算机或平台使用不同的文件分隔符.Unix系统使用正斜杠:/.这包括macs.Windows系统使用反斜杠: \ .

尝试查看 .Platform ,您会看到有很多可以引用的变量,以便创建与平台无关的代码. .Machine 是另一个.

无论如何,无论在哪种平台R上运行, file.path(dir1,dir2,dir3)都可以创建有效路径.

关于自动分级机为何不接受您的答案的原因,它们可能非常挑剔,尤其是对于隐藏的空白字符,您有时在复制和粘贴时会发现这些空白字符.有时他们会测试您的命令产生的输出,但是有时(不好的)自动分级机只会测试输入,因此,即使您的命令会产生相同的行为,即使命令不完全相同,自动分级也不会接受./p>

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.

这篇关于R命令dir.create和file.path的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆