简体   繁体   English

当 R 说“没有这样的文件或目录”时,如何打开 R 中的 CSV 文件?

[英]How to open CSV file in R when R says "no such file or directory"?

I have an excel file that I want to open in R. I tried both of these commands after saving the excel file as a csv file or a text file.我有一个 excel 文件,我想在 R 中打开。在将 excel 文件保存为 csv 文件或文本文件后,我尝试了这两个命令。

read.table() or read.csv() read.table() 或 read.csv()

I think part of the problem is where the file is located.我认为部分问题在于文件所在的位置。 I have it saved on the desk top.我把它保存在桌面上。 What am I missing here?我在这里错过了什么?

Here is the R output这是 R output

In file(file, "rt") :
  cannot open file 'Rtrial.csv': No such file or directory
> help.search("read.csv")
> read.csv("Rtrial.csv")
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
  cannot open file 'Rtrial.csv': No such file or directory
> read.table("tab")

要放弃另一个选项,为什么不使用setwd('C:\John\Desktop')将工作目录(最好通过脚本)设置到桌面,然后仅使用文件名读取文件

Try尝试

f <- file.choose()

to choose the file interactively and save the name in f .以交互方式选择文件并将名称保存在f中。

Then run read.csv on the saved filename然后在保存的文件名上运行read.csv

d <- read.csv(f)

Sound like you just have an issue with the path.听起来您只是路径有问题。 Include the full path, if you use backslashes they need to be escaped: "C:\\folder\\folder\\Desktop\\file.csv" or "C:/folder/folder/Desktop/file.csv" .包括完整路径,如果您使用反斜杠,则需要对其进行转义: "C:\\folder\\folder\\Desktop\\file.csv""C:/folder/folder/Desktop/file.csv"

myfile = read.csv("C:/folder/folder/Desktop/file.csv")  # or read.table()

It may also be wise to avoid spaces and symbols in your file names, though I'm fairly certain spaces are OK.避免文件名中的空格和符号也可能是明智的,尽管我相当确定空格是可以的。

I had to combine Maiasaura and Svun answers to get it to work: using setwd and escaping all the slashes and spaces.我必须结合 Maiasaura 和 Svun 的答案才能让它工作:使用 setwd 并转义所有的斜线和空格。

setwd('C:\\Users\\firstname\ lastname\\Desktop\\folder1\\folder2\\folder3')
data = read.csv("file.csv")
data

This solved the issue for me.这为我解决了这个问题。

Here is one way to do it.这是一种方法。 It uses the ability of R to construct file paths based on the platform and hence will work on both Mac OS and Windows.它使用 R 的能力来构建基于平台的文件路径,因此可以在 Mac OS 和 Windows 上运行。 Moreover, you don't need to convert your xls file to csv, as there are many R packages that will help you read xls directly (eg gdata package).此外,您不需要将 xls 文件转换为 csv,因为有许多 R 包可以帮助您直接读取 xls(例如 gdata 包)。

# get user's home directory
home = setwd(Sys.getenv("HOME"));

# construct path to file
fpath = file.path(home, "Desktop", "RTrial.xls");

# load gdata library to read xls files
library(gdata);

# read xls file
Rtrial = read.xls(fpath);

Let me know if this works.让我知道这个是否奏效。

  1. Save as in excel will keep the file open and lock it so you can't open it.另存为 excel 将保持文件打开并锁定它,因此您无法打开它。 Close the excel file or you won't be able to use it in R.关闭 excel 文件,否则您将无法在 R 中使用它。
  2. Give the full path and escape backslashes read.csv("c:\\users\\JoeUser\\Desktop\\JoesData.csv")给出完整路径并转义反斜杠read.csv("c:\\users\\JoeUser\\Desktop\\JoesData.csv")

I have experienced that this error occurs when you either move the excel file to the destination other than where your r file is located or when you move your r file to the destination other than where your excel file is located.我经历过,当您将 excel 文件移动到 r 文件所在位置以外的目标位置或将 r 文件移动到 excel 文件所在位置以外的目标位置时,会发生此错误。

Good Practice:良好做法:

  1. Keep your .r and .csv files in the same directory.将 .r 和 .csv 文件保存在同一目录中。
  2. open your .r file from getting into its directory instead of opening the r file from rstuio's open file option.打开你的 .r 文件进入它的目录,而不是从 rstuio 的打开文件选项打开 r 文件。

You also have import Dataset option at Environment Block , just click there and get your required packages installed & from next time use this option to read datasets.您还可以在Environment Block导入 Dataset 选项,只需单击此处并安装所需的包,然后下次使用此选项读取数据集。 You will not get this error again.您不会再收到此错误。 I also appreciate the above provided answers.我也很欣赏上面提供的答案。

在此处输入图像描述

My issue was very simple, the working directory was not the "Source" directory that was printed when the file ran.我的问题很简单,工作目录不是文件运行时打印的“源”目录。 To fix this, you can use getwd() and setwd() to get your relative links working, or just use a full path when opening the csv.要解决此问题,您可以使用getwd()setwd()使您的相对链接正常工作,或者在打开 csv 时仅使用完整路径。

print(getwd()) # Where does the code think it is?
setwd("~/Documents") # Where do I want my code to be?
dat = read.csv("~/Documents/Data Visualization/expDataAnalysis/one/ac1_survey.csv") #just make it work!

Another way of reading Excel including the new format xlsx could be the package speedR (https://r-forge.r-project.org/projects/speedr/).包括新格式 xlsx 在内的另一种读取 Excel 的方法可能是包 speedR (https://r-forge.r-project.org/projects/speedr/)。 It is an interactive and visual data importer.它是一个交互式和可视化的数据导入器。 Besides importing you can filter(subset) the existing objects from the R workspace.除了导入之外,您还可以从 R 工作区过滤(子集)现有对象。

MAC OS It happened to me as well. MAC OS 这也发生在我身上。 I simply chose from the R toolbar MISC and then chose Change Working Directory.我只是从 R 工具栏 MISC 中选择,然后选择更改工作目录。 I was able to choose the directory that the .csv file was saved in. When I went back to the command line and typed getwd() the full directory was updated and correct and the read.csv function finally worked.我能够选择保存 .csv 文件的目录。当我返回命令行并键入 getwd() 时,完整目录已更新且正确,并且 read.csv 函数终于起作用了。

I had the same problem and when I checked the properties of the file on file explorer, it shows me the next message:我遇到了同样的问题,当我在文件资源管理器上检查文件的属性时,它显示了下一条消息:

"Security: This file came from another computer and might be blocked to help protect this computer" “安全性:此文件来自另一台计算机,可能会被阻止以帮助保护这台计算机”

You click on the "Unblock" button and... you can access to the file from R without any problem, just using read.csv() function and from the directory specified as your working directory, even if is not the same as the file's directory you are accessing to.您单击“取消阻止”按钮,然后......您可以毫无问题地从 R 访问文件,只需使用 read.csv() 函数并从指定为您的工作目录的目录,即使与您正在访问的文件的目录。

我刚遇到这个问题,我先切换到另一个目录,然后再切换回来,问题就解决了。

this work for me, accesing data from root.这对我有用,从根访问数据。 use double slash to access address.使用双斜杠访问地址。

dataset = read.csv('C:\\Users\\Desktop\\Machine Learning\\Data.csv')
  1. Kindly check whether the file name has an extension for example:请检查文件名是否有扩展名,例如:

     abc.csv

    if so remove the .csv extension.如果是这样,请删除.csv扩展名。

  2. set wd to the folder containing the file (~)wd设置为包含文件的文件夹(~)

  3. data<-read.csv("abc.csv")

Your data has been read the data object您的数据已被读取数据对象

In my case this very problem was raised by wrong spelling, lower case 'c:' instead of upper case 'C:' in the path.在我的情况下,这个问题是由拼写错误引起的,路径中的小写“c:”而不是大写“C:”。 I corrected spelling and problem vanished.我更正了拼写,问题消失了。

You can add absolute path to the file您可以添加文件的绝对路径

heisenberg <- read.csv(file="C:/Users/tiago/Desktop/sample_100000.csv")

If really want to run something like如果真的想运行类似

heisenberg <- read.csv(file="sample_100000.csv")

then you'll have to change the working directory to match the place the.CSV file is at.那么您必须更改工作目录以匹配 .CSV 文件所在的位置。 More about it here .更多信息请点击此处

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

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