RStudio导入CSV数据集失败求助:提示文件无效且无法找到文件
Let’s work through this step by step—this frustrating "No such file or directory" error often hides small, easy-to-miss issues, even if you’ve already checked the basics like working directory and restarting tools.
1. Bypass the Import Wizard: Use Absolute Paths Directly in Code
Skip RStudio’s import interface for a minute and test reading the file directly with read_csv() using its full absolute path. This avoids any quirks in the wizard and confirms if the path is actually valid:
- Windows: Use forward slashes or double backslashes (since single backslashes are escape characters), e.g.,
read_csv("C:/Users/YourName/Desktop/diet.csv")orread_csv("C:\\Users\\YourName\\Desktop\\diet.csv") - macOS/Linux: Use the full system path, e.g.,
read_csv("/Users/YourName/Documents/diet.csv")
You can copy the absolute path directly from your file manager: right-click the CSV, select "Copy Path" (Windows) or "Copy as Pathname" (macOS) to avoid typos.
2. Double-Check the File’s Exact Location and Name
It sounds simple, but these details trip up even experienced users:
- Confirm the filename matches exactly (case matters on macOS/Linux—
Diet.csvis not the same asdiet.csv). - Search your system for "diet.csv" to make sure it wasn’t accidentally moved, deleted, or saved to a different folder (like a external drive or cloud storage sync folder).
- Try moving the file to a short, simple path (like your desktop) to eliminate issues with long folder names or special characters (spaces, Chinese characters, accents) in the path.
3. Check File Permissions
Sometimes the file exists, but your user account doesn’t have permission to read it:
- Windows: Right-click the CSV → Properties → Security tab. Ensure your user account has "Read" permissions enabled.
- macOS: Right-click the file → Get Info → Sharing & Permissions. Make sure your user has at least "Read" access; click the lock icon to unlock and adjust settings if needed.
- Linux: In terminal, run
ls -l /path/to/diet.csvto check permissions, andchmod +r /path/to/diet.csvto grant read access if required.
4. Verify the File is Actually a Valid CSV
A file with a .csv extension might not be a proper comma-separated text file:
- Open it in a plain text editor (Notepad++, TextEdit, VS Code) to check:
- Is content separated by commas (or the correct delimiter)?
- Are there garbled characters or encoding issues?
- If encoding is the problem, specify it in
read_csv():
For Chinese text, tryread_csv("diet.csv", locale = locale(encoding = "GBK"))orlocale(encoding = "UTF-8").
5. Validate Your Working Directory Setup
Even if you think your working directory is correct, confirm it:
- Run
getwd()in the console to see your current working directory. - Run
list.files()to list all files in that directory—ifdiet.csvisn’t listed, either move the file to this directory or stick with absolute paths in your code. - (Pro tip: Using absolute paths in your scripts is more reliable long-term than relying on working directory settings.)
6. Test with Base R’s read.csv()
Rule out readr-specific issues by using base R’s import function:
read.csv("/full/path/to/diet.csv")
If this works, update your readr package to the latest version:
install.packages("readr")
7. Use Drag-and-Drop to Avoid Path Typos
A quick hack to eliminate manual path errors: drag the CSV file directly into RStudio’s console. RStudio will automatically paste the full, correct file path—you can copy that into read_csv() to ensure it’s 100% accurate.
内容的提问来源于stack exchange,提问作者Mar_Kan

