gpt4 book ai didi

r - file.create 语法

转载 作者:行者123 更新时间:2023-12-02 01:11:52 25 4
gpt4 key购买 nike

R 的 super 大新手。我在 file.create 函数上有点卡住了。我已经成功地使用它在设置的工作目录中创建了一个文件,并且当我已经创建了一个单独的文件路径并将该文件路径分配给一个变量时。

但是,为什么我不能使用 file.create 并在没有 file.path 函数的情况下简单地列出所需的文件路径和文件名?是否file.create函数不具备在指定目录下自动创建文件的能力,但需要file.path功能来保护目录的路径?

任何澄清将不胜感激。如果这个问题相当初级,我深表歉意,但我想了解基本原理。

这是有效的代码:

BasicDir <- "/Users/slam1924/Desktop/LearnR Tutorials"
setwd(BasicDir)
file.create("myfile.doc")
fp1 <- file.path("/Users/slam1924/Desktop/Vocal Covers", "mytext.doc")
fp1
file.create(fp1)

选择:
file.create(file.path("/Users/slam1924/Desktop/Vocal Covers", "mytext.doc"))

这是失败的代码:
file.create("/Users/slam1924/Desktop/Vocal Covers", "mytext.doc")

最佳答案

首先阅读函数的帮助。 help(file.create) .用法是file.create(..., showWarnings = TRUE)
在详细信息下,您将看到

file.create creates files with the given names if they do not already exist and truncates them if they do.



所以当你尝试
file.create("/Users/slam1924/Desktop/Vocal Covers", "mytext.doc")

它正在尝试创建两个文件,其中一个(“/U​​sers/slam1924/Desktop/Vocal Covers”)可能已经是一个目录。

如果文件或目录已经存在,您将看到如下错误:
[1] FALSE
Warning message:
In file.create("data") :
cannot create file 'data', reason 'Permission denied'

您可以通过向函数发送一个字符串来解决此问题。更改失败的代码:
file.create("/Users/slam1924/Desktop/Vocal Covers/mytext.doc")

关于r - file.create 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44933217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com