简体   繁体   English

R:如果(file.exists(

[英]R: If (file.exists(

I am just learning R, so this might be a simple question. 我只是在学习R,所以这可能是一个简单的问题。

I have a bunch of code that I want to run IF a certain file path that was generated earlier in the code exists. 我有一堆代码,如果代码中先前生成的某个文件路径存在,我想运行这些代码。 So... 所以...

1.A path is created 1.创建路径

2.If the path exists, then the rest of the code should run 2.如果路径存在,则其余代码应运行

This is what this portion of the code looks like: 这是这段代码的样子:

label_file=paste(label_folder,"Files/",as.matrix(babble_data)[n,3],label_extension,sep="")

if (file.exists(label_file){...

It is running without giving me any errors, but it is treating EVERY file path (even the ones that DO exist) like the don't exist. 它运行时没有给我任何错误,但是它像对待不存在一样对待每个文件路径(甚至存在的文件路径)。

Is my syntax incorrect? 我的语法不正确吗? All the examples I can find look like this, so I have no idea why it isn't working as it is supposed to. 我可以找到的所有示例都是这样,所以我不知道为什么它不按预期工作。 Any idea? 任何想法? Thanks. 谢谢。

You are missing a closing round bracket in the if statement before the curly bracket. 您在大括号前的if语句中缺少右括号。 This can have all possible side effects, although a code parsing error is the most probable one. 尽管可能最有可能发生代码解析错误,但这可能会产生所有可能的副作用。

Additionally, as.matrix(babble_data)[n,3] can produce a vector of results, so paste can produce a vector of filenames and if (file.exists( would fail. 另外, as.matrix(babble_data)[n,3]可以产生一个结果向量,因此paste可以产生一个文件名向量, if (file.exists(会失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM